parse-filter.ts 19 KB

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