parse-filter.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. span: 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 span.traceChild('process filter rules: domainListsUrl').traceAsyncFn(() => 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 traceAsync(
  161. picocolors.gray(`- download ${filterRulesUrl}`),
  162. () => fetchAssets(filterRulesUrl, fallbackUrls),
  163. picocolors.gray
  164. )).split('\n');
  165. const key = picocolors.gray(`- parse adguard filter ${filterRulesUrl}`);
  166. console.time(key);
  167. for (let i = 0, len = filterRules.length; i < len; i++) {
  168. lineCb(filterRules[i]);
  169. }
  170. console.timeEnd(key);
  171. }
  172. return [
  173. Array.from(whitelistDomainSets),
  174. Array.from(blacklistDomainSets),
  175. warningMessages
  176. ] as const;
  177. },
  178. {
  179. ttl,
  180. temporaryBypass: DEBUG_DOMAIN_TO_FIND !== null,
  181. serializer: JSON.stringify,
  182. deserializer: JSON.parse
  183. }
  184. ));
  185. for (let i = 0, len = warningMessages.length; i < len; i++) {
  186. console.warn(
  187. picocolors.yellow(warningMessages[i]),
  188. picocolors.gray(picocolors.underline(filterRulesUrl))
  189. );
  190. }
  191. console.log(
  192. picocolors.gray('[process filter]'),
  193. picocolors.gray(filterRulesUrl),
  194. picocolors.gray(`white: ${white.length}`),
  195. picocolors.gray(`black: ${black.length}`)
  196. );
  197. return {
  198. white,
  199. black,
  200. foundDebugDomain
  201. };
  202. }
  203. const R_KNOWN_NOT_NETWORK_FILTER_PATTERN = /[#%&=~]/;
  204. const R_KNOWN_NOT_NETWORK_FILTER_PATTERN_2 = /(\$popup|\$removeparam|\$popunder|\$cname)/;
  205. // cname exceptional filter can not be parsed by NetworkFilter
  206. // Surge / Clash can't handle CNAME either, so we just ignore them
  207. function parse($line: string, gorhill: PublicSuffixList): null | [hostname: string, flag: ParseType] {
  208. if (
  209. // doesn't include
  210. !$line.includes('.') // rule with out dot can not be a domain
  211. // includes
  212. || $line.includes('!')
  213. || $line.includes('?')
  214. || $line.includes('*')
  215. || $line.includes('[')
  216. || $line.includes('(')
  217. || $line.includes(']')
  218. || $line.includes(')')
  219. || $line.includes(',')
  220. || R_KNOWN_NOT_NETWORK_FILTER_PATTERN.test($line)
  221. ) {
  222. return null;
  223. }
  224. const line = $line.trim();
  225. /** @example line.length */
  226. const len = line.length;
  227. if (len === 0) {
  228. return null;
  229. }
  230. const firstCharCode = line[0].charCodeAt(0);
  231. const lastCharCode = line[len - 1].charCodeAt(0);
  232. if (
  233. firstCharCode === 47 // 47 `/`
  234. // ends with
  235. || lastCharCode === 46 // 46 `.`, line.endsWith('.')
  236. || lastCharCode === 45 // 45 `-`, line.endsWith('-')
  237. || lastCharCode === 95 // 95 `_`, line.endsWith('_')
  238. // special modifier
  239. || R_KNOWN_NOT_NETWORK_FILTER_PATTERN_2.test(line)
  240. // || line.includes('$popup')
  241. // || line.includes('$removeparam')
  242. // || line.includes('$popunder')
  243. ) {
  244. return null;
  245. }
  246. if ((line.includes('/') || line.includes(':')) && !line.includes('://')) {
  247. return null;
  248. }
  249. const filter = NetworkFilter.parse(line);
  250. if (filter) {
  251. if (
  252. // filter.isCosmeticFilter() // always false
  253. // filter.isNetworkFilter() // always true
  254. filter.isElemHide()
  255. || filter.isGenericHide()
  256. || filter.isSpecificHide()
  257. || filter.isRedirect()
  258. || filter.isRedirectRule()
  259. || filter.hasDomains()
  260. || filter.isCSP() // must not be csp rule
  261. || (!filter.fromAny() && !filter.fromDocument())
  262. ) {
  263. // not supported type
  264. return null;
  265. }
  266. if (
  267. filter.hostname // filter.hasHostname() // must have
  268. && filter.isPlain() // isPlain() === !isRegex()
  269. && (!filter.isFullRegex())
  270. ) {
  271. const hostname = normalizeDomain(filter.hostname);
  272. if (!hostname) {
  273. return null;
  274. }
  275. // |: filter.isHostnameAnchor(),
  276. // |: filter.isLeftAnchor(),
  277. // |https://: !filter.isHostnameAnchor() && (filter.fromHttps() || filter.fromHttp())
  278. const isIncludeAllSubDomain = filter.isHostnameAnchor();
  279. if (filter.isException() || filter.isBadFilter()) {
  280. return [hostname, isIncludeAllSubDomain ? ParseType.WhiteIncludeSubdomain : ParseType.WhiteAbsolute];
  281. }
  282. const _1p = filter.firstParty();
  283. const _3p = filter.thirdParty();
  284. if (_1p) {
  285. if (_1p === _3p) {
  286. return [hostname, isIncludeAllSubDomain ? ParseType.BlackIncludeSubdomain : ParseType.BlackAbsolute];
  287. }
  288. return null;
  289. }
  290. if (_3p) {
  291. return null;
  292. }
  293. }
  294. }
  295. // After NetworkFilter.parse, it means the line can not be parsed by cliqz NetworkFilter
  296. // We now need to "salvage" the line as much as possible
  297. /*
  298. * From now on, we are mostly facing non-standard domain rules (some are regex like)
  299. * We first skip third-party and frame rules, as Surge / Clash can't handle them
  300. *
  301. * `.sharecounter.$third-party`
  302. * `.bbelements.com^$third-party`
  303. * `://o0e.ru^$third-party`
  304. * `.1.1.1.l80.js^$third-party`
  305. */
  306. if (line.includes('$third-party') || line.includes('$frame')) {
  307. return null;
  308. }
  309. /** @example line.endsWith('^') */
  310. const lineEndsWithCaret = lastCharCode === 94; // lastChar === '^';
  311. /** @example line.endsWith('^|') */
  312. const lineEndsWithCaretVerticalBar = (lastCharCode === 124 /** lastChar === '|' */) && line[len - 2] === '^';
  313. /** @example line.endsWith('^') || line.endsWith('^|') */
  314. const lineEndsWithCaretOrCaretVerticalBar = lineEndsWithCaret || lineEndsWithCaretVerticalBar;
  315. // whitelist (exception)
  316. if (
  317. firstCharCode === 64 // 64 `@`
  318. && line[1] === '@'
  319. ) {
  320. let whiteIncludeAllSubDomain = true;
  321. /**
  322. * Some "malformed" regex-based filters can not be parsed by NetworkFilter
  323. * "$genericblock`" is also not supported by NetworkFilter, see:
  324. * https://github.com/ghostery/adblocker/blob/62caf7786ba10ef03beffecd8cd4eec111bcd5ec/packages/adblocker/test/parsing.test.ts#L950
  325. *
  326. * `@@||cmechina.net^$genericblock`
  327. * `@@|ftp.bmp.ovh^|`
  328. * `@@|adsterra.com^|`
  329. * `@@.atlassian.net$document`
  330. * `@@||ad.alimama.com^$genericblock`
  331. */
  332. let sliceStart = 0;
  333. let sliceEnd: number | undefined;
  334. if (line[2] === '|') { // line.startsWith('@@|')
  335. sliceStart = 3;
  336. whiteIncludeAllSubDomain = false;
  337. if (line[3] === '|') { // line.startsWith('@@||')
  338. sliceStart = 4;
  339. whiteIncludeAllSubDomain = true;
  340. }
  341. } else if (line[2] === '.') { // line.startsWith('@@.')
  342. sliceStart = 3;
  343. whiteIncludeAllSubDomain = true;
  344. } else if (
  345. /**
  346. * line.startsWith('@@://')
  347. *
  348. * `@@://googleadservices.com^|`
  349. * `@@://www.googleadservices.com^|`
  350. */
  351. line[2] === ':' && line[3] === '/' && line[4] === '/'
  352. ) {
  353. whiteIncludeAllSubDomain = false;
  354. sliceStart = 5;
  355. }
  356. if (lineEndsWithCaretOrCaretVerticalBar) {
  357. sliceEnd = -2;
  358. } else if (line.endsWith('$genericblock')) {
  359. sliceEnd = -13;
  360. if (line[len - 14] === '^') { // line.endsWith('^$genericblock')
  361. sliceEnd = -14;
  362. }
  363. } else if (line.endsWith('$document')) {
  364. sliceEnd = -9;
  365. if (line[len - 10] === '^') { // line.endsWith('^$document')
  366. sliceEnd = -10;
  367. }
  368. }
  369. if (sliceStart !== 0 || sliceEnd !== undefined) {
  370. const sliced = line.slice(sliceStart, sliceEnd);
  371. const domain = normalizeDomain(sliced);
  372. if (domain) {
  373. return [domain, whiteIncludeAllSubDomain ? ParseType.WhiteIncludeSubdomain : ParseType.WhiteAbsolute];
  374. }
  375. return [
  376. `[parse-filter E0001] (white) invalid domain: ${JSON.stringify({
  377. line, sliced, sliceStart, sliceEnd
  378. })}`,
  379. ParseType.ErrorMessage
  380. ];
  381. }
  382. return [
  383. `[parse-filter E0006] (white) failed to parse: ${JSON.stringify({
  384. line, sliceStart, sliceEnd
  385. })}`,
  386. ParseType.ErrorMessage
  387. ];
  388. }
  389. if (
  390. // 124 `|`
  391. // line.startsWith('|')
  392. firstCharCode === 124
  393. && lineEndsWithCaretOrCaretVerticalBar
  394. ) {
  395. /**
  396. * Some malformed filters can not be parsed by NetworkFilter:
  397. *
  398. * `||smetrics.teambeachbody.com^.com^`
  399. * `||solutions.|pages.indigovision.com^`
  400. * `||vystar..0rg@client.iebetanialaargentina.edu.co^`
  401. * `app-uat.latrobehealth.com.au^predirect.snapdeal.com`
  402. */
  403. const includeAllSubDomain = line[1] === '|';
  404. const sliceStart = includeAllSubDomain ? 2 : 1;
  405. const sliceEnd = lineEndsWithCaret
  406. ? -1
  407. : (lineEndsWithCaretVerticalBar ? -2 : undefined);
  408. const sliced = line.slice(sliceStart, sliceEnd); // we already make sure line startsWith "|"
  409. const domain = normalizeDomain(sliced);
  410. if (domain) {
  411. return [domain, includeAllSubDomain ? ParseType.BlackIncludeSubdomain : ParseType.BlackAbsolute];
  412. }
  413. return [
  414. `[parse-filter E0002] (black) invalid domain: ${sliced}`,
  415. ParseType.ErrorMessage
  416. ];
  417. }
  418. const lineStartsWithSingleDot = firstCharCode === 46; // 46 `.`
  419. if (
  420. lineStartsWithSingleDot
  421. && lineEndsWithCaretOrCaretVerticalBar
  422. ) {
  423. /**
  424. * `.ay.delivery^`
  425. * `.m.bookben.com^`
  426. * `.wap.x4399.com^`
  427. */
  428. const sliced = line.slice(
  429. 1, // remove prefix dot
  430. lineEndsWithCaret // replaceAll('^', '')
  431. ? -1
  432. : (lineEndsWithCaretVerticalBar ? -2 : undefined) // replace('^|', '')
  433. );
  434. const suffix = gorhill.getPublicSuffix(sliced);
  435. if (!gorhill.suffixInPSL(suffix)) {
  436. // This exclude domain-like resource like `1.1.4.514.js`
  437. return null;
  438. }
  439. const domain = normalizeDomain(sliced);
  440. if (domain) {
  441. return [domain, ParseType.BlackIncludeSubdomain];
  442. }
  443. return [
  444. `[paparse-filter E0003] (black) invalid domain: ${sliced}`,
  445. ParseType.ErrorMessage
  446. ];
  447. }
  448. /**
  449. * `|http://x.o2.pl^`
  450. * `://mine.torrent.pw^`
  451. * `://say.ac^`
  452. */
  453. if (lineEndsWithCaretOrCaretVerticalBar) {
  454. let sliceStart = 0;
  455. let sliceEnd;
  456. if (lineEndsWithCaret) { // line.endsWith('^')
  457. sliceEnd = -1;
  458. } else if (lineEndsWithCaretVerticalBar) { // line.endsWith('^|')
  459. sliceEnd = -2;
  460. }
  461. if (line.startsWith('://')) {
  462. sliceStart = 3;
  463. } else if (line.startsWith('http://')) {
  464. sliceStart = 7;
  465. } else if (line.startsWith('https://')) {
  466. sliceStart = 8;
  467. } else if (line.startsWith('|http://')) {
  468. sliceStart = 8;
  469. } else if (line.startsWith('|https://')) {
  470. sliceStart = 9;
  471. }
  472. if (sliceStart !== 0 || sliceEnd !== undefined) {
  473. const sliced = line.slice(sliceStart, sliceEnd);
  474. const domain = normalizeDomain(sliced);
  475. if (domain) {
  476. return [domain, ParseType.BlackIncludeSubdomain];
  477. }
  478. return [
  479. `[parse-filter E0004] (black) invalid domain: ${JSON.stringify({
  480. line, sliced, sliceStart, sliceEnd
  481. })}`,
  482. ParseType.ErrorMessage
  483. ];
  484. }
  485. }
  486. /**
  487. * `_vmind.qqvideo.tc.qq.com^`
  488. * `arketing.indianadunes.com^`
  489. * `charlestownwyllie.oaklawnnonantum.com^`
  490. * `-telemetry.officeapps.live.com^`
  491. * `-tracker.biliapi.net`
  492. * `-logging.nextmedia.com`
  493. * `_social_tracking.js^`
  494. */
  495. if (
  496. firstCharCode !== 124 // 124 `|`
  497. && lastCharCode === 94 // 94 `^`
  498. ) {
  499. const _domain = line.slice(0, -1);
  500. const suffix = gorhill.getPublicSuffix(_domain);
  501. if (!suffix || !gorhill.suffixInPSL(suffix)) {
  502. // This exclude domain-like resource like `_social_tracking.js^`
  503. return null;
  504. }
  505. const domain = normalizeDomain(_domain);
  506. if (domain) {
  507. return [domain, ParseType.BlackAbsolute];
  508. }
  509. return [
  510. `[parse-filter E0005] (black) invalid domain: ${_domain}`,
  511. ParseType.ErrorMessage
  512. ];
  513. }
  514. // Possibly that entire rule is domain
  515. /**
  516. * lineStartsWithSingleDot:
  517. *
  518. * `.cookielaw.js`
  519. * `.content_tracking.js`
  520. * `.ads.css`
  521. *
  522. * else:
  523. *
  524. * `_prebid.js`
  525. * `t.yesware.com`
  526. * `ubmcmm.baidustatic.com`
  527. * `://www.smfg-card.$document`
  528. * `portal.librus.pl$$advertisement-module`
  529. * `@@-ds.metric.gstatic.com^|`
  530. * `://gom.ge/cookie.js`
  531. * `://accout-update-smba.jp.$document`
  532. * `_200x250.png`
  533. * `@@://www.liquidweb.com/kb/wp-content/themes/lw-kb-theme/images/ads/vps-sidebar.jpg`
  534. */
  535. let sliceStart = 0;
  536. let sliceEnd: number | undefined;
  537. if (lineStartsWithSingleDot) {
  538. sliceStart = 1;
  539. }
  540. if (line.endsWith('^$all')) { // This salvage line `thepiratebay3.com^$all`
  541. sliceEnd = -5;
  542. } else if (
  543. // Try to salvage line like `://account.smba.$document`
  544. // For this specific line, it will fail anyway though.
  545. line.endsWith('$document')
  546. ) {
  547. sliceEnd = -9;
  548. }
  549. const sliced = (sliceStart !== 0 || sliceEnd !== undefined) ? line.slice(sliceStart, sliceEnd) : line;
  550. const suffix = gorhill.getPublicSuffix(sliced);
  551. /**
  552. * Fast exclude definitely not domain-like resource
  553. *
  554. * `.gatracking.js`, suffix is `js`,
  555. * `.ads.css`, suffix is `css`,
  556. * `-cpm-ads.$badfilter`, suffix is `$badfilter`,
  557. * `portal.librus.pl$$advertisement-module`, suffix is `pl$$advertisement-module`
  558. */
  559. if (!suffix || !gorhill.suffixInPSL(suffix)) {
  560. // This exclude domain-like resource like `.gatracking.js`, `.beacon.min.js` and `.cookielaw.js`
  561. return null;
  562. }
  563. const tryNormalizeDomain = normalizeDomain(sliced);
  564. if (tryNormalizeDomain === sliced) {
  565. // the entire rule is domain
  566. return [sliced, ParseType.BlackIncludeSubdomain];
  567. }
  568. return [
  569. `[parse-filter E0010] can not parse: ${line}`,
  570. ParseType.ErrorMessage
  571. ];
  572. }