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

feat(core): validate if the user id and room id follow the right pattern #727

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,44 @@ describe('initialization errors', () => {
'Color sv-primary-900 is not a valid color variable value. Please check the documentation for more information.',
);
});

test('should throw an error if room id is invalid', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
roomId: '<invalid-room-id>',
}),
).rejects.toThrow(
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);

await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
roomId: '1',
}),
).rejects.toThrow(
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);
});

test('should throw an error if participant id is invalid', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
participant: { ...SIMPLE_INITIALIZATION_MOCK.participant, id: '<invalid-participant-id>' },
}),
).rejects.toThrow(
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);

await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
participant: { ...SIMPLE_INITIALIZATION_MOCK.participant, id: '1' },
}),
).rejects.toThrow(
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);
});
});
39 changes: 36 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import RemoteConfigService from '../services/remote-config-service';
import LauncherFacade from './launcher';
import { LauncherFacade as LauncherFacadeType } from './launcher/types';

/**
* @function validateId
* @description validate if the id follows the constraints
* @param {string} id - id to validate
* @returns {boolean}
*/
function validateId(id: string): boolean {
const lengthConstraint = /^.{2,64}$/;
const pattern = /^[-_&@+=,(){}\[\]\/«».:|'"#a-zA-Z0-9À-ÿ\s]*$/;

if (!lengthConstraint.test(id)) {
return false;
}

if (!pattern.test(id)) {
return false;
}

return true;
}

/**
* @function validateOptions
* @description Validate the options passed to the SDK
Expand All @@ -27,15 +48,27 @@ const validateOptions = ({
}

if (!group || !group.name || !group.id) {
throw new Error('Group fields is required');
throw new Error('[SuperViz] Group fields is required');
}

if (!participant || !participant.id || !participant.name) {
throw new Error('Participant name and id is required');
throw new Error('[SuperViz] Participant name and id is required');
}

if (!roomId) {
throw new Error('Room id is required');
throw new Error('[SuperViz] Room id is required');
}

if (!validateId(roomId)) {
throw new Error(
'[SuperViz] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);
}

if (!validateId(participant.id)) {
throw new Error(
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"',
);
}
};

Expand Down
Loading