Skip to content

Commit 24385e3

Browse files
committed
修改 NOCSP DNR -> 接受最多 32768 domains 及 512 url filters
1 parent 65d900a commit 24385e3

2 files changed

Lines changed: 60 additions & 14 deletions

File tree

src/app/service/service_worker/script.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { getSimilarityScore, ScriptUpdateCheck } from "./script_update_check";
4747
import { LocalStorageDAO } from "@App/app/repo/localStorage";
4848
import { CompiledResourceDAO } from "@App/app/repo/resource";
4949
import { initRegularUpdateCheck } from "./regular_updatecheck";
50-
import { createNoCSPRules } from "@App/pkg/utils/dnr";
50+
import { createNoCSPRules, removeDynamicRulesInRange } from "@App/pkg/utils/dnr";
5151

5252
export type TCheckScriptUpdateOption = Partial<
5353
{ checkType: "user"; noUpdateCheck?: number } | ({ checkType: "system" } & Record<string, any>)
@@ -317,21 +317,17 @@ export class ScriptService {
317317

318318
{
319319
type Config = Record<string, any>;
320-
const rules = createNoCSPRules([`|http*`]);
320+
const rules = createNoCSPRules(["github.com", "facebook.com"], [`|http*`]);
321321

322-
const updateRules = (newConfig: Config, oldConfig?: Config) => {
322+
const updateRules = async (newConfig: Config, oldConfig?: Config) => {
323323
if (oldConfig && newConfig.csp_http_disabled === oldConfig?.csp_http_disabled) {
324324
return;
325325
}
326+
await removeDynamicRulesInRange(2001, 2520);
326327
if (newConfig.csp_http_disabled) {
327-
chrome.declarativeNetRequest.updateDynamicRules({
328-
removeRuleIds: rules.map((rule) => rule.id),
328+
await chrome.declarativeNetRequest.updateDynamicRules({
329329
addRules: rules,
330330
});
331-
} else {
332-
chrome.declarativeNetRequest.updateDynamicRules({
333-
removeRuleIds: rules.map((rule) => rule.id),
334-
});
335331
}
336332
};
337333

src/pkg/utils/dnr.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const NOCSP_RULES_DOMAIN_MAX_COUNT = 32768;
2+
const NOCSP_RULES_URLFILTER_MAX_COUNT = 512;
3+
14
export const isValidDNRUrlFilter = (text: string) => {
25
// https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en#property-RuleCondition-urlFilter
36

@@ -45,7 +48,19 @@ export const convertDomainToDNRUrlFilter = (text: string) => {
4548
return ret;
4649
};
4750

48-
export const createNoCSPRules = (urlFilters: string[]) => {
51+
export const createNoCSPRules = (domains: string[], urlFilters: string[]) => {
52+
// domains = max 32768 domains
53+
// urlFilters = max 512
54+
if (domains.length > NOCSP_RULES_DOMAIN_MAX_COUNT) {
55+
throw new Error(
56+
`createNoCSPRules: The number of domains = ${domains.length} exceeding ${NOCSP_RULES_DOMAIN_MAX_COUNT}`
57+
);
58+
}
59+
if (urlFilters.length > NOCSP_RULES_URLFILTER_MAX_COUNT) {
60+
throw new Error(
61+
`createNoCSPRules: The number of urlFilters = ${urlFilters.length} exceeding ${NOCSP_RULES_URLFILTER_MAX_COUNT}`
62+
);
63+
}
4964
const REMOVE_HEADERS = [
5065
`content-security-policy`,
5166
`content-security-policy-report-only`,
@@ -54,12 +69,9 @@ export const createNoCSPRules = (urlFilters: string[]) => {
5469
`x-frame-options`,
5570
];
5671
const { RuleActionType, HeaderOperation, ResourceType } = chrome.declarativeNetRequest;
57-
if (urlFilters.length > 512) {
58-
throw new Error(`Too many URL patterns (${urlFilters.length}). Max is 512.`);
59-
}
6072
const rules: chrome.declarativeNetRequest.Rule[] = urlFilters.map((urlFilter, index) => {
6173
return {
62-
id: 2001 + index,
74+
id: 2002 + index,
6375
action: {
6476
type: RuleActionType.MODIFY_HEADERS,
6577
responseHeaders: REMOVE_HEADERS.map((header) => ({
@@ -73,5 +85,43 @@ export const createNoCSPRules = (urlFilters: string[]) => {
7385
},
7486
} satisfies chrome.declarativeNetRequest.Rule;
7587
});
88+
const requestDomains = domains
89+
.map((s) => {
90+
try {
91+
const u = new URL(`https://${s}/`); // 取编码后的 hostname
92+
return u.hostname;
93+
} catch {
94+
// ingored
95+
}
96+
})
97+
.filter(Boolean) as string[]; // 去除错误或空字串
98+
if (domains.length > 0) {
99+
rules.push({
100+
id: 2001,
101+
action: {
102+
type: RuleActionType.MODIFY_HEADERS,
103+
responseHeaders: REMOVE_HEADERS.map((header) => ({
104+
operation: HeaderOperation.REMOVE,
105+
header,
106+
})),
107+
},
108+
condition: {
109+
requestDomains: requestDomains,
110+
resourceTypes: [ResourceType.MAIN_FRAME, ResourceType.SUB_FRAME],
111+
},
112+
} satisfies chrome.declarativeNetRequest.Rule);
113+
}
76114
return rules;
77115
};
116+
117+
export const removeDynamicRulesInRange = async (minId: number, maxId: number) => {
118+
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
119+
120+
const idsToRemove = existingRules.filter((rule) => rule.id >= minId && rule.id <= maxId).map((rule) => rule.id);
121+
122+
if (idsToRemove.length === 0) return;
123+
124+
await chrome.declarativeNetRequest.updateDynamicRules({
125+
removeRuleIds: idsToRemove,
126+
});
127+
};

0 commit comments

Comments
 (0)