Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 72237a8

Browse files
feat(core): validate if the user id and room id follow the right pattern
1 parent d004243 commit 72237a8

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

src/core/index.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,44 @@ describe('initialization errors', () => {
149149
'Color sv-primary-900 is not a valid color variable value. Please check the documentation for more information.',
150150
);
151151
});
152+
153+
test('should throw an error if room id is invalid', async () => {
154+
await expect(
155+
sdk(UNIT_TEST_API_KEY, {
156+
...SIMPLE_INITIALIZATION_MOCK,
157+
roomId: '<invalid-room-id>',
158+
}),
159+
).rejects.toThrow(
160+
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
161+
);
162+
163+
await expect(
164+
sdk(UNIT_TEST_API_KEY, {
165+
...SIMPLE_INITIALIZATION_MOCK,
166+
roomId: '1',
167+
}),
168+
).rejects.toThrow(
169+
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
170+
);
171+
});
172+
173+
test('should throw an error if participant id is invalid', async () => {
174+
await expect(
175+
sdk(UNIT_TEST_API_KEY, {
176+
...SIMPLE_INITIALIZATION_MOCK,
177+
participant: { ...SIMPLE_INITIALIZATION_MOCK.participant, id: '<invalid-participant-id>' },
178+
}),
179+
).rejects.toThrow(
180+
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
181+
);
182+
183+
await expect(
184+
sdk(UNIT_TEST_API_KEY, {
185+
...SIMPLE_INITIALIZATION_MOCK,
186+
participant: { ...SIMPLE_INITIALIZATION_MOCK.participant, id: '1' },
187+
}),
188+
).rejects.toThrow(
189+
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
190+
);
191+
});
152192
});

src/core/index.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ import RemoteConfigService from '../services/remote-config-service';
1010
import LauncherFacade from './launcher';
1111
import { LauncherFacade as LauncherFacadeType } from './launcher/types';
1212

13+
/**
14+
* @function validateId
15+
* @description validate if the id follows the constraints
16+
* @param {string} id - id to validate
17+
* @returns {boolean}
18+
*/
19+
function validateId(id: string): boolean {
20+
const lengthConstraint = /^.{2,64}$/;
21+
const pattern = /^[-_&@+=,(){}\[\]\/«».:|'"#a-zA-Z0-9À-ÿ\s]*$/;
22+
23+
if (!lengthConstraint.test(id)) {
24+
return false;
25+
}
26+
27+
if (!pattern.test(id)) {
28+
return false;
29+
}
30+
31+
return true;
32+
}
33+
1334
/**
1435
* @function validateOptions
1536
* @description Validate the options passed to the SDK
@@ -27,15 +48,27 @@ const validateOptions = ({
2748
}
2849

2950
if (!group || !group.name || !group.id) {
30-
throw new Error('Group fields is required');
51+
throw new Error('[SuperViz] Group fields is required');
3152
}
3253

3354
if (!participant || !participant.id || !participant.name) {
34-
throw new Error('Participant name and id is required');
55+
throw new Error('[SuperViz] Participant name and id is required');
3556
}
3657

3758
if (!roomId) {
38-
throw new Error('Room id is required');
59+
throw new Error('[SuperViz] Room id is required');
60+
}
61+
62+
if (!validateId(roomId)) {
63+
throw new Error(
64+
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
65+
);
66+
}
67+
68+
if (!validateId(participant.id)) {
69+
throw new Error(
70+
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
71+
);
3972
}
4073
};
4174

0 commit comments

Comments
 (0)