Skip to content

Commit

Permalink
Fix regex for HTML comment tokens (#2222)
Browse files Browse the repository at this point in the history
Address code scanning alert by altering the regex for HTML comment
tokens used in the post processing step. Apparently `--!>` is a valid
way to end an HTML comment 🤯

Fixes https://github.com/MetaMask/snaps/security/code-scanning/1
  • Loading branch information
FrederikBolding authored Feb 27, 2024
1 parent 97c9fef commit 4774979
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/snaps-utils/src/post-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ describe('postProcessBundle', () => {
`);
});

it('breaks up HTML comment terminators in string literals with alternative comment end', () => {
const code = `
const foo = '<!-- bar --!>';
`;

const processedCode = postProcessBundle(code);
expect(processedCode).toMatchInlineSnapshot(`
{
"code": "const foo = "<!" + "--" + " bar " + "--" + "!" + ">";",
"sourceMap": null,
"warnings": [],
}
`);
});

it('breaks up `import()` in string literals', () => {
const code = `
const foo = 'foo bar import() baz';
Expand Down Expand Up @@ -205,6 +220,21 @@ describe('postProcessBundle', () => {
`);
});

it('breaks up HTML comment terminators in template literals with alternative comment end', () => {
const code = `
const foo = \`<!-- bar --> \${'<!-- baz --!>'} \${qux}\`;
`;

const processedCode = postProcessBundle(code);
expect(processedCode).toMatchInlineSnapshot(`
{
"code": "const foo = \`\${"<!"}\${"--"} bar \${"--"}\${">"} \${"<!" + "--" + " baz " + "--" + "!" + ">"} \${qux}\`;",
"sourceMap": null,
"warnings": [],
}
`);
});

it('breaks up `import()` in template literals', () => {
const code = `
const foo = \`foo bar import() baz\`;
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/src/post-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export enum PostProcessWarning {
// The RegEx below consists of multiple groups joined by a boolean OR.
// Each part consists of two groups which capture a part of each string
// which needs to be split up, e.g., `<!--` is split into `<!` and `--`.
const TOKEN_REGEX = /(<!)(--)|(--)(>)|(import)(\(.*?\))/gu;
const TOKEN_REGEX = /(<!)(--)|(--)(>)|(--)(!)(>)|(import)(\(.*?\))/gu;

// An empty template element, i.e., a part of a template literal without any
// value ("").
Expand Down

0 comments on commit 4774979

Please sign in to comment.