Skip to content

Commit

Permalink
refactor(core): detect if codegen should quote property without using…
Browse files Browse the repository at this point in the history
… eval() (#8045)

refactor(core): detect if should quote property when codegen without using eval()
  • Loading branch information
Josh-Cena authored Sep 2, 2022
1 parent 29703da commit 46d705a
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions packages/docusaurus/src/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,9 @@ ${indent(subroutesCodeStrings.join(',\n'))}
}

Object.entries(props).forEach(([propName, propValue]) => {
// Inspired by https://github.com/armanozak/should-quote/blob/main/packages/should-quote/src/lib/should-quote.ts
const shouldQuote = ((key: string) => {
// Pre-sanitation to prevent injection
if (/[.,;:}/\s]/.test(key)) {
return true;
}
try {
// If this key can be used in an expression like ({a:0}).a
// eslint-disable-next-line no-eval
eval(`({${key}:0}).${key}`);
return false;
} catch {
return true;
}
})(propName);
// Escape quotes as well
const key = shouldQuote ? JSON.stringify(propName) : propName;
const isIdentifier =
/^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(propName);
const key = isIdentifier ? propName : JSON.stringify(propName);
parts.push(`${key}: ${JSON.stringify(propValue)}`);
});

Expand Down

0 comments on commit 46d705a

Please sign in to comment.