Skip to content

Commit 86de11d

Browse files
authored
fix: bot detection and captcha files optional loading & CIDR validation (#1200)
* feat(attackProtection.ts): refactor bot detection and captcha update calls - src/tools/auth0/handlers/attackProtection.ts: simplify update calls for bot detection, captcha, and other configurations * feat(src/context/directory/handlers/attackProtection.ts): update bot detection and captcha loading logic - src/context/directory/handlers/attackProtection.ts: refactor to check if bot detection and captcha files exist before loading - src/context/yaml/handlers/attackProtection.ts: rename parseAndDump to separate parse and dump functions
1 parent 1914ea2 commit 86de11d

File tree

3 files changed

+48
-54
lines changed

3 files changed

+48
-54
lines changed

src/context/directory/handlers/attackProtection.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import fs from 'fs-extra';
33
import { constants } from '../../../tools';
4-
import { dumpJSON, existsMustBeDir, loadJSON } from '../../../utils';
4+
import { dumpJSON, existsMustBeDir, isFile, loadJSON } from '../../../utils';
55
import { DirectoryHandler } from '.';
66
import DirectoryContext from '..';
77
import { 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

src/context/yaml/handlers/attackProtection.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ import { attackProtectionDefaults } from '../../defaults';
66

77
type ParsedAttackProtection = ParsedAsset<'attackProtection', AttackProtection>;
88

9-
async function parseAndDump(context: YAMLContext): Promise<ParsedAttackProtection> {
9+
async function parse(context: YAMLContext): Promise<ParsedAttackProtection> {
10+
const { attackProtection } = context.assets;
11+
12+
if (!attackProtection) return { attackProtection: null };
13+
14+
return {
15+
attackProtection,
16+
};
17+
}
18+
19+
async function dump(context: YAMLContext): Promise<ParsedAttackProtection> {
1020
const { attackProtection } = context.assets;
1121

1222
if (!attackProtection) return { attackProtection: null };
@@ -41,8 +51,8 @@ async function parseAndDump(context: YAMLContext): Promise<ParsedAttackProtectio
4151
}
4252

4353
const attackProtectionHandler: YAMLHandler<ParsedAttackProtection> = {
44-
parse: parseAndDump,
45-
dump: parseAndDump,
54+
parse: parse,
55+
dump: dump,
4656
};
4757

4858
export default attackProtectionHandler;

src/tools/auth0/handlers/attackProtection.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ export const schema = {
4141
type: 'array',
4242
items: {
4343
type: 'string',
44-
oneOf: [
45-
{
46-
type: 'string',
47-
format: 'ipv4',
48-
description: 'IPv4 address or CIDR block',
49-
},
50-
{
51-
type: 'string',
52-
format: 'ipv6',
53-
description: 'IPv6 address or CIDR block',
54-
},
55-
],
5644
description: 'IP address (IPv4 or IPv6) or CIDR block',
5745
},
5846
description: 'List of IP addresses or CIDR blocks to allowlist',
@@ -301,20 +289,15 @@ export default class AttackProtectionHandler extends DefaultAPIHandler {
301289

302290
const updates: Promise<unknown>[] = [];
303291

304-
const attackProtectionClient = this.client.attackProtection;
305-
306292
if (attackProtection.botDetection && Object.keys(attackProtection.botDetection).length) {
307293
updates.push(
308-
attackProtectionClient.updateBotDetectionConfig.call(
309-
attackProtectionClient,
310-
attackProtection.botDetection
311-
)
294+
this.client.attackProtection.updateBotDetectionConfig(attackProtection.botDetection)
312295
);
313296
}
314297

315298
if (attackProtection.breachedPasswordDetection) {
316299
updates.push(
317-
attackProtectionClient.updateBreachedPasswordDetectionConfig(
300+
this.client.attackProtection.updateBreachedPasswordDetectionConfig(
318301
attackProtection.breachedPasswordDetection
319302
)
320303
);
@@ -340,23 +323,18 @@ export default class AttackProtectionHandler extends DefaultAPIHandler {
340323

341324
attackProtection.captcha = captcha;
342325

343-
updates.push(
344-
attackProtectionClient.updateCaptchaConfig.call(
345-
attackProtectionClient,
346-
attackProtection.captcha
347-
)
348-
);
326+
updates.push(this.client.attackProtection.updateCaptchaConfig(attackProtection.captcha));
349327
}
350328

351329
if (attackProtection.bruteForceProtection) {
352330
updates.push(
353-
attackProtectionClient.updateBruteForceConfig(attackProtection.bruteForceProtection)
331+
this.client.attackProtection.updateBruteForceConfig(attackProtection.bruteForceProtection)
354332
);
355333
}
356334

357335
if (attackProtection.suspiciousIpThrottling) {
358336
updates.push(
359-
attackProtectionClient.updateSuspiciousIpThrottlingConfig(
337+
this.client.attackProtection.updateSuspiciousIpThrottlingConfig(
360338
attackProtection.suspiciousIpThrottling
361339
)
362340
);

0 commit comments

Comments
 (0)