fix(core): Fix XSS validation and separate URL validation (#10424)

This commit is contained in:
Iván Ovejero
2024-08-16 10:49:08 +02:00
committed by GitHub
parent 9d6ad88c14
commit 91467ab325
11 changed files with 165 additions and 65 deletions

View File

@@ -1,43 +0,0 @@
import { NoXss } from '@db/utils/customValidators';
import { validate } from 'class-validator';
describe('customValidators', () => {
describe('NoXss', () => {
class Person {
@NoXss()
name: string;
}
const person = new Person();
const invalidNames = ['http://google.com', '<script src/>', 'www.domain.tld'];
const validNames = [
'Johann Strauß',
'Вагиф Сәмәдоғлу',
'René Magritte',
'সুকুমার রায়',
'མགོན་པོ་རྡོ་རྗེ།',
'عبدالحليم حافظ',
];
describe('Block XSS', () => {
for (const name of invalidNames) {
test(name, async () => {
person.name = name;
const validationErrors = await validate(person);
expect(validationErrors[0].property).toEqual('name');
expect(validationErrors[0].constraints).toEqual({ NoXss: 'Malicious name' });
});
}
});
describe('Allow Valid names', () => {
for (const name of validNames) {
test(name, async () => {
person.name = name;
expect(await validate(person)).toBeEmptyArray();
});
}
});
});
});

View File

@@ -1,18 +0,0 @@
import { registerDecorator } from 'class-validator';
export function NoXss() {
return (object: object, propertyName: string): void => {
registerDecorator({
name: 'NoXss',
target: object.constructor,
propertyName,
constraints: [propertyName],
options: { message: `Malicious ${propertyName}` },
validator: {
validate(value: string) {
return !/(^http|^www)|<(\s*)?(script|a)|(\.[\p{L}\d-]+)/u.test(value);
},
},
});
};
}