-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): Fix XSS validation and separate URL validation #10424
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NoXss } from '../no-xss.validator'; | ||
import { validate } from 'class-validator'; | ||
|
||
describe('NoXss', () => { | ||
class Entity { | ||
@NoXss() | ||
name = ''; | ||
|
||
@NoXss() | ||
timestamp = ''; | ||
|
||
@NoXss() | ||
version = ''; | ||
} | ||
|
||
const entity = new Entity(); | ||
|
||
describe('Scripts and URLs', () => { | ||
const MALICIOUS_STRINGS = ['http://google.com', '<script src/>', 'www.domain.tld']; | ||
|
||
for (const str of MALICIOUS_STRINGS) { | ||
test(`should block ${str}`, async () => { | ||
entity.name = str; | ||
const [error] = await validate(entity); | ||
expect(error.property).toEqual('name'); | ||
expect(error.constraints).toEqual({ NoXss: 'Potentially malicious string' }); | ||
}); | ||
} | ||
}); | ||
|
||
describe('Names', () => { | ||
const VALID_NAMES = [ | ||
'Johann Strauß', | ||
'Вагиф Сәмәдоғлу', | ||
'René Magritte', | ||
'সুকুমার রায়', | ||
'མགོན་པོ་རྡོ་རྗེ།', | ||
'عبدالحليم حافظ', | ||
]; | ||
|
||
for (const name of VALID_NAMES) { | ||
test(`should allow ${name}`, async () => { | ||
entity.name = name; | ||
expect(await validate(entity)).toBeEmptyArray(); | ||
}); | ||
} | ||
}); | ||
|
||
describe('ISO-8601 timestamps', () => { | ||
const VALID_TIMESTAMPS = ['2022-01-01T00:00:00.000Z', '2022-01-01T00:00:00.000+02:00']; | ||
|
||
for (const timestamp of VALID_TIMESTAMPS) { | ||
test(`should allow ${timestamp}`, async () => { | ||
entity.timestamp = timestamp; | ||
await expect(validate(entity)).resolves.toBeEmptyArray(); | ||
}); | ||
} | ||
}); | ||
|
||
describe('Semver versions', () => { | ||
const VALID_VERSIONS = ['1.0.0', '1.0.0-alpha.1']; | ||
|
||
for (const version of VALID_VERSIONS) { | ||
test(`should allow ${version}`, async () => { | ||
entity.version = version; | ||
await expect(validate(entity)).resolves.toBeEmptyArray(); | ||
}); | ||
} | ||
}); | ||
}); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { ValidationOptions, ValidatorConstraintInterface } from 'class-validator'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this file be somewhere else than under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, not sure why but it has been like this since forever. Changed. |
||
import { registerDecorator, ValidatorConstraint } from 'class-validator'; | ||
import sanitizeHtml from 'sanitize-html'; | ||
|
||
const URL_REGEX = /^(https?:\/\/|www\.)/i; | ||
|
||
@ValidatorConstraint({ name: 'NoXss', async: false }) | ||
class NoXssConstraint implements ValidatorConstraintInterface { | ||
validate(value: string) { | ||
const sanitized = sanitizeHtml(value, { allowedTags: [], allowedAttributes: {} }); | ||
|
||
if (sanitized !== value) return false; | ||
|
||
return !URL_REGEX.test(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should be a separate validator. Having a URL is not a XSS. I guess for backwards compat we could then include that validator also in places that are currently using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree! Not sure why it was ever included. |
||
} | ||
|
||
defaultMessage() { | ||
return 'Potentially malicious string'; | ||
} | ||
} | ||
|
||
export function NoXss(options?: ValidationOptions) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'NoXss', | ||
target: object.constructor, | ||
propertyName, | ||
options, | ||
validator: NoXssConstraint, | ||
}); | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also
<script>alert('xss')</script>?