tldts-core 7.4.27 → 7.5.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/src/domain.ts CHANGED
@@ -11,7 +11,10 @@ import { IOptions } from './options';
11
11
  * * hostname = 'not.evil.com' and vhost = 'evil.com' => ok
12
12
  * * hostname = 'not.evil.com' and vhost = 'not.evil.com' => ok
13
13
  */
14
- function shareSameDomainSuffix(hostname: string, vhost: string): boolean {
14
+ export function shareSameDomainSuffix(
15
+ hostname: string,
16
+ vhost: string,
17
+ ): boolean {
15
18
  if (hostname.endsWith(vhost)) {
16
19
  return (
17
20
  hostname.length === vhost.length ||
@@ -148,7 +148,7 @@ export default function extractHostname(
148
148
  // Scheme-relative reference ("//host/path").
149
149
  start += 2;
150
150
  } else {
151
- const indexOfProtocol = url.indexOf(':/', start);
151
+ let indexOfProtocol = url.indexOf(':/', start);
152
152
  if (indexOfProtocol !== -1) {
153
153
  // "scheme://…". Classify the scheme, then position `start` at the host.
154
154
  const special = getSpecialScheme(url, start, indexOfProtocol);
@@ -200,23 +200,40 @@ export default function extractHostname(
200
200
  validate,
201
201
  );
202
202
  }
203
- return null;
203
+ // A '/', '\', '?' or '#' first puts this ':/' in a path, query or
204
+ // fragment ("a.com/r?u=https://x"): re-read the input without it.
205
+ for (let j = i; j < indexOfProtocol; j += 1) {
206
+ const c = url.charCodeAt(j);
207
+ if (c === 47 || c === 92 || c === 63 || c === 35) {
208
+ indexOfProtocol = -1;
209
+ break;
210
+ }
211
+ }
212
+ if (indexOfProtocol !== -1) {
213
+ return null;
214
+ }
215
+ break;
204
216
  }
205
217
  }
206
- // A non-special scheme has an authority only after "//" (else it is an
207
- // opaque path with no host). `indexOf(':/')` already gave the first '/'.
208
- if (url.charCodeAt(indexOfProtocol + 2) === 47 /* '/' */) {
209
- isSpecial = false;
210
- start = indexOfProtocol + 3;
211
- } else {
212
- return null;
218
+ if (indexOfProtocol !== -1) {
219
+ // A non-special scheme has an authority only after "//" (else it is
220
+ // an opaque path with no host). `indexOf(':/')` already gave the
221
+ // first '/'.
222
+ if (url.charCodeAt(indexOfProtocol + 2) === 47 /* '/' */) {
223
+ isSpecial = false;
224
+ start = indexOfProtocol + 3;
225
+ } else {
226
+ return null;
227
+ }
213
228
  }
214
229
  }
215
- } else if (url.charCodeAt(start) !== 91 /* '[' */) {
216
- // Cold path: no scheme "://", and not a bare IPv6 literal (whose first
217
- // ':' would otherwise look like a scheme separator; "[…]" falls through
218
- // to the ipv6 handling below). May be a bare host, a host:port, a
219
- // user@host, a slash-less special scheme ("https:host"), or an opaque
230
+ }
231
+
232
+ if (indexOfProtocol === -1 && url.charCodeAt(start) !== 91 /* '[' */) {
233
+ // Cold path: no "scheme://" prefix, and not a bare IPv6 literal (whose
234
+ // first ':' would otherwise look like a scheme separator; "[…]" falls
235
+ // through to the ipv6 handling below). May be a bare host, a host:port,
236
+ // a user@host, a slash-less special scheme ("https:host"), or an opaque
220
237
  // URI ("mailto:", "tel:", "urn:…").
221
238
  let indexOfColon = -1;
222
239
  for (let i = start; i < end; i += 1) {
@@ -274,7 +291,7 @@ export default function extractHostname(
274
291
  if (!allDigits) {
275
292
  const special = getSpecialScheme(url, start, indexOfColon);
276
293
  if (special === 0) {
277
- // No "://" anywhere on the cold path and not a special scheme.
294
+ // No "scheme://" on the cold path and not a special scheme.
278
295
  // A second ':' before the host's end marks a bare, unbracketed
279
296
  // IPv6 literal ("2a01:e35::1"): fall through and let the host
280
297
  // loop + isIp classify it. Without one this is an opaque path
package/src/factory.ts CHANGED
@@ -12,6 +12,7 @@ import extractHostname, {
12
12
  import isIp from './is-ip';
13
13
  import isSpecialUse from './is-special-use';
14
14
  import isValidHostname from './is-valid';
15
+ import extraSuffixLookup from './lookup/extra-suffixes';
15
16
  import { IPublicSuffix, ISuffixLookupOptions } from './lookup/interface';
16
17
  import { IOptions, setDefaults } from './options';
17
18
  import getSubdomain from './subdomain';
@@ -91,7 +92,7 @@ export function parseImpl(
91
92
  _1: string,
92
93
  _2: ISuffixLookupOptions,
93
94
  _3: IPublicSuffix,
94
- ) => void,
95
+ ) => unknown,
95
96
  partialOptions: Partial<IOptions> | undefined,
96
97
  result: IResult,
97
98
  ): IResult {
@@ -171,8 +172,11 @@ export function parseImpl(
171
172
  result.isSpecialUse = isSpecialUse(result.hostname);
172
173
  }
173
174
 
174
- // Extract public suffix
175
- suffixLookup(result.hostname, options, result);
175
+ // Extract public suffix (`isException`: the winner is an exception rule)
176
+ const isException = suffixLookup(result.hostname, options, result);
177
+ if (options.extraSuffixes !== null) {
178
+ extraSuffixLookup(result.hostname, options, isException === true, result);
179
+ }
176
180
  if (step === FLAG.PUBLIC_SUFFIX || result.publicSuffix === null) {
177
181
  return result;
178
182
  }
@@ -0,0 +1,98 @@
1
+ import { shareSameDomainSuffix } from '../domain';
2
+ import { IOptions } from '../options';
3
+ import { IPublicSuffix } from './interface';
4
+
5
+ /**
6
+ * Apply `options.extraSuffixes` to the built-in lookup result in `out`, as if
7
+ * they were part of the list (`isException`: the built-in winner is an
8
+ * exception rule). As in browsers, the longest match prevails and an exception
9
+ * beats a rule of the same length. Remaining ties go to the built-in match
10
+ * (unless it matched no rule: both flags falsy), then to `icann`.
11
+ */
12
+ export default function extraSuffixLookup(
13
+ hostname: string,
14
+ options: IOptions,
15
+ isException: boolean,
16
+ out: IPublicSuffix,
17
+ ): void {
18
+ const suffix = out.publicSuffix;
19
+ if (suffix === null) {
20
+ return;
21
+ }
22
+
23
+ // Best extra exception and rule, as start indices in `hostname` (smaller is
24
+ // longer; `none` if unmatched), and whether each is ICANN.
25
+ const none = hostname.length + 1;
26
+ let exception = none;
27
+ let exceptionIsIcann = false;
28
+ let rule = none;
29
+ let ruleIsIcann = false;
30
+ const extra = options.extraSuffixes!;
31
+ for (let section = 0; section < 2; section += 1) {
32
+ const isIcann = section === 0;
33
+ const rules = isIcann
34
+ ? options.allowIcannDomains && extra.icann
35
+ : options.allowPrivateDomains && extra.private;
36
+ if (!Array.isArray(rules)) {
37
+ continue;
38
+ }
39
+ for (const r of rules) {
40
+ // Sparse or untyped lists may hold non-strings.
41
+ if (typeof r !== 'string') {
42
+ continue;
43
+ }
44
+ const exceptionRule = r.startsWith('!');
45
+ const wildcard = r.startsWith('*.');
46
+ const body = exceptionRule ? r.slice(1) : wildcard ? r.slice(2) : r;
47
+ // Skip empty rules, and exceptions without a parent ('!p').
48
+ if (
49
+ body === '' ||
50
+ (exceptionRule && !body.includes('.')) ||
51
+ !shareSameDomainSuffix(hostname, body)
52
+ ) {
53
+ continue;
54
+ }
55
+ let start = hostname.length - body.length;
56
+ // Strict `<`: on a tie, the first match (`icann` first) wins.
57
+ if (exceptionRule) {
58
+ if (start < exception) {
59
+ exception = start;
60
+ exceptionIsIcann = isIcann;
61
+ }
62
+ continue;
63
+ }
64
+ // `*.p` also matches `p`, else one more label (possibly empty: '.p').
65
+ if (wildcard && start !== 0) {
66
+ start = start === 1 ? 0 : hostname.lastIndexOf('.', start - 2) + 1;
67
+ }
68
+ if (start < rule) {
69
+ rule = start;
70
+ ruleIsIcann = isIcann;
71
+ }
72
+ }
73
+ }
74
+
75
+ // Start of the built-in match: an exception starts one label left of its
76
+ // suffix.
77
+ const suffixStart = hostname.length - suffix.length;
78
+ const builtin = isException
79
+ ? hostname.lastIndexOf('.', suffixStart - 2) + 1
80
+ : suffixStart;
81
+ // An exception wins ties with rules; a built-in exception wins ties with
82
+ // an extra one.
83
+ if (
84
+ exception <= rule &&
85
+ (isException ? exception < builtin : exception <= builtin)
86
+ ) {
87
+ out.publicSuffix = hostname.slice(hostname.indexOf('.', exception) + 1);
88
+ out.isIcann = exceptionIsIcann;
89
+ out.isPrivate = !exceptionIsIcann;
90
+ } else if (
91
+ rule < builtin ||
92
+ (rule === builtin && !out.isIcann && !out.isPrivate)
93
+ ) {
94
+ out.publicSuffix = hostname.slice(rule);
95
+ out.isIcann = ruleIsIcann;
96
+ out.isPrivate = !ruleIsIcann;
97
+ }
98
+ }
package/src/options.ts CHANGED
@@ -6,6 +6,11 @@ export interface IOptions {
6
6
  // `isSpecialUse` on `parse()`. Off by default so the common path stays
7
7
  // allocation-free with no extra work; enable it to populate the field.
8
8
  detectSpecialUse: boolean;
9
+ // Extra public suffix rules, in PSL syntax, per section (default: null)
10
+ extraSuffixes: {
11
+ icann?: readonly string[];
12
+ private?: readonly string[];
13
+ } | null;
9
14
  extractHostname: boolean;
10
15
  mixedInputs: boolean;
11
16
  validHosts: string[] | null;
@@ -17,6 +22,7 @@ function setDefaultsImpl({
17
22
  allowPrivateDomains = false,
18
23
  detectIp = true,
19
24
  detectSpecialUse = false,
25
+ extraSuffixes = null,
20
26
  extractHostname = true,
21
27
  mixedInputs = true,
22
28
  validHosts = null,
@@ -27,6 +33,7 @@ function setDefaultsImpl({
27
33
  allowPrivateDomains,
28
34
  detectIp,
29
35
  detectSpecialUse,
36
+ extraSuffixes,
30
37
  extractHostname,
31
38
  mixedInputs,
32
39
  validHosts,