Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 417a407

Browse files
alan-agius4thePunderWoman
authored andcommitted
fix(compiler): restrict possible event handler check to property names longer than 2 characters
Previously, the compiler disallowed translation of any attribute starting with 'on' for security reasons. This incorrectly disallowed translation of the 'on' attribute itself, which is not an event handler. This commit introduces `isPossibleEventHandler` to verify that the property name has a length greater than 2 in addition to starting with 'on'. This allows attributes like 'on' to be translated while still correctly disallowing actual event handlers like 'onerror', 'onclick', etc.
1 parent 2112ede commit 417a407

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎packages/compiler/src/render3/view/i18n/meta.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class I18nMetaVisitor implements html.Visitor {
208208
isTrustedType = isTrustedTypesSink(node.name, name);
209209
}
210210

211-
if (isTrustedType || name.toLowerCase().startsWith('on')) {
211+
if (isTrustedType || isPossibleEventHandler(name)) {
212212
this._reportError(
213213
attr,
214214
`Translating attribute '${name}' is disallowed for security reasons.`,
@@ -350,3 +350,14 @@ export function i18nMetaToJSDoc(meta: I18nMeta): o.JSDocComment {
350350
}
351351
return o.jsDocComment(tags);
352352
}
353+
354+
/**
355+
* Check if the propertyName is a potential event handler.
356+
* We consider a property to be a potential event handler if its name is longer than 2 characters and starts with 'on' (e.g. 'onclick', 'onload', etc.).
357+
* @param propertyName The name of the property to check.
358+
* @returns True if the property is a potential event handler, false otherwise.
359+
*/
360+
function isPossibleEventHandler(propertyName: string): boolean {
361+
const name = propertyName.toLowerCase();
362+
return name.length > 2 && name !== 'only' && name.startsWith('on');
363+
}

‎packages/core/test/linker/security_integration_spec.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ describe('security integration tests', function () {
385385
);
386386
});
387387

388+
it('should not throw error on translating "on" attribute', () => {
389+
const template = `<div on="some-value" i18n-on></div>`;
390+
TestBed.overrideComponent(SecuredComponent, {set: {template}});
391+
392+
expect(() => TestBed.createComponent(SecuredComponent)).not.toThrow();
393+
});
394+
388395
it('should throw error on security-sensitive attributes with constant values', () => {
389396
const template = `<iframe srcdoc="foo" i18n-srcdoc></iframe>`;
390397
TestBed.overrideComponent(SecuredComponent, {set: {template}});

0 commit comments

Comments
 (0)