Visitar URL original
fix(compiler): harden JIT against prototype pollution · angular/angular@35a02f8 · GitHub
Skip to content

Commit 35a02f8

Browse files
crisbetoamishne
authored andcommitted
fix(compiler): harden JIT against prototype pollution
Fixes a few places in the JIT compiler that were vulnerable against prototype pollution and code injection. (cherry picked from commit f2b1c1d)
1 parent fa21d08 commit 35a02f8

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎packages/compiler/src/output/output_jit.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ export class JitEvaluator {
6060
evaluateCode(
6161
sourceUrl: string,
6262
ctx: EmitterVisitorContext,
63-
vars: {[key: string]: any},
63+
vars: Record<string, any>,
6464
createSourceMap: boolean,
6565
): any {
6666
let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;
6767
const fnArgNames: string[] = [];
6868
const fnArgValues: any[] = [];
6969
for (const argName in vars) {
70-
fnArgValues.push(vars[argName]);
71-
fnArgNames.push(argName);
70+
if (Object.hasOwn(vars, argName)) {
71+
fnArgValues.push(vars[argName]);
72+
fnArgNames.push(argName);
73+
}
7274
}
7375
if (createSourceMap) {
7476
// using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise

‎packages/compiler/src/output/output_jit_trusted_types.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ function trustedScriptFromString(script: string): TrustedScript | string {
9595
return getPolicy()?.createScript(script) || script;
9696
}
9797

98+
const PARAM_NAME_PATTERN = /^[A-Za-z_$][\w$]*$/;
99+
98100
/**
99101
* Unsafely call the Function constructor with the given string arguments.
100102
* @security This is a security-sensitive function; any use of this function
@@ -113,9 +115,16 @@ export function newTrustedFunctionForJIT(...args: string[]): Function {
113115
// constructor. The following implements the workaround proposed on the page
114116
// below, where the Chromium bug is also referenced:
115117
// https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
116-
const fnArgs = args.slice(0, -1).join(',');
117118
const fnBody = args[args.length - 1];
118-
const body = `(function anonymous(${fnArgs}
119+
const fnParams = args.slice(0, -1);
120+
121+
for (const param of fnParams) {
122+
if (!PARAM_NAME_PATTERN.test(param)) {
123+
throw new Error(`newTrustedFunctionForJIT: invalid parameter name "${param}"`);
124+
}
125+
}
126+
127+
const body = `(function anonymous(${fnParams.join(',')}
119128
) { ${fnBody}
120129
})`;
121130

‎packages/compiler/src/parse_util.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ export function identifierName(
213213
return null;
214214
}
215215
const ref = compileIdentifier.reference;
216-
if (ref['__anonymousType']) {
216+
if (Object.hasOwn(ref, '__anonymousType')) {
217217
return ref['__anonymousType'];
218218
}
219-
if (ref['__forward_ref__']) {
219+
if (Object.hasOwn(ref, '__forward_ref__')) {
220220
// We do not want to try to stringify a `forwardRef()` function because that would cause the
221221
// inner function to be evaluated too early, defeating the whole point of the `forwardRef`.
222222
return '__forward_ref__';

0 commit comments

Comments
 (0)