parse-filter.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // @ts-check
  2. import { fetchRemoteTextByLine } from './fetch-text-by-line';
  3. import { NetworkFilter } from '@cliqz/adblocker';
  4. import { processLine } from './process-line';
  5. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  6. import type { PublicSuffixList } from '@gorhill/publicsuffixlist';
  7. import picocolors from 'picocolors';
  8. import { normalizeDomain } from './normalize-domain';
  9. import { fetchAssets } from './fetch-assets';
  10. import { deserializeArray, deserializeSet, fsFetchCache, serializeArray, serializeSet } from './cache-filesystem';
  11. import type { Span } from '../trace';
  12. import createKeywordFilter from './aho-corasick';
  13. const DEBUG_DOMAIN_TO_FIND: string | null = null; // example.com | null
  14. let foundDebugDomain = false;
  15. const temporaryBypass = DEBUG_DOMAIN_TO_FIND !== null;
  16. export function processDomainLists(span: Span, domainListsUrl: string, includeAllSubDomain = false, ttl: number | null = null) {
  17. return span.traceChild(`process domainlist: ${domainListsUrl}`).traceAsyncFn(() => fsFetchCache.apply(
  18. domainListsUrl,
  19. async () => {
  20. const domainSets: string[] = [];
  21. for await (const line of await fetchRemoteTextByLine(domainListsUrl)) {
  22. let domainToAdd = processLine(line);
  23. if (!domainToAdd) continue;
  24. domainToAdd = normalizeDomain(domainToAdd);
  25. if (!domainToAdd) continue;
  26. if (DEBUG_DOMAIN_TO_FIND && domainToAdd.includes(DEBUG_DOMAIN_TO_FIND)) {
  27. console.warn(picocolors.red(domainListsUrl), '(black)', domainToAdd.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND)));
  28. foundDebugDomain = true;
  29. }
  30. domainSets.push(includeAllSubDomain ? `.${domainToAdd}` : domainToAdd);
  31. }
  32. return domainSets;
  33. },
  34. {
  35. ttl,
  36. temporaryBypass,
  37. serializer: serializeArray,
  38. deserializer: deserializeArray
  39. }
  40. ));
  41. }
  42. const hostsLineCb = (l: string, set: string[], includeAllSubDomain: boolean, meta: string) => {
  43. const line = processLine(l);
  44. if (!line) {
  45. return;
  46. }
  47. const _domain = line.split(/\s/)[1]?.trim();
  48. if (!_domain) {
  49. return;
  50. }
  51. const domain = normalizeDomain(_domain);
  52. if (!domain) {
  53. return;
  54. }
  55. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  56. console.warn(picocolors.red(meta), '(black)', domain.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND)));
  57. foundDebugDomain = true;
  58. }
  59. set.push(includeAllSubDomain ? `.${domain}` : domain);
  60. };
  61. export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false, ttl: number | null = null) {
  62. return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn((childSpan) => fsFetchCache.apply(
  63. hostsUrl,
  64. async () => {
  65. const domainSets: string[] = [];
  66. if (mirrors == null || mirrors.length === 0) {
  67. for await (const l of await fetchRemoteTextByLine(hostsUrl)) {
  68. hostsLineCb(l, domainSets, includeAllSubDomain, hostsUrl);
  69. }
  70. } else {
  71. const filterRules = await childSpan
  72. .traceChild('download hosts')
  73. .traceAsyncFn(() => fetchAssets(hostsUrl, mirrors).then(text => text.split('\n')));
  74. childSpan.traceChild('parse hosts').traceSyncFn(() => {
  75. for (let i = 0, len = filterRules.length; i < len; i++) {
  76. hostsLineCb(filterRules[i], domainSets, includeAllSubDomain, hostsUrl);
  77. }
  78. });
  79. }
  80. return domainSets;
  81. },
  82. {
  83. ttl,
  84. temporaryBypass,
  85. serializer: serializeArray,
  86. deserializer: deserializeArray
  87. }
  88. ));
  89. }
  90. // eslint-disable-next-line sukka-ts/no-const-enum -- bun bundler is smart, maybe?
  91. const enum ParseType {
  92. WhiteIncludeSubdomain = 0,
  93. WhiteAbsolute = -1,
  94. BlackAbsolute = 1,
  95. BlackIncludeSubdomain = 2,
  96. ErrorMessage = 10
  97. }
  98. export async function processFilterRules(
  99. parentSpan: Span,
  100. filterRulesUrl: string,
  101. fallbackUrls?: readonly string[] | undefined | null,
  102. ttl: number | null = null
  103. ): Promise<{ white: string[], black: string[], foundDebugDomain: boolean }> {
  104. const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn((span) => fsFetchCache.apply<Readonly<[
  105. white: string[],
  106. black: string[],
  107. warningMessages: string[]
  108. ]>>(
  109. filterRulesUrl,
  110. async () => {
  111. const whitelistDomainSets = new Set<string>();
  112. const blacklistDomainSets = new Set<string>();
  113. const warningMessages: string[] = [];
  114. const gorhillPromise = getGorhillPublicSuffixPromise();
  115. const peekedGorhill = Bun.peek(gorhillPromise);
  116. const gorhill = peekedGorhill === gorhillPromise
  117. ? await span.traceChild('get gorhill').tracePromise(gorhillPromise)
  118. : (peekedGorhill as PublicSuffixList);
  119. /**
  120. * @param {string} line
  121. */
  122. const lineCb = (line: string) => {
  123. const result = parse(line, gorhill);
  124. if (!result) {
  125. return;
  126. }
  127. const flag = result[1];
  128. const hostname = result[0];
  129. if (DEBUG_DOMAIN_TO_FIND) {
  130. if (hostname.includes(DEBUG_DOMAIN_TO_FIND)) {
  131. console.warn(
  132. picocolors.red(filterRulesUrl),
  133. flag === ParseType.WhiteIncludeSubdomain || flag === ParseType.WhiteAbsolute
  134. ? '(white)'
  135. : '(black)',
  136. hostname.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND))
  137. );
  138. foundDebugDomain = true;
  139. }
  140. }
  141. switch (flag) {
  142. case ParseType.WhiteIncludeSubdomain:
  143. if (hostname[0] !== '.') {
  144. whitelistDomainSets.add(`.${hostname}`);
  145. } else {
  146. whitelistDomainSets.add(hostname);
  147. }
  148. break;
  149. case ParseType.WhiteAbsolute:
  150. whitelistDomainSets.add(hostname);
  151. break;
  152. case ParseType.BlackAbsolute:
  153. blacklistDomainSets.add(hostname);
  154. break;
  155. case ParseType.BlackIncludeSubdomain:
  156. if (hostname[0] !== '.') {
  157. blacklistDomainSets.add(`.${hostname}`);
  158. } else {
  159. blacklistDomainSets.add(hostname);
  160. }
  161. break;
  162. case ParseType.ErrorMessage:
  163. warningMessages.push(hostname);
  164. break;
  165. default:
  166. break;
  167. }
  168. };
  169. if (!fallbackUrls || fallbackUrls.length === 0) {
  170. for await (const line of await fetchRemoteTextByLine(filterRulesUrl)) {
  171. // don't trim here
  172. lineCb(line);
  173. }
  174. } else {
  175. const filterRules = await span.traceChild('download adguard filter').traceAsyncFn(() => {
  176. return fetchAssets(filterRulesUrl, fallbackUrls).then(text => text.split('\n'));
  177. });
  178. span.traceChild('parse adguard filter').traceSyncFn(() => {
  179. for (let i = 0, len = filterRules.length; i < len; i++) {
  180. lineCb(filterRules[i]);
  181. }
  182. });
  183. }
  184. return [
  185. Array.from(whitelistDomainSets),
  186. Array.from(blacklistDomainSets),
  187. warningMessages
  188. ] as const;
  189. },
  190. {
  191. ttl,
  192. temporaryBypass,
  193. serializer: JSON.stringify,
  194. deserializer: JSON.parse
  195. }
  196. ));
  197. for (let i = 0, len = warningMessages.length; i < len; i++) {
  198. console.warn(
  199. picocolors.yellow(warningMessages[i]),
  200. picocolors.gray(picocolors.underline(filterRulesUrl))
  201. );
  202. }
  203. console.log(
  204. picocolors.gray('[process filter]'),
  205. picocolors.gray(filterRulesUrl),
  206. picocolors.gray(`white: ${white.length}`),
  207. picocolors.gray(`black: ${black.length}`)
  208. );
  209. return {
  210. white,
  211. black,
  212. foundDebugDomain
  213. };
  214. }
  215. // const R_KNOWN_NOT_NETWORK_FILTER_PATTERN_2 = /(\$popup|\$removeparam|\$popunder|\$cname)/;
  216. // cname exceptional filter can not be parsed by NetworkFilter
  217. // Surge / Clash can't handle CNAME either, so we just ignore them
  218. const kwfilter = createKeywordFilter([
  219. '!',
  220. '?',
  221. '*',
  222. '[',
  223. '(',
  224. ']',
  225. ')',
  226. ',',
  227. '#',
  228. '%',
  229. '&',
  230. '=',
  231. '~',
  232. // special modifier
  233. '$popup',
  234. '$removeparam',
  235. '$popunder',
  236. '$cname'
  237. ]);
  238. function parse($line: string, gorhill: PublicSuffixList): null | [hostname: string, flag: ParseType] {
  239. if (
  240. // doesn't include
  241. !$line.includes('.') // rule with out dot can not be a domain
  242. // includes
  243. || kwfilter($line)
  244. ) {
  245. return null;
  246. }
  247. const line = $line.trim();
  248. /** @example line.length */
  249. const len = line.length;
  250. if (len === 0) {
  251. return null;
  252. }
  253. const firstCharCode = line[0].charCodeAt(0);
  254. const lastCharCode = line[len - 1].charCodeAt(0);
  255. if (
  256. firstCharCode === 47 // 47 `/`
  257. // ends with
  258. || lastCharCode === 46 // 46 `.`, line.endsWith('.')
  259. || lastCharCode === 45 // 45 `-`, line.endsWith('-')
  260. || lastCharCode === 95 // 95 `_`, line.endsWith('_')
  261. // || line.includes('$popup')
  262. // || line.includes('$removeparam')
  263. // || line.includes('$popunder')
  264. ) {
  265. return null;
  266. }
  267. if ((line.includes('/') || line.includes(':')) && !line.includes('://')) {
  268. return null;
  269. }
  270. const filter = NetworkFilter.parse(line);
  271. if (filter) {
  272. if (
  273. // filter.isCosmeticFilter() // always false
  274. // filter.isNetworkFilter() // always true
  275. filter.isElemHide()
  276. || filter.isGenericHide()
  277. || filter.isSpecificHide()
  278. || filter.isRedirect()
  279. || filter.isRedirectRule()
  280. || filter.hasDomains()
  281. || filter.isCSP() // must not be csp rule
  282. || (!filter.fromAny() && !filter.fromDocument())
  283. ) {
  284. // not supported type
  285. return null;
  286. }
  287. if (
  288. filter.hostname // filter.hasHostname() // must have
  289. && filter.isPlain() // isPlain() === !isRegex()
  290. && (!filter.isFullRegex())
  291. ) {
  292. const hostname = normalizeDomain(filter.hostname);
  293. if (!hostname) {
  294. return null;
  295. }
  296. // |: filter.isHostnameAnchor(),
  297. // |: filter.isLeftAnchor(),
  298. // |https://: !filter.isHostnameAnchor() && (filter.fromHttps() || filter.fromHttp())
  299. const isIncludeAllSubDomain = filter.isHostnameAnchor();
  300. if (filter.isException() || filter.isBadFilter()) {
  301. return [hostname, isIncludeAllSubDomain ? ParseType.WhiteIncludeSubdomain : ParseType.WhiteAbsolute];
  302. }
  303. const _1p = filter.firstParty();
  304. const _3p = filter.thirdParty();
  305. if (_1p) {
  306. if (_1p === _3p) {
  307. return [hostname, isIncludeAllSubDomain ? ParseType.BlackIncludeSubdomain : ParseType.BlackAbsolute];
  308. }
  309. return null;
  310. }
  311. if (_3p) {
  312. return null;
  313. }
  314. }
  315. }
  316. // After NetworkFilter.parse, it means the line can not be parsed by cliqz NetworkFilter
  317. // We now need to "salvage" the line as much as possible
  318. /*
  319. * From now on, we are mostly facing non-standard domain rules (some are regex like)
  320. * We first skip third-party and frame rules, as Surge / Clash can't handle them
  321. *
  322. * `.sharecounter.$third-party`
  323. * `.bbelements.com^$third-party`
  324. * `://o0e.ru^$third-party`
  325. * `.1.1.1.l80.js^$third-party`
  326. */
  327. if (line.includes('$third-party') || line.includes('$frame')) {
  328. return null;
  329. }
  330. /** @example line.endsWith('^') */
  331. const lineEndsWithCaret = lastCharCode === 94; // lastChar === '^';
  332. /** @example line.endsWith('^|') */
  333. const lineEndsWithCaretVerticalBar = (lastCharCode === 124 /** lastChar === '|' */) && line[len - 2] === '^';
  334. /** @example line.endsWith('^') || line.endsWith('^|') */
  335. const lineEndsWithCaretOrCaretVerticalBar = lineEndsWithCaret || lineEndsWithCaretVerticalBar;
  336. // whitelist (exception)
  337. if (
  338. firstCharCode === 64 // 64 `@`
  339. && line[1] === '@'
  340. ) {
  341. let whiteIncludeAllSubDomain = true;
  342. /**
  343. * Some "malformed" regex-based filters can not be parsed by NetworkFilter
  344. * "$genericblock`" is also not supported by NetworkFilter, see:
  345. * https://github.com/ghostery/adblocker/blob/62caf7786ba10ef03beffecd8cd4eec111bcd5ec/packages/adblocker/test/parsing.test.ts#L950
  346. *
  347. * `@@||cmechina.net^$genericblock`
  348. * `@@|ftp.bmp.ovh^|`
  349. * `@@|adsterra.com^|`
  350. * `@@.atlassian.net$document`
  351. * `@@||ad.alimama.com^$genericblock`
  352. */
  353. let sliceStart = 0;
  354. let sliceEnd: number | undefined;
  355. if (line[2] === '|') { // line.startsWith('@@|')
  356. sliceStart = 3;
  357. whiteIncludeAllSubDomain = false;
  358. if (line[3] === '|') { // line.startsWith('@@||')
  359. sliceStart = 4;
  360. whiteIncludeAllSubDomain = true;
  361. }
  362. } else if (line[2] === '.') { // line.startsWith('@@.')
  363. sliceStart = 3;
  364. whiteIncludeAllSubDomain = true;
  365. } else if (
  366. /**
  367. * line.startsWith('@@://')
  368. *
  369. * `@@://googleadservices.com^|`
  370. * `@@://www.googleadservices.com^|`
  371. */
  372. line[2] === ':' && line[3] === '/' && line[4] === '/'
  373. ) {
  374. whiteIncludeAllSubDomain = false;
  375. sliceStart = 5;
  376. }
  377. if (lineEndsWithCaretOrCaretVerticalBar) {
  378. sliceEnd = -2;
  379. } else if (line.endsWith('$genericblock')) {
  380. sliceEnd = -13;
  381. if (line[len - 14] === '^') { // line.endsWith('^$genericblock')
  382. sliceEnd = -14;
  383. }
  384. } else if (line.endsWith('$document')) {
  385. sliceEnd = -9;
  386. if (line[len - 10] === '^') { // line.endsWith('^$document')
  387. sliceEnd = -10;
  388. }
  389. }
  390. if (sliceStart !== 0 || sliceEnd !== undefined) {
  391. const sliced = line.slice(sliceStart, sliceEnd);
  392. const domain = normalizeDomain(sliced);
  393. if (domain) {
  394. return [domain, whiteIncludeAllSubDomain ? ParseType.WhiteIncludeSubdomain : ParseType.WhiteAbsolute];
  395. }
  396. return [
  397. `[parse-filter E0001] (white) invalid domain: ${JSON.stringify({
  398. line, sliced, sliceStart, sliceEnd
  399. })}`,
  400. ParseType.ErrorMessage
  401. ];
  402. }
  403. return [
  404. `[parse-filter E0006] (white) failed to parse: ${JSON.stringify({
  405. line, sliceStart, sliceEnd
  406. })}`,
  407. ParseType.ErrorMessage
  408. ];
  409. }
  410. if (
  411. // 124 `|`
  412. // line.startsWith('|')
  413. firstCharCode === 124
  414. && lineEndsWithCaretOrCaretVerticalBar
  415. ) {
  416. /**
  417. * Some malformed filters can not be parsed by NetworkFilter:
  418. *
  419. * `||smetrics.teambeachbody.com^.com^`
  420. * `||solutions.|pages.indigovision.com^`
  421. * `||vystar..0rg@client.iebetanialaargentina.edu.co^`
  422. * `app-uat.latrobehealth.com.au^predirect.snapdeal.com`
  423. */
  424. const includeAllSubDomain = line[1] === '|';
  425. const sliceStart = includeAllSubDomain ? 2 : 1;
  426. const sliceEnd = lineEndsWithCaret
  427. ? -1
  428. : (lineEndsWithCaretVerticalBar ? -2 : undefined);
  429. const sliced = line.slice(sliceStart, sliceEnd); // we already make sure line startsWith "|"
  430. const domain = normalizeDomain(sliced);
  431. if (domain) {
  432. return [domain, includeAllSubDomain ? ParseType.BlackIncludeSubdomain : ParseType.BlackAbsolute];
  433. }
  434. return [
  435. `[parse-filter E0002] (black) invalid domain: ${sliced}`,
  436. ParseType.ErrorMessage
  437. ];
  438. }
  439. const lineStartsWithSingleDot = firstCharCode === 46; // 46 `.`
  440. if (
  441. lineStartsWithSingleDot
  442. && lineEndsWithCaretOrCaretVerticalBar
  443. ) {
  444. /**
  445. * `.ay.delivery^`
  446. * `.m.bookben.com^`
  447. * `.wap.x4399.com^`
  448. */
  449. const sliced = line.slice(
  450. 1, // remove prefix dot
  451. lineEndsWithCaret // replaceAll('^', '')
  452. ? -1
  453. : (lineEndsWithCaretVerticalBar ? -2 : undefined) // replace('^|', '')
  454. );
  455. const suffix = gorhill.getPublicSuffix(sliced);
  456. if (!gorhill.suffixInPSL(suffix)) {
  457. // This exclude domain-like resource like `1.1.4.514.js`
  458. return null;
  459. }
  460. const domain = normalizeDomain(sliced);
  461. if (domain) {
  462. return [domain, ParseType.BlackIncludeSubdomain];
  463. }
  464. return [
  465. `[paparse-filter E0003] (black) invalid domain: ${sliced}`,
  466. ParseType.ErrorMessage
  467. ];
  468. }
  469. /**
  470. * `|http://x.o2.pl^`
  471. * `://mine.torrent.pw^`
  472. * `://say.ac^`
  473. */
  474. if (lineEndsWithCaretOrCaretVerticalBar) {
  475. let sliceStart = 0;
  476. let sliceEnd;
  477. if (lineEndsWithCaret) { // line.endsWith('^')
  478. sliceEnd = -1;
  479. } else if (lineEndsWithCaretVerticalBar) { // line.endsWith('^|')
  480. sliceEnd = -2;
  481. }
  482. if (line.startsWith('://')) {
  483. sliceStart = 3;
  484. } else if (line.startsWith('http://')) {
  485. sliceStart = 7;
  486. } else if (line.startsWith('https://')) {
  487. sliceStart = 8;
  488. } else if (line.startsWith('|http://')) {
  489. sliceStart = 8;
  490. } else if (line.startsWith('|https://')) {
  491. sliceStart = 9;
  492. }
  493. if (sliceStart !== 0 || sliceEnd !== undefined) {
  494. const sliced = line.slice(sliceStart, sliceEnd);
  495. const domain = normalizeDomain(sliced);
  496. if (domain) {
  497. return [domain, ParseType.BlackIncludeSubdomain];
  498. }
  499. return [
  500. `[parse-filter E0004] (black) invalid domain: ${JSON.stringify({
  501. line, sliced, sliceStart, sliceEnd
  502. })}`,
  503. ParseType.ErrorMessage
  504. ];
  505. }
  506. }
  507. /**
  508. * `_vmind.qqvideo.tc.qq.com^`
  509. * `arketing.indianadunes.com^`
  510. * `charlestownwyllie.oaklawnnonantum.com^`
  511. * `-telemetry.officeapps.live.com^`
  512. * `-tracker.biliapi.net`
  513. * `-logging.nextmedia.com`
  514. * `_social_tracking.js^`
  515. */
  516. if (
  517. firstCharCode !== 124 // 124 `|`
  518. && lastCharCode === 94 // 94 `^`
  519. ) {
  520. const _domain = line.slice(0, -1);
  521. const suffix = gorhill.getPublicSuffix(_domain);
  522. if (!suffix || !gorhill.suffixInPSL(suffix)) {
  523. // This exclude domain-like resource like `_social_tracking.js^`
  524. return null;
  525. }
  526. const domain = normalizeDomain(_domain);
  527. if (domain) {
  528. return [domain, ParseType.BlackAbsolute];
  529. }
  530. return [
  531. `[parse-filter E0005] (black) invalid domain: ${_domain}`,
  532. ParseType.ErrorMessage
  533. ];
  534. }
  535. // Possibly that entire rule is domain
  536. /**
  537. * lineStartsWithSingleDot:
  538. *
  539. * `.cookielaw.js`
  540. * `.content_tracking.js`
  541. * `.ads.css`
  542. *
  543. * else:
  544. *
  545. * `_prebid.js`
  546. * `t.yesware.com`
  547. * `ubmcmm.baidustatic.com`
  548. * `://www.smfg-card.$document`
  549. * `portal.librus.pl$$advertisement-module`
  550. * `@@-ds.metric.gstatic.com^|`
  551. * `://gom.ge/cookie.js`
  552. * `://accout-update-smba.jp.$document`
  553. * `_200x250.png`
  554. * `@@://www.liquidweb.com/kb/wp-content/themes/lw-kb-theme/images/ads/vps-sidebar.jpg`
  555. */
  556. let sliceStart = 0;
  557. let sliceEnd: number | undefined;
  558. if (lineStartsWithSingleDot) {
  559. sliceStart = 1;
  560. }
  561. if (line.endsWith('^$all')) { // This salvage line `thepiratebay3.com^$all`
  562. sliceEnd = -5;
  563. } else if (
  564. // Try to salvage line like `://account.smba.$document`
  565. // For this specific line, it will fail anyway though.
  566. line.endsWith('$document')
  567. ) {
  568. sliceEnd = -9;
  569. }
  570. const sliced = (sliceStart !== 0 || sliceEnd !== undefined) ? line.slice(sliceStart, sliceEnd) : line;
  571. const suffix = gorhill.getPublicSuffix(sliced);
  572. /**
  573. * Fast exclude definitely not domain-like resource
  574. *
  575. * `.gatracking.js`, suffix is `js`,
  576. * `.ads.css`, suffix is `css`,
  577. * `-cpm-ads.$badfilter`, suffix is `$badfilter`,
  578. * `portal.librus.pl$$advertisement-module`, suffix is `pl$$advertisement-module`
  579. */
  580. if (!suffix || !gorhill.suffixInPSL(suffix)) {
  581. // This exclude domain-like resource like `.gatracking.js`, `.beacon.min.js` and `.cookielaw.js`
  582. return null;
  583. }
  584. const tryNormalizeDomain = normalizeDomain(sliced);
  585. if (tryNormalizeDomain === sliced) {
  586. // the entire rule is domain
  587. return [sliced, ParseType.BlackIncludeSubdomain];
  588. }
  589. return [
  590. `[parse-filter E0010] can not parse: ${line}`,
  591. ParseType.ErrorMessage
  592. ];
  593. }