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

Skip to content
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
1 change: 1 addition & 0 deletions dev-app/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"externalDependencies": ["xhr2"],
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"polyfills": ["@angular/localize/init"],
"assets": [
{
"glob": "**/*",
Expand Down
1 change: 1 addition & 0 deletions dev-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@angular/forms": "workspace:*",
"@angular/platform-browser": "workspace:*",
"@angular/platform-server": "workspace:*",
"@angular/localize": "workspace:*",
"@angular/router": "workspace:*",
"@angular/ssr": "22.0.0-next.0",
"rxjs": "~7.8.0",
Expand Down
22 changes: 9 additions & 13 deletions packages/core/src/render3/i18n/i18n_parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export function i18nAttributesFirstPass(tView: TView, index: number, values: str
previousElementIndex,
attrName,
countBindings(updateOpCodes),
null,
URI_ATTRS[attrName.toLowerCase()] ? _sanitizeUrl : null,
);
}
}
Expand Down Expand Up @@ -810,18 +810,14 @@ function walkIcuTree(
const hasBinding = !!attr.value.match(BINDING_REGEXP);
if (hasBinding) {
if (VALID_ATTRS.hasOwnProperty(lowerAttrName)) {
if (URI_ATTRS[lowerAttrName]) {
generateBindingUpdateOpCodes(
update,
attr.value,
newIndex,
attr.name,
0,
_sanitizeUrl,
);
} else {
generateBindingUpdateOpCodes(update, attr.value, newIndex, attr.name, 0, null);
}
generateBindingUpdateOpCodes(
update,
attr.value,
newIndex,
attr.name,
0,
URI_ATTRS[lowerAttrName] ? _sanitizeUrl : null,
);
} else {
ngDevMode &&
console.warn(
Expand Down
65 changes: 65 additions & 0 deletions packages/core/test/acceptance/i18n_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3534,6 +3534,71 @@ describe('runtime i18n', () => {
'translatedText value',
);
});

describe('attribute sanitization', () => {
@Component({template: ''})
class SanitizeAppComp {
url = 'javascript:alert("oh no")';
count = 0;
}

it('should sanitize translated attribute binding', () => {
const fixture = initWithTemplate(SanitizeAppComp, '<a [attr.href]="url" i18n-href></a>');
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize translated property binding', () => {
const fixture = initWithTemplate(SanitizeAppComp, '<a [href]="url" i18n-href></a>');
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize translated interpolation', () => {
const fixture = initWithTemplate(SanitizeAppComp, '<a href="{{url}}" i18n-href></a>');
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize interpolation inside translated element', () => {
const fixture = initWithTemplate(SanitizeAppComp, `<div i18n><a href="{{url}}"></a></div>`);
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize attribute binding inside translated element', () => {
const fixture = initWithTemplate(
SanitizeAppComp,
`<div i18n><a [attr.href]="url"></a></div>`,
);
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize property binding inside translated element', () => {
const fixture = initWithTemplate(SanitizeAppComp, `<div i18n><a [href]="url"></a></div>`);
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});

it('should sanitize property binding inside an ICU', () => {
const fixture = initWithTemplate(
SanitizeAppComp,
`<div i18n>{count, plural,
=0 {no <strong>link</strong> yet}
other {{{count}} Here is the <a href="{{url}}">link</a>!}
}</div>`,
);

expect(fixture.nativeElement.querySelector('a')).toBeFalsy();

fixture.componentInstance.count = 1;
fixture.detectChanges();
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
expect(link).toBeTruthy();
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
});
});
});

function initWithTemplate(compType: Type<any>, template: string) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading