is-antibot 2.5.21 → 2.6.0

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.21",
5
+ "version": "2.6.0",
6
6
  "types": "./src/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
@@ -79,7 +79,7 @@
79
79
  "standard-version": "latest"
80
80
  },
81
81
  "engines": {
82
- "node": ">= 20"
82
+ "node": ">= 22"
83
83
  },
84
84
  "files": [
85
85
  "src"
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,12 +78,14 @@ 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[]
81
90
  rules: DetectionRule[]
82
91
  }
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,12 +329,22 @@ 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
344
+ if (detection.type === 'asn' && statusCodes !== undefined) {
345
+ throw new TypeError('An asn detection cannot be gated by statusCodes')
346
+ }
347
+ const compiled = DETECTION_COMPILERS[detection.type](detection)
323
348
  if (!Array.isArray(statusCodes) || statusCodes.length === 0) {
324
349
  return compiled
325
350
  }
@@ -344,17 +369,26 @@ const compileAnyLiteral = (providers, type) =>
344
369
  .join('|')
345
370
  )
346
371
 
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
- })
372
+ const compileProviders = ({ providers = [] } = {}) => {
373
+ const entries = providers.flatMap(provider =>
374
+ provider.detections.map(detection => ({
375
+ provider: provider.name,
376
+ fallback: detection.fallback === true,
377
+ detection: compileDetection(detection)
378
+ }))
379
+ )
380
+ return {
381
+ detections: [
382
+ ...entries.filter(entry => !entry.fallback),
383
+ ...entries.filter(entry => entry.fallback)
384
+ ],
385
+ anyHtmlLiteral: compileAnyLiteral(providers, 'html')
386
+ }
387
+ }
354
388
 
355
389
  const detectWithProviders = (
356
- { providers: compiledProviders, anyHtmlLiteral },
357
- { headers = {}, html = '', url = '', statusCode } = {}
390
+ { detections, anyHtmlLiteral },
391
+ { headers = {}, html = '', url = '', statusCode, asn } = {}
358
392
  ) => {
359
393
  const getHeader = createGetHeader(headers)
360
394
  let hasCookieImpl
@@ -382,30 +416,29 @@ const detectWithProviders = (
382
416
  hasCookie,
383
417
  htmlHas,
384
418
  urlHas,
385
- statusCode
419
+ statusCode,
420
+ asn
386
421
  }
387
422
 
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
- }
423
+ for (const { provider, detection } of detections) {
424
+ if (detection.domain || detection.domainWithoutSuffix) {
425
+ const parsed = getParsedUrl()
426
+ if (!parsed) continue
427
+ if (detection.domain && detection.domain !== parsed.domain) continue
428
+ if (
429
+ detection.domainWithoutSuffix &&
430
+ detection.domainWithoutSuffix !== parsed.domainWithoutSuffix
431
+ ) {
432
+ continue
400
433
  }
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
434
  }
435
+ if (!detection.matches(context)) continue
436
+ return createResult(
437
+ true,
438
+ provider,
439
+ DETECTION[detection.type] || detection.type,
440
+ detection.technique
441
+ )
409
442
  }
410
443
 
411
444
  return createResult(false, null, null)
@@ -413,13 +446,20 @@ const detectWithProviders = (
413
446
 
414
447
  const COMPILED_PROVIDERS = compileProviders(providersData)
415
448
 
449
+ const toAsn = asn => {
450
+ const body = String(asn).trim().replace(/^AS/i, '')
451
+ if (body === '') return undefined
452
+ return Number(body)
453
+ }
454
+
416
455
  const normalizeInput = input => {
417
- const { headers, html, body, url, statusCode, status } = input || {}
456
+ const { headers, html, body, url, statusCode, status, asn } = input || {}
418
457
  return {
419
458
  headers,
420
459
  html: html || body,
421
460
  url,
422
- statusCode: statusCode ?? status
461
+ statusCode: statusCode ?? status,
462
+ asn: toAsn(asn)
423
463
  }
424
464
  }
425
465
 
@@ -73,6 +73,18 @@
73
73
  }
74
74
  ]
75
75
  },
76
+ {
77
+ "type": "headers",
78
+ "technique": "waf",
79
+ "fallback": true,
80
+ "statusCodes": [403],
81
+ "rules": [
82
+ {
83
+ "header": "server",
84
+ "oneOf": ["AkamaiGHost", "AkamaiNetStorage"]
85
+ }
86
+ ]
87
+ },
76
88
  {
77
89
  "type": "cookies",
78
90
  "technique": "cookie",
@@ -91,6 +103,19 @@
91
103
  "contains": "bmak.sensor_data"
92
104
  }
93
105
  ]
106
+ },
107
+ {
108
+ "type": "asn",
109
+ "technique": "waf",
110
+ "fallback": true,
111
+ "rules": [
112
+ {
113
+ "asn": 16625
114
+ },
115
+ {
116
+ "asn": 20940
117
+ }
118
+ ]
94
119
  }
95
120
  ]
96
121
  },
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": {
@@ -123,6 +139,9 @@
123
139
  },
124
140
  {
125
141
  "$ref": "#/$defs/statusEquals"
142
+ },
143
+ {
144
+ "$ref": "#/$defs/asnEquals"
126
145
  }
127
146
  ]
128
147
  },
@@ -263,6 +282,17 @@
263
282
  }
264
283
  }
265
284
  },
285
+ "asnEquals": {
286
+ "type": "object",
287
+ "description": "Match when the origin IP belongs to the autonomous system with this number.",
288
+ "required": ["asn"],
289
+ "additionalProperties": false,
290
+ "properties": {
291
+ "asn": {
292
+ "type": "integer"
293
+ }
294
+ }
295
+ },
266
296
  "statusEquals": {
267
297
  "type": "object",
268
298
  "description": "Match when the HTTP status code equals the given value.",