Skip to content

Commit

Permalink
[FIX] qweb: prevent issue with builtin object properties
Browse files Browse the repository at this point in the history
The QWeb expression parser use an object as a mapping between some
strings and the desired output in the compiled template.  However, as we
should all know, objects are not Maps, they have some additional
properties, such as "constructor" or "hasOwnProperty".

The simple solution is to make sure the mapping object does not have
anything in its prototype chain to pollute its purpose.

closes #835
  • Loading branch information
ged-odoo authored and aab-odoo committed Feb 4, 2021
1 parent 19a47a7 commit 7673541
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/qweb/expression_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const RESERVED_WORDS = "true,false,NaN,null,undefined,debugger,console,window,in
","
);

const WORD_REPLACEMENT = {
const WORD_REPLACEMENT = Object.assign(Object.create(null), {
and: "&&",
or: "||",
gt: ">",
gte: ">=",
lt: "<",
lte: "<=",
};
});

export interface QWebVar {
id: string; // foo
Expand Down Expand Up @@ -69,7 +69,7 @@ interface Token {
varName?: string;
}

const STATIC_TOKEN_MAP: { [key: string]: TKind } = {
const STATIC_TOKEN_MAP: { [key: string]: TKind } = Object.assign(Object.create(null), {
"{": "LEFT_BRACE",
"}": "RIGHT_BRACE",
"[": "LEFT_BRACKET",
Expand All @@ -78,7 +78,7 @@ const STATIC_TOKEN_MAP: { [key: string]: TKind } = {
",": "COMMA",
"(": "LEFT_PAREN",
")": "RIGHT_PAREN",
};
});

// note that the space after typeof is relevant. It makes sure that the formatted
// expression has a space after typeof
Expand Down
4 changes: 4 additions & 0 deletions tests/qweb/qweb_expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,8 @@ describe("expression evaluation", () => {
expect(compileExpr("f(...state.list)", {})).toBe("scope['f'](...scope['state'].list)");
expect(compileExpr("f([...list])", {})).toBe("scope['f']([...scope['list']])");
});

test("works with builtin properties", () => {
expect(compileExpr("state.constructor.name", {})).toBe("scope['state'].constructor.name");
});
});

0 comments on commit 7673541

Please sign in to comment.