Skip to content

Commit

Permalink
fix: use better email regex
Browse files Browse the repository at this point in the history
* The email regex was too strict, it didn't allow a domain longer than 3 characters.
* Also, we had multiple regexes for email. The regex string is moved to utils
file so it can be reused.
  • Loading branch information
Vojtech-Sassmann committed Sep 6, 2021
1 parent 57c39cb commit d158c45
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NotificatorService } from '@perun-web-apps/perun/services';
import { OwnersManagerService } from '@perun-web-apps/perun/openapi';
import { TranslateService } from '@ngx-translate/core';
import { FormControl, Validators } from '@angular/forms';
import { emailRegexString } from '@perun-web-apps/perun/utils';

@Component({
selector: 'app-add-owner-dialog',
Expand All @@ -28,11 +29,9 @@ export class AddOwnerDialogComponent implements OnInit {
contactCtrl: FormControl;
type = '1';

emailRegex = /^(([^<>+()[\]\\.,;:\s@"-#$%&=]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$/;

ngOnInit() {
this.nameCtrl = new FormControl(null, [Validators.required, Validators.pattern('^[\\w.-]+( [\\w.-]+)*$')]);
this.contactCtrl = new FormControl(null, [Validators.required, Validators.pattern(this.emailRegex)]);
this.contactCtrl = new FormControl(null, [Validators.required, Validators.pattern(emailRegexString)]);
}

onCancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ServicesManagerService
} from '@perun-web-apps/perun/openapi';
import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms';
import { emailRegexString } from '@perun-web-apps/perun/utils';

export interface AddServicesDestinationDialogData {
facility: Facility;
Expand Down Expand Up @@ -46,7 +47,7 @@ export class AddServicesDestinationDialogComponent implements OnInit {
loading = false;
emailControl: FormControl;

emailRegex = new RegExp(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
emailRegex = new RegExp(emailRegexString);
hostPattern = new RegExp("^(?!:\\/\\/)(?=.{1,255}$)((.{1,63}\\.){1,127}(?![0-9]*$)[a-z0-9-]+\\.?)$|^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
urlPattern = new RegExp("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;()*$']*[-a-zA-Z0-9+&@#/%=~_|()*$']$");
userAtHostPattern = new RegExp("^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\\$)@(?:(?!:\\/\\/)(?=.{1,255}$)((.{1,63}\\.){1,127}(?![0-9]*$)[a-z0-9-]+\\.?)$|(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Role } from '@perun-web-apps/perun/models';
import { of, timer } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import { ErrorStateMatcher } from '@angular/material/core';
import { emailRegexString } from '@perun-web-apps/perun/utils';

export interface CreateSponsoredMemberDialogData {
entityId?: number;
Expand Down Expand Up @@ -77,8 +78,6 @@ export class CreateSponsoredMemberDialogComponent implements OnInit {
parsedRules: Map<string, {login: string, password: string}> =
new Map<string, {login: string, password: string}>();

emailRegx = /^(([^<>+()[\]\\.,;:\s@"-#$%&=]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$/;

userControl: FormGroup = null;
namespaceControl: FormGroup = null;
passwordStateMatcher = new ImmediateStateMatcher();
Expand Down Expand Up @@ -123,7 +122,7 @@ export class CreateSponsoredMemberDialogComponent implements OnInit {
password: ['', Validators.required, [loginAsyncValidator(null, this.usersService, this.apiRequestConfiguration)]],
passwordReset: [false, []],
showPassword: [false, []],
email: ['', [Validators.required, Validators.pattern(this.emailRegx)]]
email: ['', [Validators.required, Validators.pattern(emailRegexString)]]
});

this.membersService.getAllNamespacesRules().subscribe(rules => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { formatDate } from '@angular/common';
import { SelectionModel } from '@angular/cdk/collections';
import { Urns } from '@perun-web-apps/perun/urns';
import { TABLE_VO_GROUPS, TableConfigService } from '@perun-web-apps/config/table-config';
import { downloadData } from '@perun-web-apps/perun/utils';
import { downloadData, emailRegexString } from '@perun-web-apps/perun/utils';

export interface GenerateSponsoredMembersDialogData {
voId: number;
Expand All @@ -33,8 +33,6 @@ export class GenerateSponsoredMembersDialogComponent implements OnInit {
loading = false;
functionalityNotSupported = false;

emailRegx = /^(([^<>+()[\]\\.,;:\s@"-#$%&=]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$/;

namespaceOptions: string[] = [];
namespaceRules: NamespaceRules[] = [];
usersInfoFormGroup: FormGroup;
Expand Down Expand Up @@ -241,7 +239,7 @@ export class GenerateSponsoredMembersDialogComponent implements OnInit {
}
//now we expect that mail is always on the same index - third position
if (arrayOfAttributes[2].slice(arrayOfAttributes[2].length - 4).toLowerCase() === 'mail') { //check if the third attribute is mail
if (!memberAttributes[2].trim().match(this.emailRegx)) { //check if the email is valid email
if (!memberAttributes[2].trim().match(emailRegexString)) { //check if the email is valid email
return 'email';
}
}
Expand Down
2 changes: 2 additions & 0 deletions libs/perun/utils/src/lib/perun-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { saveAs } from 'file-saver';

export const TABLE_ITEMS_COUNT_OPTIONS = [5, 10, 25, 100];

export const emailRegexString = /^(([^<>+()[\]\\.,;:\s@"-#$%&=]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]+))$/;

/**
* Gets email of given member. The preferred email has top priority, the vo-email
* has lower priority. If there are no emails, an empty string is returned.
Expand Down

0 comments on commit d158c45

Please sign in to comment.