is-antibot 2.6.0 → 2.6.1

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "is-antibot",
3
3
  "description": "Detect antibot protection from 30+ providers — Cloudflare, Akamai, DataDome, PerimeterX, and more.",
4
4
  "homepage": "https://antibot.microlink.io/",
5
- "version": "2.6.0",
5
+ "version": "2.6.1",
6
6
  "types": "./src/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
package/src/index.d.ts CHANGED
@@ -87,6 +87,7 @@ declare namespace isAntibot {
87
87
  domainWithoutSuffix?: string
88
88
  fallback?: true
89
89
  statusCodes?: number[]
90
+ unlessHeaders?: string[]
90
91
  rules: DetectionRule[]
91
92
  }
92
93
 
package/src/index.js CHANGED
@@ -345,9 +345,14 @@ const compileDetection = detection => {
345
345
  throw new TypeError('An asn detection cannot be gated by statusCodes')
346
346
  }
347
347
  const compiled = DETECTION_COMPILERS[detection.type](detection)
348
- if (!Array.isArray(statusCodes) || statusCodes.length === 0) {
349
- return compiled
350
- }
348
+ return gateByHeaders(
349
+ gateByStatusCodes(compiled, statusCodes),
350
+ detection.unlessHeaders
351
+ )
352
+ }
353
+
354
+ const gateByStatusCodes = (compiled, statusCodes) => {
355
+ if (!Array.isArray(statusCodes) || statusCodes.length === 0) return compiled
351
356
  const allowed = new Set(statusCodes)
352
357
  const innerMatches = compiled.matches
353
358
  return {
@@ -356,6 +361,17 @@ const compileDetection = detection => {
356
361
  }
357
362
  }
358
363
 
364
+ const gateByHeaders = (compiled, unlessHeaders) => {
365
+ if (!Array.isArray(unlessHeaders) || unlessHeaders.length === 0) { return compiled }
366
+ const innerMatches = compiled.matches
367
+ const isVetoed = ({ getHeader }) =>
368
+ unlessHeaders.some(name => getHeader(name) != null)
369
+ return {
370
+ ...compiled,
371
+ matches: context => innerMatches(context) && !isVetoed(context)
372
+ }
373
+ }
374
+
359
375
  const escapeRegExp = value => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
360
376
 
361
377
  const compileAnyLiteral = (providers, type) =>
@@ -78,6 +78,7 @@
78
78
  "technique": "waf",
79
79
  "fallback": true,
80
80
  "statusCodes": [403],
81
+ "unlessHeaders": ["x-failure-reason"],
81
82
  "rules": [
82
83
  {
83
84
  "header": "server",
package/src/schema.json CHANGED
@@ -98,6 +98,15 @@
98
98
  "minItems": 1,
99
99
  "description": "When set, the detection matches only if the response status code equals one of these values (AND with the detection's rules)."
100
100
  },
101
+ "unlessHeaders": {
102
+ "type": "array",
103
+ "items": {
104
+ "type": "string",
105
+ "minLength": 1
106
+ },
107
+ "minItems": 1,
108
+ "description": "Response headers whose presence vetoes the detection, whatever their value. For origins that state why they refused (e.g. x-failure-reason: failure-expired on an expired signed URL) behind an edge that otherwise looks like a block."
109
+ },
101
110
  "rules": {
102
111
  "type": "array",
103
112
  "minItems": 1,