parse-filter.ts 19 KB

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