janja 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.cjs +245 -180
- package/dist/index.d.ts +52 -4
- package/dist/index.esm.js +242 -181
- package/dist/index.js +245 -180
- package/dist/index.min.js +1 -1
- package/package.json +10 -10
package/dist/index.esm.js
CHANGED
|
@@ -1577,6 +1577,189 @@ class Parser extends Tokenizer {
|
|
|
1577
1577
|
}
|
|
1578
1578
|
}
|
|
1579
1579
|
|
|
1580
|
+
const special = /[&<>"']/;
|
|
1581
|
+
const specials = /[&<>"']/g;
|
|
1582
|
+
const entities = {
|
|
1583
|
+
'&': '&',
|
|
1584
|
+
'"': '"',
|
|
1585
|
+
"'": ''',
|
|
1586
|
+
'<': '<',
|
|
1587
|
+
'>': '>',
|
|
1588
|
+
};
|
|
1589
|
+
const entity = (char) => entities[char];
|
|
1590
|
+
function escape(v) {
|
|
1591
|
+
const str = typeof v === 'string' ? v : `${v}`;
|
|
1592
|
+
// Most values contain nothing to escape; they are returned as they are,
|
|
1593
|
+
// without allocating
|
|
1594
|
+
return special.test(str) ? str.replace(specials, entity) : str;
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
class Safe extends String {
|
|
1598
|
+
constructor(value) {
|
|
1599
|
+
super(`${value}`);
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
/** Defaults and resolved limits used by the compiler and renderer. */
|
|
1604
|
+
function resolveSecurityLimits(security = {}) {
|
|
1605
|
+
return {
|
|
1606
|
+
maxIncludeDepth: security.maxIncludeDepth ?? 100,
|
|
1607
|
+
maxLoopIterations: security.maxLoopIterations,
|
|
1608
|
+
maxCallDepth: security.maxCallDepth,
|
|
1609
|
+
timeoutMs: security.timeoutMs,
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
class SecuritySandbox {
|
|
1613
|
+
constructor(options = {}) {
|
|
1614
|
+
Object.defineProperty(this, "allowedGlobals", {
|
|
1615
|
+
enumerable: true,
|
|
1616
|
+
configurable: true,
|
|
1617
|
+
writable: true,
|
|
1618
|
+
value: void 0
|
|
1619
|
+
});
|
|
1620
|
+
Object.defineProperty(this, "cspNonce", {
|
|
1621
|
+
enumerable: true,
|
|
1622
|
+
configurable: true,
|
|
1623
|
+
writable: true,
|
|
1624
|
+
value: void 0
|
|
1625
|
+
});
|
|
1626
|
+
this.allowedGlobals = new Set(options.allowedGlobals || []);
|
|
1627
|
+
this.cspNonce = options.cspNonce;
|
|
1628
|
+
}
|
|
1629
|
+
sanitizeGlobals(globals) {
|
|
1630
|
+
const sanitized = {};
|
|
1631
|
+
for (const [key, value] of Object.entries(globals)) {
|
|
1632
|
+
if (this.allowedGlobals.has(key)) {
|
|
1633
|
+
sanitized[key] = value;
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
return sanitized;
|
|
1637
|
+
}
|
|
1638
|
+
validateExpression(expression) {
|
|
1639
|
+
// Prevent access to dangerous globals
|
|
1640
|
+
const dangerousPatterns = [
|
|
1641
|
+
/\bprocess\b/,
|
|
1642
|
+
/\brequire\b/,
|
|
1643
|
+
/\bimport\b/,
|
|
1644
|
+
/\bexport\b/,
|
|
1645
|
+
/\beval\b/,
|
|
1646
|
+
/\bFunction\b/,
|
|
1647
|
+
/\b__proto__\b/,
|
|
1648
|
+
/\bconstructor\b/,
|
|
1649
|
+
];
|
|
1650
|
+
for (const pattern of dangerousPatterns) {
|
|
1651
|
+
if (pattern.test(expression)) {
|
|
1652
|
+
return false;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
return true;
|
|
1656
|
+
}
|
|
1657
|
+
createSecureContext(globals) {
|
|
1658
|
+
const sanitized = this.sanitizeGlobals(globals);
|
|
1659
|
+
// Add safe utility functions
|
|
1660
|
+
return {
|
|
1661
|
+
...sanitized,
|
|
1662
|
+
Math,
|
|
1663
|
+
Date,
|
|
1664
|
+
JSON,
|
|
1665
|
+
// Add other safe built-ins as needed
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
getCSPNonce() {
|
|
1669
|
+
return this.cspNonce;
|
|
1670
|
+
}
|
|
1671
|
+
generateCSPHeaders() {
|
|
1672
|
+
const headers = {};
|
|
1673
|
+
if (this.cspNonce) {
|
|
1674
|
+
headers['Content-Security-Policy'] =
|
|
1675
|
+
`script-src 'nonce-${this.cspNonce}'`;
|
|
1676
|
+
}
|
|
1677
|
+
return headers;
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
function deepEscape(value, seen = new WeakSet()) {
|
|
1681
|
+
if (value === null || value === undefined) {
|
|
1682
|
+
return '';
|
|
1683
|
+
}
|
|
1684
|
+
if (value instanceof Safe) {
|
|
1685
|
+
return `${value}`;
|
|
1686
|
+
}
|
|
1687
|
+
if (typeof value === 'object') {
|
|
1688
|
+
// Guard against circular references
|
|
1689
|
+
if (seen.has(value)) {
|
|
1690
|
+
return '';
|
|
1691
|
+
}
|
|
1692
|
+
seen.add(value);
|
|
1693
|
+
const values = Array.isArray(value)
|
|
1694
|
+
? value
|
|
1695
|
+
: Object.values(value);
|
|
1696
|
+
return values.map((v) => deepEscape(v, seen)).join('');
|
|
1697
|
+
}
|
|
1698
|
+
return escape(value);
|
|
1699
|
+
}
|
|
1700
|
+
class SecurityAuditor {
|
|
1701
|
+
auditTemplate(template) {
|
|
1702
|
+
const issues = [];
|
|
1703
|
+
// Check for dangerous patterns
|
|
1704
|
+
const dangerousPatterns = [
|
|
1705
|
+
{
|
|
1706
|
+
pattern: /\beval\s*\(/,
|
|
1707
|
+
type: 'dangerous-eval',
|
|
1708
|
+
message: 'Use of eval() is dangerous and should be avoided',
|
|
1709
|
+
severity: 'critical',
|
|
1710
|
+
},
|
|
1711
|
+
{
|
|
1712
|
+
pattern: /\bFunction\s*\(/,
|
|
1713
|
+
type: 'dangerous-function',
|
|
1714
|
+
message: 'Use of Function constructor is dangerous',
|
|
1715
|
+
severity: 'critical',
|
|
1716
|
+
},
|
|
1717
|
+
{
|
|
1718
|
+
pattern: /\brequire\s*\(/,
|
|
1719
|
+
type: 'require-usage',
|
|
1720
|
+
message: 'Direct require() usage in templates is unsafe',
|
|
1721
|
+
severity: 'high',
|
|
1722
|
+
},
|
|
1723
|
+
{
|
|
1724
|
+
pattern: /\bimport\s*\(/,
|
|
1725
|
+
type: 'dynamic-import',
|
|
1726
|
+
message: 'Dynamic import() usage in templates may be unsafe',
|
|
1727
|
+
severity: 'high',
|
|
1728
|
+
},
|
|
1729
|
+
];
|
|
1730
|
+
for (const { pattern, type, message, severity } of dangerousPatterns) {
|
|
1731
|
+
const match = template.match(pattern);
|
|
1732
|
+
if (match) {
|
|
1733
|
+
issues.push({
|
|
1734
|
+
type,
|
|
1735
|
+
message,
|
|
1736
|
+
location: `at index ${match.index}`,
|
|
1737
|
+
severity,
|
|
1738
|
+
});
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
// Calculate overall severity
|
|
1742
|
+
const overallSeverity = this.calculateOverallSeverity(issues);
|
|
1743
|
+
return {
|
|
1744
|
+
issues,
|
|
1745
|
+
severity: overallSeverity,
|
|
1746
|
+
};
|
|
1747
|
+
}
|
|
1748
|
+
calculateOverallSeverity(issues) {
|
|
1749
|
+
if (issues.length === 0) {
|
|
1750
|
+
return 'low';
|
|
1751
|
+
}
|
|
1752
|
+
const severityMap = {
|
|
1753
|
+
critical: 4,
|
|
1754
|
+
high: 3,
|
|
1755
|
+
medium: 2,
|
|
1756
|
+
low: 1,
|
|
1757
|
+
};
|
|
1758
|
+
const maxSeverity = Math.max(...issues.map((issue) => severityMap[issue.severity]));
|
|
1759
|
+
return Object.entries(severityMap).find(([, value]) => value === maxSeverity)[0];
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1580
1763
|
const fingerprints = new WeakMap();
|
|
1581
1764
|
// How deep includes may nest while rendering. Only templates that include
|
|
1582
1765
|
// themselves get anywhere near it; the limit turns a cycle that never ends
|
|
@@ -1593,6 +1776,8 @@ function optionsFingerprint(options) {
|
|
|
1593
1776
|
let fingerprint = fingerprints.get(options);
|
|
1594
1777
|
if (fingerprint === undefined) {
|
|
1595
1778
|
const { trimWhitespace, stripComments, commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = options;
|
|
1779
|
+
// Limits that change the generated code must bust the compile cache
|
|
1780
|
+
const { maxIncludeDepth, maxLoopIterations, maxCallDepth } = resolveSecurityLimits(options.security);
|
|
1596
1781
|
fingerprint = JSON.stringify([
|
|
1597
1782
|
trimWhitespace,
|
|
1598
1783
|
stripComments,
|
|
@@ -1602,6 +1787,9 @@ function optionsFingerprint(options) {
|
|
|
1602
1787
|
directiveClose,
|
|
1603
1788
|
outputOpen,
|
|
1604
1789
|
outputClose,
|
|
1790
|
+
maxIncludeDepth,
|
|
1791
|
+
maxLoopIterations ?? null,
|
|
1792
|
+
maxCallDepth ?? null,
|
|
1605
1793
|
]);
|
|
1606
1794
|
fingerprints.set(options, fingerprint);
|
|
1607
1795
|
}
|
|
@@ -1719,12 +1907,16 @@ class Compiler extends Context {
|
|
|
1719
1907
|
this.rootNode = new Parser(this.options).parse(template);
|
|
1720
1908
|
this.state = {};
|
|
1721
1909
|
this.start(this.isRoot);
|
|
1910
|
+
if (this.isRoot) {
|
|
1911
|
+
this.pushResourceLimitCounters();
|
|
1912
|
+
}
|
|
1722
1913
|
await this.parseTemplates();
|
|
1723
1914
|
if (this.isRoot) {
|
|
1724
1915
|
if (this.partials.size) {
|
|
1916
|
+
const { maxIncludeDepth } = resolveSecurityLimits(this.options.security);
|
|
1725
1917
|
this.pushRaw(null, 'let d_=0;', 'const p={');
|
|
1726
1918
|
for (const [path, code] of this.partials) {
|
|
1727
|
-
this.pushRaw(null, `${JSON.stringify(path)}:async()=>{`, `if(d_>=${
|
|
1919
|
+
this.pushRaw(null, `${JSON.stringify(path)}:async()=>{`, `if(d_>=${maxIncludeDepth})throw new Error(${JSON.stringify(`Includes of ${JSON.stringify(path)} are nested too deeply`)});`, 'd_++;', 'try{return await(async()=>{', code, '})();}finally{d_--;}', '},');
|
|
1728
1920
|
}
|
|
1729
1921
|
this.pushRaw(null, '};');
|
|
1730
1922
|
}
|
|
@@ -1773,6 +1965,19 @@ class Compiler extends Context {
|
|
|
1773
1965
|
fingerprint() {
|
|
1774
1966
|
return optionsFingerprint(this.options);
|
|
1775
1967
|
}
|
|
1968
|
+
/**
|
|
1969
|
+
* Shared counters for optional security limits. Declared once on the root
|
|
1970
|
+
* template so nested includes, macros and loops all see the same budget.
|
|
1971
|
+
*/
|
|
1972
|
+
pushResourceLimitCounters() {
|
|
1973
|
+
const { maxLoopIterations, maxCallDepth } = resolveSecurityLimits(this.options.security);
|
|
1974
|
+
if (maxLoopIterations != null) {
|
|
1975
|
+
this.pushRaw(null, 'let li_=0;');
|
|
1976
|
+
}
|
|
1977
|
+
if (maxCallDepth != null) {
|
|
1978
|
+
this.pushRaw(null, 'let cd_=0;');
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1776
1981
|
async isFresh({ sources }) {
|
|
1777
1982
|
if (!this.revalidate) {
|
|
1778
1983
|
return true;
|
|
@@ -1894,29 +2099,6 @@ class Compiler extends Context {
|
|
|
1894
2099
|
}
|
|
1895
2100
|
}
|
|
1896
2101
|
|
|
1897
|
-
const special = /[&<>"']/;
|
|
1898
|
-
const specials = /[&<>"']/g;
|
|
1899
|
-
const entities = {
|
|
1900
|
-
'&': '&',
|
|
1901
|
-
'"': '"',
|
|
1902
|
-
"'": ''',
|
|
1903
|
-
'<': '<',
|
|
1904
|
-
'>': '>',
|
|
1905
|
-
};
|
|
1906
|
-
const entity = (char) => entities[char];
|
|
1907
|
-
function escape(v) {
|
|
1908
|
-
const str = typeof v === 'string' ? v : `${v}`;
|
|
1909
|
-
// Most values contain nothing to escape; they are returned as they are,
|
|
1910
|
-
// without allocating
|
|
1911
|
-
return special.test(str) ? str.replace(specials, entity) : str;
|
|
1912
|
-
}
|
|
1913
|
-
|
|
1914
|
-
class Safe extends String {
|
|
1915
|
-
constructor(value) {
|
|
1916
|
-
super(`${value}`);
|
|
1917
|
-
}
|
|
1918
|
-
}
|
|
1919
|
-
|
|
1920
2102
|
function abs(value = 0) {
|
|
1921
2103
|
return Math.abs(value);
|
|
1922
2104
|
}
|
|
@@ -2847,13 +3029,19 @@ async function compileFor({ loop: { left, right, loc }, body, alternative }, com
|
|
|
2847
3029
|
: [[left.value, 'v_']];
|
|
2848
3030
|
const scope = compiler.in();
|
|
2849
3031
|
compiler.pushRaw(loc, 'let i_=0;');
|
|
3032
|
+
const { maxLoopIterations } = resolveSecurityLimits(compiler.options.security);
|
|
3033
|
+
const loopGuard = maxLoopIterations == null
|
|
3034
|
+
? []
|
|
3035
|
+
: [
|
|
3036
|
+
`if(++li_>${maxLoopIterations})throw new Error(${JSON.stringify(`Loop iteration limit of ${maxLoopIterations} exceeded`)});`,
|
|
3037
|
+
];
|
|
2850
3038
|
if (isReadOnly(body)) {
|
|
2851
3039
|
// Nothing in the body can modify the scope, so a single one is shared by
|
|
2852
3040
|
// all iterations instead of copying the outer context for each of them
|
|
2853
|
-
compiler.pushRaw(loc, `const ${scope}={...${context}};`, 'for(const v_ of a_){', `${scope}.loop={${loop.slice(6)};`, ...variables.map(([name, value]) => `${scope}.${name}=${value};`));
|
|
3041
|
+
compiler.pushRaw(loc, `const ${scope}={...${context}};`, 'for(const v_ of a_){', ...loopGuard, `${scope}.loop={${loop.slice(6)};`, ...variables.map(([name, value]) => `${scope}.${name}=${value};`));
|
|
2854
3042
|
}
|
|
2855
3043
|
else {
|
|
2856
|
-
compiler.pushRaw(loc, 'for(const v_ of a_){', `const ${scope}={`, `...${context},`, `${loop},`, ...variables.map(([name, value]) => `${name}:${value},`), '};');
|
|
3044
|
+
compiler.pushRaw(loc, 'for(const v_ of a_){', ...loopGuard, `const ${scope}={`, `...${context},`, `${loop},`, ...variables.map(([name, value]) => `${name}:${value},`), '};');
|
|
2857
3045
|
}
|
|
2858
3046
|
await compiler.compileNodes(body);
|
|
2859
3047
|
compiler.out();
|
|
@@ -3368,15 +3556,22 @@ const plugin$7 = {
|
|
|
3368
3556
|
async function compileMacro({ val: { value, args }, loc, body }, compiler) {
|
|
3369
3557
|
const { context } = compiler;
|
|
3370
3558
|
const expCompiler = new ExpCompiler();
|
|
3559
|
+
const { maxCallDepth } = resolveSecurityLimits(compiler.options.security);
|
|
3371
3560
|
compiler.pushRaw(loc,
|
|
3372
3561
|
// A macro collects its own output and returns it, so that it ends up
|
|
3373
3562
|
// where the macro is called rather than where it was defined
|
|
3374
|
-
`${context}.${value}=(...a_)=>async(_c)=>{`,
|
|
3563
|
+
`${context}.${value}=(...a_)=>async(_c)=>{`, ...(maxCallDepth == null
|
|
3564
|
+
? []
|
|
3565
|
+
: [
|
|
3566
|
+
`if(cd_>=${maxCallDepth})throw new Error(${JSON.stringify(`Macro call depth limit of ${maxCallDepth} exceeded`)});`,
|
|
3567
|
+
'cd_++;',
|
|
3568
|
+
'try{',
|
|
3569
|
+
]), 'let s="";', `const ${compiler.in()}={`, `...${context},`, ...(args ?? []).map((el, i) => el.type === 'ASSIGN'
|
|
3375
3570
|
? `${el.left.value}:a_[${i}]===undefined?${expCompiler.compile(el.right)}:a_[${i}],`
|
|
3376
3571
|
: `${el.value}:a_[${i}],`), '};');
|
|
3377
3572
|
await compiler.compileNodes(body);
|
|
3378
3573
|
compiler.out();
|
|
3379
|
-
compiler.pushRaw(null, 'return s;', '};');
|
|
3574
|
+
compiler.pushRaw(null, 'return s;', ...(maxCallDepth == null ? [] : ['}finally{cd_--;}']), '};');
|
|
3380
3575
|
}
|
|
3381
3576
|
function compileCaller({ loc }, compiler) {
|
|
3382
3577
|
compiler.pushRaw(loc, 's+=await _c?.()??"";');
|
|
@@ -3908,157 +4103,6 @@ class RenderError extends CompileError {
|
|
|
3908
4103
|
}
|
|
3909
4104
|
}
|
|
3910
4105
|
|
|
3911
|
-
class SecuritySandbox {
|
|
3912
|
-
constructor(options = {}) {
|
|
3913
|
-
Object.defineProperty(this, "allowedGlobals", {
|
|
3914
|
-
enumerable: true,
|
|
3915
|
-
configurable: true,
|
|
3916
|
-
writable: true,
|
|
3917
|
-
value: void 0
|
|
3918
|
-
});
|
|
3919
|
-
Object.defineProperty(this, "cspNonce", {
|
|
3920
|
-
enumerable: true,
|
|
3921
|
-
configurable: true,
|
|
3922
|
-
writable: true,
|
|
3923
|
-
value: void 0
|
|
3924
|
-
});
|
|
3925
|
-
this.allowedGlobals = new Set(options.allowedGlobals || []);
|
|
3926
|
-
this.cspNonce = options.cspNonce;
|
|
3927
|
-
}
|
|
3928
|
-
sanitizeGlobals(globals) {
|
|
3929
|
-
const sanitized = {};
|
|
3930
|
-
for (const [key, value] of Object.entries(globals)) {
|
|
3931
|
-
if (this.allowedGlobals.has(key)) {
|
|
3932
|
-
sanitized[key] = value;
|
|
3933
|
-
}
|
|
3934
|
-
}
|
|
3935
|
-
return sanitized;
|
|
3936
|
-
}
|
|
3937
|
-
validateExpression(expression) {
|
|
3938
|
-
// Prevent access to dangerous globals
|
|
3939
|
-
const dangerousPatterns = [
|
|
3940
|
-
/\bprocess\b/,
|
|
3941
|
-
/\brequire\b/,
|
|
3942
|
-
/\bimport\b/,
|
|
3943
|
-
/\bexport\b/,
|
|
3944
|
-
/\beval\b/,
|
|
3945
|
-
/\bFunction\b/,
|
|
3946
|
-
/\b__proto__\b/,
|
|
3947
|
-
/\bconstructor\b/,
|
|
3948
|
-
];
|
|
3949
|
-
for (const pattern of dangerousPatterns) {
|
|
3950
|
-
if (pattern.test(expression)) {
|
|
3951
|
-
return false;
|
|
3952
|
-
}
|
|
3953
|
-
}
|
|
3954
|
-
return true;
|
|
3955
|
-
}
|
|
3956
|
-
createSecureContext(globals) {
|
|
3957
|
-
const sanitized = this.sanitizeGlobals(globals);
|
|
3958
|
-
// Add safe utility functions
|
|
3959
|
-
return {
|
|
3960
|
-
...sanitized,
|
|
3961
|
-
Math,
|
|
3962
|
-
Date,
|
|
3963
|
-
JSON,
|
|
3964
|
-
// Add other safe built-ins as needed
|
|
3965
|
-
};
|
|
3966
|
-
}
|
|
3967
|
-
getCSPNonce() {
|
|
3968
|
-
return this.cspNonce;
|
|
3969
|
-
}
|
|
3970
|
-
generateCSPHeaders() {
|
|
3971
|
-
const headers = {};
|
|
3972
|
-
if (this.cspNonce) {
|
|
3973
|
-
headers['Content-Security-Policy'] =
|
|
3974
|
-
`script-src 'nonce-${this.cspNonce}'`;
|
|
3975
|
-
}
|
|
3976
|
-
return headers;
|
|
3977
|
-
}
|
|
3978
|
-
}
|
|
3979
|
-
function deepEscape(value, seen = new WeakSet()) {
|
|
3980
|
-
if (value === null || value === undefined) {
|
|
3981
|
-
return '';
|
|
3982
|
-
}
|
|
3983
|
-
if (value instanceof Safe) {
|
|
3984
|
-
return `${value}`;
|
|
3985
|
-
}
|
|
3986
|
-
if (typeof value === 'object') {
|
|
3987
|
-
// Guard against circular references
|
|
3988
|
-
if (seen.has(value)) {
|
|
3989
|
-
return '';
|
|
3990
|
-
}
|
|
3991
|
-
seen.add(value);
|
|
3992
|
-
const values = Array.isArray(value)
|
|
3993
|
-
? value
|
|
3994
|
-
: Object.values(value);
|
|
3995
|
-
return values.map((v) => deepEscape(v, seen)).join('');
|
|
3996
|
-
}
|
|
3997
|
-
return escape(value);
|
|
3998
|
-
}
|
|
3999
|
-
class SecurityAuditor {
|
|
4000
|
-
auditTemplate(template) {
|
|
4001
|
-
const issues = [];
|
|
4002
|
-
// Check for dangerous patterns
|
|
4003
|
-
const dangerousPatterns = [
|
|
4004
|
-
{
|
|
4005
|
-
pattern: /\beval\s*\(/,
|
|
4006
|
-
type: 'dangerous-eval',
|
|
4007
|
-
message: 'Use of eval() is dangerous and should be avoided',
|
|
4008
|
-
severity: 'critical',
|
|
4009
|
-
},
|
|
4010
|
-
{
|
|
4011
|
-
pattern: /\bFunction\s*\(/,
|
|
4012
|
-
type: 'dangerous-function',
|
|
4013
|
-
message: 'Use of Function constructor is dangerous',
|
|
4014
|
-
severity: 'critical',
|
|
4015
|
-
},
|
|
4016
|
-
{
|
|
4017
|
-
pattern: /\brequire\s*\(/,
|
|
4018
|
-
type: 'require-usage',
|
|
4019
|
-
message: 'Direct require() usage in templates is unsafe',
|
|
4020
|
-
severity: 'high',
|
|
4021
|
-
},
|
|
4022
|
-
{
|
|
4023
|
-
pattern: /\bimport\s*\(/,
|
|
4024
|
-
type: 'dynamic-import',
|
|
4025
|
-
message: 'Dynamic import() usage in templates may be unsafe',
|
|
4026
|
-
severity: 'high',
|
|
4027
|
-
},
|
|
4028
|
-
];
|
|
4029
|
-
for (const { pattern, type, message, severity } of dangerousPatterns) {
|
|
4030
|
-
const match = template.match(pattern);
|
|
4031
|
-
if (match) {
|
|
4032
|
-
issues.push({
|
|
4033
|
-
type,
|
|
4034
|
-
message,
|
|
4035
|
-
location: `at index ${match.index}`,
|
|
4036
|
-
severity,
|
|
4037
|
-
});
|
|
4038
|
-
}
|
|
4039
|
-
}
|
|
4040
|
-
// Calculate overall severity
|
|
4041
|
-
const overallSeverity = this.calculateOverallSeverity(issues);
|
|
4042
|
-
return {
|
|
4043
|
-
issues,
|
|
4044
|
-
severity: overallSeverity,
|
|
4045
|
-
};
|
|
4046
|
-
}
|
|
4047
|
-
calculateOverallSeverity(issues) {
|
|
4048
|
-
if (issues.length === 0) {
|
|
4049
|
-
return 'low';
|
|
4050
|
-
}
|
|
4051
|
-
const severityMap = {
|
|
4052
|
-
critical: 4,
|
|
4053
|
-
high: 3,
|
|
4054
|
-
medium: 2,
|
|
4055
|
-
low: 1,
|
|
4056
|
-
};
|
|
4057
|
-
const maxSeverity = Math.max(...issues.map((issue) => severityMap[issue.severity]));
|
|
4058
|
-
return Object.entries(severityMap).find(([, value]) => value === maxSeverity)[0];
|
|
4059
|
-
}
|
|
4060
|
-
}
|
|
4061
|
-
|
|
4062
4106
|
// Thrown inside a template to stop rendering a stream nobody reads anymore
|
|
4063
4107
|
const cancellation = new Error('Stream cancelled');
|
|
4064
4108
|
class Renderer {
|
|
@@ -4272,7 +4316,24 @@ class Renderer {
|
|
|
4272
4316
|
...globals,
|
|
4273
4317
|
}
|
|
4274
4318
|
: { ...this.options.globals, ...globals };
|
|
4275
|
-
|
|
4319
|
+
const result = script(context, this.escape, this.filters, write);
|
|
4320
|
+
const { timeoutMs } = this.options.security ?? {};
|
|
4321
|
+
if (timeoutMs == null) {
|
|
4322
|
+
return result;
|
|
4323
|
+
}
|
|
4324
|
+
// The script keeps running after the budget; only the promise rejects.
|
|
4325
|
+
// Use a separate process when templates must be forcibly stopped.
|
|
4326
|
+
let timer;
|
|
4327
|
+
return Promise.race([
|
|
4328
|
+
Promise.resolve(result).finally(() => {
|
|
4329
|
+
clearTimeout(timer);
|
|
4330
|
+
}),
|
|
4331
|
+
new Promise((_, reject) => {
|
|
4332
|
+
timer = setTimeout(() => {
|
|
4333
|
+
reject(new Error(`Render timed out after ${timeoutMs}ms`));
|
|
4334
|
+
}, timeoutMs);
|
|
4335
|
+
}),
|
|
4336
|
+
]);
|
|
4276
4337
|
}
|
|
4277
4338
|
async renderFile(filepath, globals) {
|
|
4278
4339
|
return this.render(await this.options.loader(filepath), globals);
|
|
@@ -5472,4 +5533,4 @@ function renderStream(template, data = {}, options) {
|
|
|
5472
5533
|
return getRenderer(options).renderStream(template, data);
|
|
5473
5534
|
}
|
|
5474
5535
|
|
|
5475
|
-
export { CompileError, Compiler, ErrorRecovery, ExpCompiler, ExpError, ExpParser, ExpTokenizer, I18n, MAX_INCLUDE_DEPTH, MixinRegistry, Parser, RenderError, Renderer, RootNode, Safe, TemplatePipeline, TemplateValidator, TokenType, Tokenizer, Traversal, analyzeTemplate, applyMixin, applyMixins, applyWhitespaceControl, baseOptions, compilerOptions, composeLayout, composeTemplates, composeTemplatesWithData, composeWithSlots, createErrorRecovery, createFileLoader, createMixin, defaultErrorHandler, defaultWhitespaceOptions, defineFilters, definePlugin, expTokenTypes, extendTemplateWithMixin, filters, formatTemplate, generateDeclarations, generateSourceMap, getWhitespaceCompilerOptions, getWhitespaceRendererOptions, globalMixinRegistry, i18n, isForbiddenIdentifier, mergeOptions, mergeWhitespaceOptions, optionsFingerprint, parserOptions, precompile, precompileTemplate, render, renderCompiled, renderFile, renderOptions, renderStream, templateCacheKey, validateTemplate };
|
|
5536
|
+
export { CompileError, Compiler, ErrorRecovery, ExpCompiler, ExpError, ExpParser, ExpTokenizer, I18n, MAX_INCLUDE_DEPTH, MixinRegistry, Parser, RenderError, Renderer, RootNode, Safe, SecurityAuditor, SecuritySandbox, TemplatePipeline, TemplateValidator, TokenType, Tokenizer, Traversal, analyzeTemplate, applyMixin, applyMixins, applyWhitespaceControl, baseOptions, compilerOptions, composeLayout, composeTemplates, composeTemplatesWithData, composeWithSlots, createErrorRecovery, createFileLoader, createMixin, deepEscape, defaultErrorHandler, defaultWhitespaceOptions, defineFilters, definePlugin, expTokenTypes, extendTemplateWithMixin, filters, formatTemplate, generateDeclarations, generateSourceMap, getWhitespaceCompilerOptions, getWhitespaceRendererOptions, globalMixinRegistry, i18n, isForbiddenIdentifier, mergeOptions, mergeWhitespaceOptions, optionsFingerprint, parserOptions, precompile, precompileTemplate, render, renderCompiled, renderFile, renderOptions, renderStream, resolveSecurityLimits, templateCacheKey, validateTemplate };
|