is-antibot 2.5.16 → 2.5.17

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.16",
5
+ "version": "2.5.17",
6
6
  "types": "./src/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
package/src/index.js CHANGED
@@ -14,6 +14,38 @@ const DETECTION = {
14
14
  status_code: 'statusCode'
15
15
  }
16
16
 
17
+ const OPEN_GRAPH = 'openGraph'
18
+ const META_KEY = /\b(?:property|name)\s*=\s*(?:"([^"]*)"|'([^']*)')/i
19
+ const META_CONTENT = /\bcontent\s*=\s*(?:"([^"]*)"|'([^']*)')/i
20
+ const SHELL_OG_TITLE = /^(?:login\s*[•·]\s*)?instagram$/i
21
+
22
+ const metaAttr = (tag, pattern) => {
23
+ const match = pattern.exec(tag)
24
+ return match ? match[1] ?? match[2] ?? '' : ''
25
+ }
26
+
27
+ /**
28
+ * A logged-out Instagram document can keep `<title>Instagram</title>` while
29
+ * `og:title` and `og:image` already describe the post. That document is the
30
+ * metadata; a shell has the tab title and no post image.
31
+ */
32
+ const hasPostOpenGraph = html => {
33
+ let title = ''
34
+ let image = ''
35
+ for (const tag of html.matchAll(/<meta\b[^>]*>/gi)) {
36
+ const key = metaAttr(tag[0], META_KEY).toLowerCase()
37
+ if (key !== 'og:title' && key !== 'og:image') continue
38
+ const content = metaAttr(tag[0], META_CONTENT).trim()
39
+ if (key === 'og:title') title = content
40
+ else if (/^https?:\/\//i.test(content)) image = content
41
+ }
42
+ return (
43
+ Boolean(image) &&
44
+ Boolean(title) &&
45
+ !SHELL_OG_TITLE.test(title.replace(/\s+/g, ' '))
46
+ )
47
+ }
48
+
17
49
  const createGetHeader = headers =>
18
50
  typeof headers.get === 'function'
19
51
  ? name => headers.get(name)
@@ -41,11 +73,21 @@ const createCompiledTestPattern = value => {
41
73
  if (!value) return () => false
42
74
  const lowerValue = value.toLowerCase()
43
75
  return pattern => {
44
- if (pattern instanceof RegExp) return pattern.test(value)
45
- if (pattern && pattern.type === 'contains') {
46
- return lowerValue.indexOf(pattern.value) !== -1
76
+ const inner =
77
+ pattern && pattern.type === OPEN_GRAPH ? pattern.pattern : pattern
78
+ let matched = false
79
+ if (inner instanceof RegExp) {
80
+ matched = inner.test(value)
81
+ } else if (inner && inner.type === 'contains') {
82
+ matched = lowerValue.indexOf(inner.value) !== -1
83
+ } else if (inner) {
84
+ matched = lowerValue.indexOf(String(inner).toLowerCase()) !== -1
47
85
  }
48
- return lowerValue.indexOf(pattern.toLowerCase()) !== -1
86
+ if (!matched) return false
87
+ if (pattern && pattern.type === OPEN_GRAPH && hasPostOpenGraph(value)) {
88
+ return false
89
+ }
90
+ return true
49
91
  }
50
92
  }
51
93
 
@@ -222,10 +264,12 @@ const compileHeaderRule = rule => {
222
264
  }
223
265
 
224
266
  const compileTextPattern = rule => {
225
- if (rule.contains !== undefined) {
226
- return { type: 'contains', value: rule.contains.toLowerCase() }
227
- }
228
- return createRegExp(rule.regex, rule.flags ?? 'i')
267
+ const pattern =
268
+ rule.contains !== undefined
269
+ ? { type: 'contains', value: rule.contains.toLowerCase() }
270
+ : createRegExp(rule.regex, rule.flags ?? 'i')
271
+ if (!rule.unlessOpenGraph) return pattern
272
+ return { type: OPEN_GRAPH, pattern }
229
273
  }
230
274
 
231
275
  const compileTextDetection = (detection, getPatternTester) => {
@@ -952,11 +952,13 @@
952
952
  "rules": [
953
953
  {
954
954
  "regex": "<title>\\s*Login\\s*[•·]\\s*Instagram\\s*<\\/title>",
955
- "flags": "i"
955
+ "flags": "i",
956
+ "unlessOpenGraph": true
956
957
  },
957
958
  {
958
959
  "regex": "<title>\\s*Instagram\\s*<\\/title>",
959
- "flags": "i"
960
+ "flags": "i",
961
+ "unlessOpenGraph": true
960
962
  }
961
963
  ]
962
964
  }
package/src/schema.json CHANGED
@@ -256,6 +256,10 @@
256
256
  "type": "string",
257
257
  "pattern": "^[imsu]*$",
258
258
  "default": "i"
259
+ },
260
+ "unlessOpenGraph": {
261
+ "const": true,
262
+ "description": "Suppress the match when the document already has a non-shell og:title and an absolute og:image."
259
263
  }
260
264
  }
261
265
  },