is-antibot 2.5.22 → 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.5.22",
5
+ "version": "2.6.1",
6
6
  "types": "./src/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
package/src/index.d.ts CHANGED
@@ -3,7 +3,13 @@ type HeadersLike =
3
3
  | Record<string, string | string[] | undefined>
4
4
 
5
5
  declare namespace isAntibot {
6
- type Detection = 'headers' | 'cookies' | 'html' | 'url' | 'statusCode'
6
+ type Detection =
7
+ | 'headers'
8
+ | 'cookies'
9
+ | 'html'
10
+ | 'url'
11
+ | 'statusCode'
12
+ | 'asn'
7
13
  type Technique = 'javascript' | 'captcha' | 'waf' | 'cookie'
8
14
  type ProviderName =
9
15
  | 'cloudflare'
@@ -52,6 +58,7 @@ declare namespace isAntibot {
52
58
  url?: string
53
59
  statusCode?: number
54
60
  status?: number
61
+ asn?: number | string
55
62
  }
56
63
 
57
64
  interface Result {
@@ -71,13 +78,16 @@ declare namespace isAntibot {
71
78
  | { contains: string }
72
79
  | { regex: string; flags?: string }
73
80
  | { status: number }
81
+ | { asn: number }
74
82
 
75
83
  interface DetectorDetection {
76
- type: 'headers' | 'cookies' | 'html' | 'url' | 'status_code'
84
+ type: 'headers' | 'cookies' | 'html' | 'url' | 'status_code' | 'asn'
77
85
  technique?: Technique
78
86
  domain?: string
79
87
  domainWithoutSuffix?: string
88
+ fallback?: true
80
89
  statusCodes?: number[]
90
+ unlessHeaders?: string[]
81
91
  rules: DetectionRule[]
82
92
  }
83
93
 
package/src/index.js CHANGED
@@ -11,7 +11,8 @@ const DETECTION = {
11
11
  cookies: 'cookies',
12
12
  html: 'html',
13
13
  url: 'url',
14
- status_code: 'statusCode'
14
+ status_code: 'statusCode',
15
+ asn: 'asn'
15
16
  }
16
17
 
17
18
  const OPEN_GRAPH = 'openGraph'
@@ -46,10 +47,14 @@ const hasPostOpenGraph = html => {
46
47
  )
47
48
  }
48
49
 
49
- const createGetHeader = headers =>
50
- typeof headers.get === 'function'
51
- ? name => headers.get(name)
52
- : name => headers[name]
50
+ const createGetHeader = headers => {
51
+ if (typeof headers.get === 'function') return name => headers.get(name)
52
+ const byName = new Map()
53
+ for (const name of Object.keys(headers)) {
54
+ byName.set(name.toLowerCase(), headers[name])
55
+ }
56
+ return name => byName.get(name.toLowerCase())
57
+ }
53
58
 
54
59
  const createTestPattern = value => {
55
60
  if (!value) return () => false
@@ -259,7 +264,12 @@ const compileHeaderRule = rule => {
259
264
 
260
265
  if (rule.oneOf !== undefined) {
261
266
  const oneOf = new Set(rule.oneOf)
262
- return ({ getHeader }) => oneOf.has(getHeader(rule.header))
267
+ return ({ getHeader }) => {
268
+ const value = getHeader(rule.header)
269
+ return Array.isArray(value)
270
+ ? value.some(item => oneOf.has(item))
271
+ : oneOf.has(value)
272
+ }
263
273
  }
264
274
 
265
275
  if (typeof rule.headerNamePattern === 'string') {
@@ -270,6 +280,11 @@ const compileHeaderRule = rule => {
270
280
  throw new TypeError('Invalid header rule shape')
271
281
  }
272
282
 
283
+ const compileAsnRule = rule => {
284
+ if (!Number.isInteger(rule.asn)) throw new TypeError('Invalid asn rule shape')
285
+ return rule.asn
286
+ }
287
+
273
288
  const compileTextPattern = rule => {
274
289
  const pattern =
275
290
  rule.contains !== undefined
@@ -314,15 +329,30 @@ const DETECTION_COMPILERS = {
314
329
  return createCompiledDetection(detection, ({ statusCode }) =>
315
330
  statusCodes.includes(statusCode)
316
331
  )
332
+ },
333
+ asn: detection => {
334
+ const networks = new Set(detection.rules.map(compileAsnRule))
335
+ return createCompiledDetection(
336
+ detection,
337
+ ({ asn, statusCode }) => statusCode == null && networks.has(asn)
338
+ )
317
339
  }
318
340
  }
319
341
 
320
342
  const compileDetection = detection => {
321
- const compiled = DETECTION_COMPILERS[detection.type](detection)
322
343
  const statusCodes = detection.statusCodes
323
- if (!Array.isArray(statusCodes) || statusCodes.length === 0) {
324
- return compiled
344
+ if (detection.type === 'asn' && statusCodes !== undefined) {
345
+ throw new TypeError('An asn detection cannot be gated by statusCodes')
325
346
  }
347
+ const compiled = DETECTION_COMPILERS[detection.type](detection)
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
326
356
  const allowed = new Set(statusCodes)
327
357
  const innerMatches = compiled.matches
328
358
  return {
@@ -331,6 +361,17 @@ const compileDetection = detection => {
331
361
  }
332
362
  }
333
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
+
334
375
  const escapeRegExp = value => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
335
376
 
336
377
  const compileAnyLiteral = (providers, type) =>
@@ -344,17 +385,26 @@ const compileAnyLiteral = (providers, type) =>
344
385
  .join('|')
345
386
  )
346
387
 
347
- const compileProviders = ({ providers = [] } = {}) => ({
348
- providers: providers.map(provider => ({
349
- name: provider.name,
350
- detections: provider.detections.map(compileDetection)
351
- })),
352
- anyHtmlLiteral: compileAnyLiteral(providers, 'html')
353
- })
388
+ const compileProviders = ({ providers = [] } = {}) => {
389
+ const entries = providers.flatMap(provider =>
390
+ provider.detections.map(detection => ({
391
+ provider: provider.name,
392
+ fallback: detection.fallback === true,
393
+ detection: compileDetection(detection)
394
+ }))
395
+ )
396
+ return {
397
+ detections: [
398
+ ...entries.filter(entry => !entry.fallback),
399
+ ...entries.filter(entry => entry.fallback)
400
+ ],
401
+ anyHtmlLiteral: compileAnyLiteral(providers, 'html')
402
+ }
403
+ }
354
404
 
355
405
  const detectWithProviders = (
356
- { providers: compiledProviders, anyHtmlLiteral },
357
- { headers = {}, html = '', url = '', statusCode } = {}
406
+ { detections, anyHtmlLiteral },
407
+ { headers = {}, html = '', url = '', statusCode, asn } = {}
358
408
  ) => {
359
409
  const getHeader = createGetHeader(headers)
360
410
  let hasCookieImpl
@@ -382,30 +432,29 @@ const detectWithProviders = (
382
432
  hasCookie,
383
433
  htmlHas,
384
434
  urlHas,
385
- statusCode
435
+ statusCode,
436
+ asn
386
437
  }
387
438
 
388
- for (const provider of compiledProviders) {
389
- for (const detection of provider.detections) {
390
- if (detection.domain || detection.domainWithoutSuffix) {
391
- const parsed = getParsedUrl()
392
- if (!parsed) continue
393
- if (detection.domain && detection.domain !== parsed.domain) continue
394
- if (
395
- detection.domainWithoutSuffix &&
396
- detection.domainWithoutSuffix !== parsed.domainWithoutSuffix
397
- ) {
398
- continue
399
- }
439
+ for (const { provider, detection } of detections) {
440
+ if (detection.domain || detection.domainWithoutSuffix) {
441
+ const parsed = getParsedUrl()
442
+ if (!parsed) continue
443
+ if (detection.domain && detection.domain !== parsed.domain) continue
444
+ if (
445
+ detection.domainWithoutSuffix &&
446
+ detection.domainWithoutSuffix !== parsed.domainWithoutSuffix
447
+ ) {
448
+ continue
400
449
  }
401
- if (!detection.matches(context)) continue
402
- return createResult(
403
- true,
404
- provider.name,
405
- DETECTION[detection.type] || detection.type,
406
- detection.technique
407
- )
408
450
  }
451
+ if (!detection.matches(context)) continue
452
+ return createResult(
453
+ true,
454
+ provider,
455
+ DETECTION[detection.type] || detection.type,
456
+ detection.technique
457
+ )
409
458
  }
410
459
 
411
460
  return createResult(false, null, null)
@@ -413,13 +462,20 @@ const detectWithProviders = (
413
462
 
414
463
  const COMPILED_PROVIDERS = compileProviders(providersData)
415
464
 
465
+ const toAsn = asn => {
466
+ const body = String(asn).trim().replace(/^AS/i, '')
467
+ if (body === '') return undefined
468
+ return Number(body)
469
+ }
470
+
416
471
  const normalizeInput = input => {
417
- const { headers, html, body, url, statusCode, status } = input || {}
472
+ const { headers, html, body, url, statusCode, status, asn } = input || {}
418
473
  return {
419
474
  headers,
420
475
  html: html || body,
421
476
  url,
422
- statusCode: statusCode ?? status
477
+ statusCode: statusCode ?? status,
478
+ asn: toAsn(asn)
423
479
  }
424
480
  }
425
481
 
@@ -73,6 +73,19 @@
73
73
  }
74
74
  ]
75
75
  },
76
+ {
77
+ "type": "headers",
78
+ "technique": "waf",
79
+ "fallback": true,
80
+ "statusCodes": [403],
81
+ "unlessHeaders": ["x-failure-reason"],
82
+ "rules": [
83
+ {
84
+ "header": "server",
85
+ "oneOf": ["AkamaiGHost", "AkamaiNetStorage"]
86
+ }
87
+ ]
88
+ },
76
89
  {
77
90
  "type": "cookies",
78
91
  "technique": "cookie",
@@ -91,6 +104,19 @@
91
104
  "contains": "bmak.sensor_data"
92
105
  }
93
106
  ]
107
+ },
108
+ {
109
+ "type": "asn",
110
+ "technique": "waf",
111
+ "fallback": true,
112
+ "rules": [
113
+ {
114
+ "asn": 16625
115
+ },
116
+ {
117
+ "asn": 20940
118
+ }
119
+ ]
94
120
  }
95
121
  ]
96
122
  },
package/src/schema.json CHANGED
@@ -58,9 +58,21 @@
58
58
  "type": "object",
59
59
  "required": ["type", "technique", "rules"],
60
60
  "additionalProperties": false,
61
+ "if": {
62
+ "type": "object",
63
+ "properties": { "type": { "const": "asn" } }
64
+ },
65
+ "then": {
66
+ "type": "object",
67
+ "not": { "type": "object", "required": ["statusCodes"] },
68
+ "properties": {
69
+ "rules": { "type": "array", "items": { "$ref": "#/$defs/asnEquals" } }
70
+ }
71
+ },
61
72
  "properties": {
62
73
  "type": {
63
- "enum": ["headers", "cookies", "html", "url", "status_code"]
74
+ "enum": ["headers", "cookies", "html", "url", "status_code", "asn"],
75
+ "description": "asn matches only when the response has no status code: the origin never answered, so the network its IP belongs to is the only signal left."
64
76
  },
65
77
  "technique": {
66
78
  "enum": ["javascript", "captcha", "waf", "cookie"],
@@ -74,6 +86,10 @@
74
86
  "type": "string",
75
87
  "description": "When set, this detection only applies if the request URL's registrable domain without public suffix matches (e.g. 'amazon' for amazon.es, amazon.co.uk, amazon.com)."
76
88
  },
89
+ "fallback": {
90
+ "const": true,
91
+ "description": "Evaluate this detection only after every provider's other detections missed. For weak signals (an edge server header, the origin network) that would otherwise take the verdict from a more specific provider seen through the same edge."
92
+ },
77
93
  "statusCodes": {
78
94
  "type": "array",
79
95
  "items": {
@@ -82,6 +98,15 @@
82
98
  "minItems": 1,
83
99
  "description": "When set, the detection matches only if the response status code equals one of these values (AND with the detection's rules)."
84
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
+ },
85
110
  "rules": {
86
111
  "type": "array",
87
112
  "minItems": 1,
@@ -123,6 +148,9 @@
123
148
  },
124
149
  {
125
150
  "$ref": "#/$defs/statusEquals"
151
+ },
152
+ {
153
+ "$ref": "#/$defs/asnEquals"
126
154
  }
127
155
  ]
128
156
  },
@@ -263,6 +291,17 @@
263
291
  }
264
292
  }
265
293
  },
294
+ "asnEquals": {
295
+ "type": "object",
296
+ "description": "Match when the origin IP belongs to the autonomous system with this number.",
297
+ "required": ["asn"],
298
+ "additionalProperties": false,
299
+ "properties": {
300
+ "asn": {
301
+ "type": "integer"
302
+ }
303
+ }
304
+ },
266
305
  "statusEquals": {
267
306
  "type": "object",
268
307
  "description": "Match when the HTTP status code equals the given value.",