janja 3.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/dist/index.d.ts CHANGED
@@ -172,6 +172,7 @@ interface CompiledTemplate {
172
172
  sources: Map<string, string>;
173
173
  fingerprint: string;
174
174
  }
175
+ declare const MAX_INCLUDE_DEPTH = 100;
175
176
  declare function templateCacheKey(template: string, stream?: boolean): string;
176
177
  declare function optionsFingerprint(options: Required<CompilerOptions>): string;
177
178
  declare class Compiler extends Context {
@@ -197,6 +198,11 @@ declare class Compiler extends Context {
197
198
  get script(): Script;
198
199
  private generateCacheKey;
199
200
  private fingerprint;
201
+ /**
202
+ * Shared counters for optional security limits. Declared once on the root
203
+ * template so nested includes, macros and loops all see the same budget.
204
+ */
205
+ private pushResourceLimitCounters;
200
206
  private isFresh;
201
207
  private parseTemplates;
202
208
  private collectPartialsAndBlocks;
@@ -241,7 +247,46 @@ interface SecurityOptions {
241
247
  deepEscape?: boolean;
242
248
  cspNonce?: string;
243
249
  cspHeaders?: Record<string, string>;
244
- }
250
+ /**
251
+ * Maximum nest depth of `include` while rendering. Defaults to 100.
252
+ * Cycles that never end become an error instead of a stack overflow.
253
+ */
254
+ maxIncludeDepth?: number;
255
+ /**
256
+ * Maximum number of `for` loop iterations across one render. Off by default.
257
+ * Enable when templates may be untrusted.
258
+ */
259
+ maxLoopIterations?: number;
260
+ /**
261
+ * Maximum nest depth of macro invocations across one render. Off by default.
262
+ */
263
+ maxCallDepth?: number;
264
+ /**
265
+ * Wall-clock budget for one `render` / `renderCompiled` call, in
266
+ * milliseconds. Off by default. The running script is not aborted; the
267
+ * promise rejects when the budget is exceeded. Prefer a separate process
268
+ * for truly hostile templates.
269
+ */
270
+ timeoutMs?: number;
271
+ }
272
+ /** Defaults and resolved limits used by the compiler and renderer. */
273
+ declare function resolveSecurityLimits(security?: SecurityOptions): {
274
+ maxIncludeDepth: number;
275
+ maxLoopIterations: number | undefined;
276
+ maxCallDepth: number | undefined;
277
+ timeoutMs: number | undefined;
278
+ };
279
+ declare class SecuritySandbox {
280
+ private allowedGlobals;
281
+ private cspNonce?;
282
+ constructor(options?: SecurityOptions);
283
+ sanitizeGlobals(globals: ObjectType): ObjectType;
284
+ validateExpression(expression: string): boolean;
285
+ createSecureContext(globals: ObjectType): ObjectType;
286
+ getCSPNonce(): string | undefined;
287
+ generateCSPHeaders(): Record<string, string>;
288
+ }
289
+ declare function deepEscape(value: unknown, seen?: WeakSet<object>): string;
245
290
  interface SecurityAuditResult {
246
291
  issues: SecurityIssue[];
247
292
  severity: 'low' | 'medium' | 'high' | 'critical';
@@ -252,6 +297,10 @@ interface SecurityIssue {
252
297
  location?: string;
253
298
  severity: 'low' | 'medium' | 'high' | 'critical';
254
299
  }
300
+ declare class SecurityAuditor {
301
+ auditTemplate(template: string): SecurityAuditResult;
302
+ private calculateOverallSeverity;
303
+ }
255
304
 
256
305
  type MaybePromise<T> = T | Promise<T>;
257
306
  type ObjectType<T = any> = Record<string, T>;
@@ -599,6 +648,7 @@ declare class Renderer {
599
648
  private sandbox?;
600
649
  private filters;
601
650
  private escape;
651
+ private compiling;
602
652
  constructor(options?: RendererOptions);
603
653
  render(template: string, globals: ObjectType): Promise<string>;
604
654
  /**
@@ -616,7 +666,7 @@ declare class Renderer {
616
666
  * parts of the template and between the iterations of top-level loops.
617
667
  */
618
668
  renderStream(template: string, globals?: ObjectType): ReadableStream<string>;
619
- private createCompiler;
669
+ private compile;
620
670
  private wrapError;
621
671
  private run;
622
672
  renderFile(filepath: string, globals: ObjectType): Promise<string>;
@@ -641,6 +691,11 @@ declare class ExpCompiler {
641
691
  private compileExp;
642
692
  private compileNot;
643
693
  private compileId;
694
+ /**
695
+ * Positional arguments in the order they are written, followed by one
696
+ * object holding the named ones: `f(1, a=2, b=3)` calls `f(1, {a:2,b:3})`.
697
+ */
698
+ private compileArgs;
644
699
  private compileLit;
645
700
  private compileSeq;
646
701
  private compilePipe;
@@ -951,7 +1006,7 @@ interface FileLoaderOptions {
951
1006
  * Paths are resolved against `root`, with or without an extension, and may
952
1007
  * not lead outside of it, so that a template cannot include arbitrary files.
953
1008
  */
954
- declare function createFileLoader(root: string, { extensions }?: FileLoaderOptions): (name: string) => Promise<string>;
1009
+ declare function createFileLoader(root: string, { extensions }?: FileLoaderOptions): (name: string) => Promise<any>;
955
1010
 
956
1011
  interface ErrorRecoveryOptions {
957
1012
  /**
@@ -1021,5 +1076,5 @@ declare function renderCompiled(script: Script, data?: ObjectType, options?: Ren
1021
1076
  */
1022
1077
  declare function renderStream(template: string, data?: ObjectType, options?: RendererOptions): ReadableStream<string>;
1023
1078
 
1024
- export { CompileError, Compiler, ErrorRecovery, ExpCompiler, ExpError, ExpParser, ExpTokenizer, I18n, 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 };
1025
- export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, CacheOptions, Checker, CommentToken, CompiledTemplate, CompilerFn, CompilerMap, CompilerOptions, CompositionOptions, DirectiveExpression, DirectiveToken, ErrorContext, ErrorRecoveryOptions, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, FileLoaderOptions, Filter, Filters, FormatOptions, FormatterFn, FormatterMap, FormatterOptions, I18nOptions, IdExp, IfExp, LitExp, Loc, MaybePromise, MixinDefinition, MixinOptions, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, ParserOptions, Plugin, PluralRules, Pos, PrecompileOptions, PrecompileResult, Primitive, RendererOptions, Script, SeqExp, SourceMapOptions, SourceMapV3, Strip, SyntaxNode, TemplateAnalysis, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, TranslationDict, UnaryExpTokenType, UnaryExpType, ValidationError, ValidationResult, WhitespaceOptions };
1079
+ 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 };
1080
+ export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, CacheOptions, Checker, CommentToken, CompiledTemplate, CompilerFn, CompilerMap, CompilerOptions, CompositionOptions, DirectiveExpression, DirectiveToken, ErrorContext, ErrorRecoveryOptions, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, FileLoaderOptions, Filter, Filters, FormatOptions, FormatterFn, FormatterMap, FormatterOptions, I18nOptions, IdExp, IfExp, LitExp, Loc, MaybePromise, MixinDefinition, MixinOptions, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, ParserOptions, Plugin, PluralRules, Pos, PrecompileOptions, PrecompileResult, Primitive, RendererOptions, Script, SecurityAuditResult, SecurityIssue, SecurityOptions, SeqExp, SourceMapOptions, SourceMapV3, Strip, SyntaxNode, TemplateAnalysis, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, TranslationDict, UnaryExpTokenType, UnaryExpType, ValidationError, ValidationResult, WhitespaceOptions };