11import path from 'path' ;
22import fs from 'fs-extra' ;
33import { constants } from '../../../tools' ;
4- import { dumpJSON , existsMustBeDir , loadJSON } from '../../../utils' ;
4+ import { dumpJSON , existsMustBeDir , isFile , loadJSON } from '../../../utils' ;
55import { DirectoryHandler } from '.' ;
66import DirectoryContext from '..' ;
77import { ParsedAsset } from '../../../types' ;
@@ -39,10 +39,6 @@ function parse(context: DirectoryContext): ParsedAttackProtection {
3939 } ;
4040 }
4141
42- const botDetection = loadJSON ( files . botDetection , {
43- mappings : context . mappings ,
44- disableKeywordReplacement : context . disableKeywordReplacement ,
45- } ) ;
4642 const breachedPasswordDetection = loadJSON ( files . breachedPasswordDetection , {
4743 mappings : context . mappings ,
4844 disableKeywordReplacement : context . disableKeywordReplacement ,
@@ -51,25 +47,33 @@ function parse(context: DirectoryContext): ParsedAttackProtection {
5147 mappings : context . mappings ,
5248 disableKeywordReplacement : context . disableKeywordReplacement ,
5349 } ) ;
54- const captcha = loadJSON ( files . captcha , {
55- mappings : context . mappings ,
56- disableKeywordReplacement : context . disableKeywordReplacement ,
57- } ) ;
5850 const suspiciousIpThrottling = loadJSON ( files . suspiciousIpThrottling , {
5951 mappings : context . mappings ,
6052 disableKeywordReplacement : context . disableKeywordReplacement ,
6153 } ) ;
6254
63- const maskedAttackProtection = attackProtectionDefaults ( {
64- botDetection,
55+ const attackProtection : AttackProtection = {
6556 breachedPasswordDetection,
6657 bruteForceProtection,
67- captcha,
6858 suspiciousIpThrottling,
69- } ) ;
59+ } ;
60+
61+ if ( isFile ( files . botDetection ) ) {
62+ attackProtection . botDetection = loadJSON ( files . botDetection , {
63+ mappings : context . mappings ,
64+ disableKeywordReplacement : context . disableKeywordReplacement ,
65+ } ) ;
66+ }
67+
68+ if ( isFile ( files . captcha ) ) {
69+ attackProtection . captcha = loadJSON ( files . captcha , {
70+ mappings : context . mappings ,
71+ disableKeywordReplacement : context . disableKeywordReplacement ,
72+ } ) ;
73+ }
7074
7175 return {
72- attackProtection : maskedAttackProtection ,
76+ attackProtection,
7377 } ;
7478}
7579
@@ -81,20 +85,22 @@ async function dump(context: DirectoryContext): Promise<void> {
8185 const files = attackProtectionFiles ( context . filePath ) ;
8286 fs . ensureDirSync ( files . directory ) ;
8387
84- if ( attackProtection . botDetection ) {
85- dumpJSON ( files . botDetection , attackProtection . botDetection ) ;
88+ const maskedAttackProtection = attackProtectionDefaults ( attackProtection ) ;
89+
90+ if ( maskedAttackProtection . botDetection ) {
91+ dumpJSON ( files . botDetection , maskedAttackProtection . botDetection ) ;
8692 }
87- if ( attackProtection . breachedPasswordDetection ) {
88- dumpJSON ( files . breachedPasswordDetection , attackProtection . breachedPasswordDetection ) ;
93+ if ( maskedAttackProtection . breachedPasswordDetection ) {
94+ dumpJSON ( files . breachedPasswordDetection , maskedAttackProtection . breachedPasswordDetection ) ;
8995 }
90- if ( attackProtection . bruteForceProtection ) {
91- dumpJSON ( files . bruteForceProtection , attackProtection . bruteForceProtection ) ;
96+ if ( maskedAttackProtection . bruteForceProtection ) {
97+ dumpJSON ( files . bruteForceProtection , maskedAttackProtection . bruteForceProtection ) ;
9298 }
93- if ( attackProtection . captcha ) {
94- dumpJSON ( files . captcha , attackProtection . captcha ) ;
99+ if ( maskedAttackProtection . captcha ) {
100+ dumpJSON ( files . captcha , maskedAttackProtection . captcha ) ;
95101 }
96- if ( attackProtection . suspiciousIpThrottling ) {
97- dumpJSON ( files . suspiciousIpThrottling , attackProtection . suspiciousIpThrottling ) ;
102+ if ( maskedAttackProtection . suspiciousIpThrottling ) {
103+ dumpJSON ( files . suspiciousIpThrottling , maskedAttackProtection . suspiciousIpThrottling ) ;
98104 }
99105}
100106
0 commit comments