Skip to content

Commit 32acfe8

Browse files
fix: ensure template names are valid identifiers (#3438)
1 parent 4767caf commit 32acfe8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/pug-code-gen/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function toConstant(src) {
3939
return constantinople.toConstant(src, {pug: runtime, pug_interp: undefined});
4040
}
4141

42+
function isIdentifier(name) {
43+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
44+
}
45+
4246
/**
4347
* Initialize `Compiler` with the given `node`.
4448
*
@@ -62,6 +66,23 @@ function Compiler(node, options) {
6266
'The pretty parameter should either be a boolean or whitespace only string'
6367
);
6468
}
69+
if (this.options.templateName && !isIdentifier(this.options.templateName)) {
70+
throw new Error(
71+
'The templateName parameter must be a valid JavaScript identifier if specified.'
72+
);
73+
}
74+
if (
75+
this.doctype &&
76+
(this.doctype.includes('<') || this.doctype.includes('>'))
77+
) {
78+
throw new Error('Doctype can not contain "<" or ">"');
79+
}
80+
if (this.options.globals && !this.options.globals.every(isIdentifier)) {
81+
throw new Error(
82+
'The globals option must be an array of valid JavaScript identifiers if specified.'
83+
);
84+
}
85+
6586
this.debug = false !== options.compileDebug;
6687
this.indents = 0;
6788
this.parentIndents = 0;
@@ -167,6 +188,7 @@ Compiler.prototype = {
167188
');' +
168189
'}';
169190
}
191+
170192
return (
171193
buildRuntime(this.runtimeFunctionsUsed) +
172194
'function ' +

0 commit comments

Comments
 (0)