@adguard/agtree
Version:
Tool set for working with adblock filter lists
201 lines (198 loc) • 6.41 kB
JavaScript
/*
* AGTree v3.4.3 (build date: Thu, 11 Dec 2025 13:43:19 GMT)
* (c) 2025 Adguard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
*/
import { CAPITAL_LETTERS, SMALL_LETTERS, NUMBERS, UNDERSCORE, OPEN_PARENTHESIS, CLOSE_PARENTHESIS } from '../utils/constants.js';
/**
* Set of all allowed characters for app name except the dot `.`.
*/
const APP_NAME_ALLOWED_CHARS = new Set([
...CAPITAL_LETTERS,
...SMALL_LETTERS,
...NUMBERS,
UNDERSCORE,
]);
/**
* Allowed methods for $method modifier.
*
* @see {@link https://adguard.app/kb/general/ad-filtering/create-own-filters/#method-modifier}
*/
const ALLOWED_METHODS = new Set([
'connect',
'delete',
'get',
'head',
'options',
'patch',
'post',
'put',
'trace',
]);
/**
* Allowed stealth options for $stealth modifier.
*
* @see {@link https://adguard.app/kb/general/ad-filtering/create-own-filters/#stealth-modifier}
*/
const ALLOWED_STEALTH_OPTIONS = new Set([
'searchqueries',
'donottrack',
'3p-cookie',
'1p-cookie',
'3p-cache',
'3p-auth',
'webrtc',
'push',
'location',
'flash',
'java',
'referrer',
'useragent',
'ip',
'xclientdata',
'dpi',
]);
/**
* Allowed CSP directives for $csp modifier.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy#directives}
*/
const ALLOWED_CSP_DIRECTIVES = new Set([
'base-uri',
'child-src',
'connect-src',
'default-src',
'fenced-frame-src',
'font-src',
'form-action',
'frame-ancestors',
'frame-src',
'img-src',
'manifest-src',
'media-src',
'navigate-to',
'object-src',
'plugin-types',
'prefetch-src',
'referrer',
'report-to',
'report-uri',
'require-trusted-types-for',
'sandbox',
'script-src',
'script-src-attr',
'script-src-elem',
'style-src',
'style-src-attr',
'style-src-elem',
'trusted-types',
'upgrade-insecure-requests',
'worker-src',
]);
/**
* Allowed directives for $permissions modifier.
*
* @see {@link https://adguard.app/kb/general/ad-filtering/create-own-filters/#permissions-modifier}
*/
const ALLOWED_PERMISSION_DIRECTIVES = new Set([
'accelerometer',
'ambient-light-sensor',
'autoplay',
'battery',
'browsing-topics',
'camera',
'display-capture',
'document-domain',
'encrypted-media',
'execution-while-not-rendered',
'execution-while-out-of-viewport',
'fullscreen',
'gamepad',
'geolocation',
'gyroscope',
'hid',
'identity-credentials-get',
'idle-detection',
'join-ad-interest-group',
'local-fonts',
'magnetometer',
'microphone',
'midi',
'payment',
'picture-in-picture',
'publickey-credentials-create',
'publickey-credentials-get',
'run-ad-auction',
'screen-wake-lock',
'serial',
'speaker-selection',
'storage-access',
'usb',
'web-share',
'xr-spatial-tracking',
]);
/**
* One of available tokens for $permission modifier value.
*
* @see {@link https://w3c.github.io/webappsec-permissions-policy/#structured-header-serialization}
*/
const PERMISSIONS_TOKEN_SELF = 'self';
/**
* One of allowlist values for $permissions modifier.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Permissions_Policy#allowlists}
*/
const EMPTY_PERMISSIONS_ALLOWLIST = `${OPEN_PARENTHESIS}${CLOSE_PARENTHESIS}`;
/**
* Allowed directives for $referrerpolicy modifier.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy}
*/
const REFERRER_POLICY_DIRECTIVES = new Set([
'no-referrer',
'no-referrer-when-downgrade',
'origin',
'origin-when-cross-origin',
'same-origin',
'strict-origin',
'strict-origin-when-cross-origin',
'unsafe-url',
]);
/**
* Prefixes for error messages used in modifier validation.
*/
const VALIDATION_ERROR_PREFIX = {
BLOCK_ONLY: 'Only blocking rules may contain the modifier',
EXCEPTION_ONLY: 'Only exception rules may contain the modifier',
INVALID_CSP_DIRECTIVES: 'Invalid CSP directives for the modifier',
INVALID_LIST_VALUES: 'Invalid values for the modifier',
INVALID_NOOP: 'Invalid noop modifier',
INVALID_PERMISSION_DIRECTIVE: 'Invalid Permissions-Policy directive for the modifier',
INVALID_PERMISSION_ORIGINS: 'Origins in the value is invalid for the modifier and the directive',
INVALID_PERMISSION_ORIGIN_QUOTES: 'Double quotes should be used for origins in the value of the modifier',
INVALID_REFERRER_POLICY_DIRECTIVE: 'Invalid Referrer-Policy directive for the modifier',
MIXED_NEGATIONS: 'Simultaneous usage of negated and not negated values is forbidden for the modifier',
NO_CSP_VALUE: 'No CSP value for the modifier and the directive',
NO_CSP_DIRECTIVE_QUOTE: 'CSP directives should no be quoted for the modifier',
NO_UNESCAPED_PERMISSION_COMMA: 'Unescaped comma in the value is not allowed for the modifier',
// TODO: implement later for $scp and $permissions
// NO_VALUE_ONLY_FOR_EXCEPTION: 'Modifier without value can be used only in exception rules',
NOT_EXISTENT: 'Non-existent modifier',
NOT_NEGATABLE_MODIFIER: 'Non-negatable modifier',
NOT_NEGATABLE_VALUE: 'Values cannot be negated for the modifier',
NOT_SUPPORTED: '%s does not support the modifier',
REMOVED: 'Removed and no longer supported modifier',
VALUE_FORBIDDEN: 'Value is not allowed for the modifier',
VALUE_INVALID: 'Value is invalid for the modifier',
VALUE_REQUIRED: 'Value is required for the modifier',
};
/**
* Prefixes for error messages related to issues in the source YAML files' data.
*/
const SOURCE_DATA_ERROR_PREFIX = {
INVALID_VALUE_FORMAT_REGEXP: "Invalid regular expression in 'value_format' for the modifier",
NO_DEPRECATION_MESSAGE: "Property 'deprecation_message' is required for the 'deprecated' modifier",
NO_VALUE_FORMAT_FOR_ASSIGNABLE: "Property 'value_format' should be specified for the assignable modifier",
};
export { ALLOWED_CSP_DIRECTIVES, ALLOWED_METHODS, ALLOWED_PERMISSION_DIRECTIVES, ALLOWED_STEALTH_OPTIONS, APP_NAME_ALLOWED_CHARS, EMPTY_PERMISSIONS_ALLOWLIST, PERMISSIONS_TOKEN_SELF, REFERRER_POLICY_DIRECTIVES, SOURCE_DATA_ERROR_PREFIX, VALIDATION_ERROR_PREFIX };