@@ -217,6 +217,10 @@ export class DefaultSecurityChecker implements SecurityChecker {
217217 private policy : RuntimeSecurityPolicy = getSecurityProfilePolicy (
218218 DEFAULT_SECURITY_PROFILE ,
219219 ) ;
220+ private sourceBannedPatterns : Array < {
221+ raw : string ;
222+ regex : RegExp ;
223+ } > = compileSourceBannedPatterns ( this . policy . sourceBannedPatternStrings ) ;
220224 private profile : RuntimeSecurityProfile = DEFAULT_SECURITY_PROFILE ;
221225
222226 initialize ( input ?: SecurityInitializationInput ) : void {
@@ -243,6 +247,9 @@ export class DefaultSecurityChecker implements SecurityChecker {
243247 normalized . overrides ?. sourceBannedPatternStrings ??
244248 basePolicy . sourceBannedPatternStrings ,
245249 } ;
250+ this . sourceBannedPatterns = compileSourceBannedPatterns (
251+ this . policy . sourceBannedPatternStrings ,
252+ ) ;
246253 this . profile = profile ;
247254 }
248255
@@ -598,16 +605,9 @@ export class DefaultSecurityChecker implements SecurityChecker {
598605 issues . push ( "Runtime source dynamic import() is disabled by policy" ) ;
599606 }
600607
601- for ( const patternText of this . policy . sourceBannedPatternStrings ) {
602- let pattern : RegExp ;
603- try {
604- pattern = new RegExp ( patternText , "i" ) ;
605- } catch {
606- continue ;
607- }
608-
609- if ( pattern . test ( source . code ) ) {
610- issues . push ( `Runtime source contains blocked pattern: ${ patternText } ` ) ;
608+ for ( const pattern of this . sourceBannedPatterns ) {
609+ if ( pattern . regex . test ( source . code ) ) {
610+ issues . push ( `Runtime source contains blocked pattern: ${ pattern . raw } ` ) ;
611611 }
612612 }
613613
@@ -818,6 +818,27 @@ function clonePolicy(policy: RuntimeSecurityPolicy): RuntimeSecurityPolicy {
818818 } ;
819819}
820820
821+ function compileSourceBannedPatterns ( patterns : string [ ] ) : Array < {
822+ raw : string ;
823+ regex : RegExp ;
824+ } > {
825+ const compiled : Array < {
826+ raw : string ;
827+ regex : RegExp ;
828+ } > = [ ] ;
829+
830+ for ( const patternText of patterns ) {
831+ try {
832+ compiled . push ( {
833+ raw : patternText ,
834+ regex : new RegExp ( patternText , "i" ) ,
835+ } ) ;
836+ } catch { }
837+ }
838+
839+ return compiled ;
840+ }
841+
821842function walkNodes (
822843 node : RuntimeNode ,
823844 visitor : ( node : RuntimeNode ) => void ,
0 commit comments