Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure template names are valid identifiers #3438

Merged
merged 1 commit into from
May 24, 2024
Merged
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
22 changes: 22 additions & 0 deletions packages/pug-code-gen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function toConstant(src) {
return constantinople.toConstant(src, {pug: runtime, pug_interp: undefined});
}

function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}

/**
* Initialize `Compiler` with the given `node`.
*
Expand All @@ -62,6 +66,23 @@ function Compiler(node, options) {
'The pretty parameter should either be a boolean or whitespace only string'
);
}
if (this.options.templateName && !isIdentifier(this.options.templateName)) {
throw new Error(
'The templateName parameter must be a valid JavaScript identifier if specified.'
);
}
if (
this.doctype &&
(this.doctype.includes('<') || this.doctype.includes('>'))
) {
throw new Error('Doctype can not contain "<" or ">"');
}
if (this.options.globals && !this.options.globals.every(isIdentifier)) {
throw new Error(
'The globals option must be an array of valid JavaScript identifiers if specified.'
);
}

this.debug = false !== options.compileDebug;
this.indents = 0;
this.parentIndents = 0;
Expand Down Expand Up @@ -167,6 +188,7 @@ Compiler.prototype = {
');' +
'}';
}

return (
buildRuntime(this.runtimeFunctionsUsed) +
'function ' +
Expand Down
Loading