feat(security): add tests for style sanitisation.
This commit is contained in:
parent
99c0d503d7
commit
7b6c4d5acc
|
@ -37,7 +37,12 @@ function hasBalancedQuotes(value: string) {
|
||||||
return outsideSingle && outsideDouble;
|
return outsideSingle && outsideDouble;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitizes the given untrusted CSS style property value (i.e. not an entire object, just a single
|
||||||
|
* value) and returns a value that is safe to use in a browser environment.
|
||||||
|
*/
|
||||||
export function sanitizeStyle(value: string): string {
|
export function sanitizeStyle(value: string): string {
|
||||||
if (String(value).match(SAFE_STYLE_VALUE) && hasBalancedQuotes(value)) return value;
|
value = String(value); // Make sure it's actually a string.
|
||||||
|
if (value.match(SAFE_STYLE_VALUE) && hasBalancedQuotes(value)) return value;
|
||||||
return 'unsafe';
|
return 'unsafe';
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
import * as t from '@angular/core/testing/testing_internal';
|
||||||
|
import {sanitizeStyle} from '../../src/security/style_sanitizer';
|
||||||
|
|
||||||
|
export function main() {
|
||||||
|
t.describe('Style sanitizer', () => {
|
||||||
|
t.it('sanitizes values', () => {
|
||||||
|
t.expect(sanitizeStyle('abc')).toEqual('abc');
|
||||||
|
t.expect(sanitizeStyle('expression(haha)')).toEqual('unsafe');
|
||||||
|
// Unbalanced quotes.
|
||||||
|
t.expect(sanitizeStyle('"value" "')).toEqual('unsafe');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue