Skip to content

Commit 2ef6d61

Browse files
authored
feat(consts): introduce profile name (first, last, organization, ...) restrictions and adjust username regex [internal] (#486)
1 parent 2a4b4d5 commit 2ef6d61

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

packages/consts/src/consts.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DNS_SAFE_NAME_REGEX } from './regexs';
1+
import { DNS_SAFE_NAME_REGEX, EMAIL_REGEX } from './regexs';
22

33
export const FREE_SUBSCRIPTION_PLAN_CODE = 'DEV';
44

@@ -97,6 +97,11 @@ export const ACTOR_TYPES = {
9797
CRAWLER: 'crawlers',
9898
} as const;
9999

100+
/**
101+
* Used as username for returning user own info from API v2/users/username
102+
*/
103+
export const ME_USER_NAME_PLACEHOLDER = 'me';
104+
100105
/**
101106
* Username used when user is anonymous.
102107
*/
@@ -114,6 +119,19 @@ export const USERNAME = {
114119
REGEX: /^[a-zA-Z0-9_.-]{3,30}$/,
115120
};
116121

122+
export const EMAIL = {
123+
MAX_LENGTH: 254, // see https://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
124+
REGEX: EMAIL_REGEX,
125+
};
126+
127+
/**
128+
* Profile name (such as organization or first / last name) constraints.
129+
*/
130+
export const PROFILE_NAME = {
131+
MAX_LENGTH: 50,
132+
REGEX: /^(?!.*:\/\/)[^@><]*$/, // Prohibits usage of @, <, > and ://
133+
};
134+
117135
/**
118136
* Max length for DNS safe string
119137
*/
@@ -239,11 +257,6 @@ export const DEFAULT_PLATFORM_LIMITS = {
239257
MAX_TASKS_PER_SCHEDULER: 10,
240258
};
241259

242-
/**
243-
* Use as username for returning user own info from API v2/users/username
244-
*/
245-
export const ME_USER_NAME_PLACEHOLDER = 'me';
246-
247260
/**
248261
* Max length of the queue head that server will return in Request Queue API.
249262
*/

test/consts.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { USERNAME, APIFY_ID_REGEX, ACTOR_ENV_VARS, ENV_VARS, APIFY_ENV_VARS, LOCAL_ACTOR_ENV_VARS, LOCAL_APIFY_ENV_VARS, LOCAL_ENV_VARS } from '@apify/consts';
1+
import {
2+
USERNAME,
3+
APIFY_ID_REGEX,
4+
ACTOR_ENV_VARS,
5+
ENV_VARS,
6+
APIFY_ENV_VARS,
7+
LOCAL_ACTOR_ENV_VARS,
8+
LOCAL_APIFY_ENV_VARS,
9+
LOCAL_ENV_VARS,
10+
PROFILE_NAME,
11+
} from '@apify/consts';
212
import { cryptoRandomObjectId } from '@apify/utilities';
313

414
describe('consts', () => {
@@ -17,6 +27,30 @@ describe('consts', () => {
1727
});
1828
});
1929

30+
describe('PROFILE_NAME', () => {
31+
it('REGEX works as expected', () => {
32+
// Valid cases
33+
expect(PROFILE_NAME.REGEX.test('John Doe')).toBe(true);
34+
expect(PROFILE_NAME.REGEX.test('Anonymous')).toBe(true);
35+
expect(PROFILE_NAME.REGEX.test('John123')).toBe(true);
36+
expect(PROFILE_NAME.REGEX.test('John-Doe')).toBe(true);
37+
expect(PROFILE_NAME.REGEX.test('Org_Example')).toBe(true);
38+
expect(PROFILE_NAME.REGEX.test(':/JohnDoe')).toBe(true);
39+
expect(PROFILE_NAME.REGEX.test(':/a/Simple.Name')).toBe(true);
40+
expect(PROFILE_NAME.REGEX.test('John:/Doe/')).toBe(true);
41+
expect(PROFILE_NAME.REGEX.test('Simple:.//Name')).toBe(true);
42+
expect(PROFILE_NAME.REGEX.test('Joh////:/n-Doe')).toBe(true);
43+
expect(PROFILE_NAME.REGEX.test('user:name')).toBe(true);
44+
45+
// Invalid cases
46+
expect(PROFILE_NAME.REGEX.test('user@name')).toBe(false);
47+
expect(PROFILE_NAME.REGEX.test('user>name')).toBe(false);
48+
expect(PROFILE_NAME.REGEX.test('user<name')).toBe(false);
49+
expect(PROFILE_NAME.REGEX.test('example://test')).toBe(false);
50+
expect(PROFILE_NAME.REGEX.test('example://////test')).toBe(false);
51+
});
52+
});
53+
2054
describe('APIFY_ID_REGEX', () => {
2155
it('matches testing apify IDs', () => {
2256
const testingStrings = {

0 commit comments

Comments
 (0)