-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathcspInterceptor.ts
More file actions
257 lines (231 loc) · 8.59 KB
/
cspInterceptor.ts
File metadata and controls
257 lines (231 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { CSPRuleDAO, type CSPRule } from "@App/app/repo/cspRule";
import type Logger from "@App/app/logger/logger";
import LoggerCore from "@App/app/logger/core";
import { toDeclarativeNetRequestFilter } from "@App/pkg/utils/patternMatcher";
const CSP_RULE_ID_START = 10000;
const MAX_DYNAMIC_RULES = 5000;
export class CSPInterceptorService {
private logger: Logger;
private cspRuleDAO: CSPRuleDAO;
private enabledRules: CSPRule[] = [];
private globalEnabled: boolean = false;
private initialized: boolean = false;
private ruleIdCounter: number = CSP_RULE_ID_START;
constructor() {
this.logger = LoggerCore.logger().with({ service: "cspInterceptor" });
this.cspRuleDAO = new CSPRuleDAO();
}
/**
* 初始化拦截器
* 加载已有的启用规则并注册到 Chrome DNR
*/
async init() {
if (this.initialized) {
return;
}
this.initialized = true;
// 清除历史遗留的脏数据
try {
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
const cspRuleIds = existingRules
.filter((rule) => rule.id >= CSP_RULE_ID_START && rule.id < CSP_RULE_ID_START + MAX_DYNAMIC_RULES)
.map((rule) => rule.id);
if (cspRuleIds.length > 0) {
this.logger.info("cleaning up legacy CSP dynamic rules", { count: cspRuleIds.length });
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: cspRuleIds,
});
}
} catch (e) {
this.logger.warn("failed to cleanup legacy CSP rules", { error: String(e) });
}
// 加载全局配置和已有规则
const config = await this.cspRuleDAO.getCSPConfig();
this.globalEnabled = config.globalEnabled;
this.enabledRules = await this.cspRuleDAO.getEnabledRules();
this.logger.info("csp interceptor initialized", {
globalEnabled: this.globalEnabled,
ruleCount: this.enabledRules.length,
});
await this.updateDeclarativeRules();
}
/**
* 更新启用的规则列表并重新注册 DNR 规则
* 由 CSPRuleService 在规则变更时直接调用
*/
async updateRules(enabledRules: CSPRule[]) {
this.enabledRules = enabledRules;
this.logger.info("csp rules updated", { ruleCount: enabledRules.length });
await this.updateDeclarativeRules();
}
/**
* 更新全局开关状态
*/
async updateGlobalEnabled(enabled: boolean) {
this.globalEnabled = enabled;
this.logger.info("csp global enabled changed", { globalEnabled: enabled });
await this.updateDeclarativeRules();
}
private async updateDeclarativeRules() {
try {
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
const existingRuleIds = existingRules
.filter((rule) => rule.id >= CSP_RULE_ID_START && rule.id < CSP_RULE_ID_START + MAX_DYNAMIC_RULES)
.map((rule) => rule.id);
this.logger.info("updating declarative rules", {
removeCount: existingRuleIds.length,
globalEnabled: this.globalEnabled,
enabledCount: this.enabledRules.length,
});
const newRules: chrome.declarativeNetRequest.Rule[] = [];
this.ruleIdCounter = CSP_RULE_ID_START;
// 全局模式:注册一条全量移除规则
if (this.globalEnabled) {
newRules.push({
id: this.ruleIdCounter++,
priority: 9999,
action: {
type: "modifyHeaders" as chrome.declarativeNetRequest.RuleActionType,
responseHeaders: [
{
header: "Content-Security-Policy",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "Content-Security-Policy-Report-Only",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "X-Content-Security-Policy",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "X-WebKit-CSP",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
],
},
condition: {
urlFilter: "*",
resourceTypes: ["main_frame", "sub_frame"] as chrome.declarativeNetRequest.ResourceType[],
},
});
// 全局模式下不需要注册具体规则
} else {
// 规则模式:按优先级注册各规则
const sortedRules = [...this.enabledRules].sort((a, b) => b.priority - a.priority);
for (const rule of sortedRules) {
const dnrRules = this.convertToDeclarativeRule(rule);
if (dnrRules) {
for (const dnrRule of dnrRules) {
if (newRules.length >= MAX_DYNAMIC_RULES) {
this.logger.warn("max dynamic rules limit reached", { limit: MAX_DYNAMIC_RULES });
break;
}
newRules.push(dnrRule);
}
}
if (newRules.length >= MAX_DYNAMIC_RULES) {
break;
}
}
}
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: existingRuleIds,
addRules: newRules,
});
this.logger.info("declarative rules updated successfully", {
addedCount: newRules.length,
});
} catch (e) {
this.logger.error("failed to update declarative rules", { error: String(e) });
}
}
private convertToDeclarativeRule(rule: CSPRule): chrome.declarativeNetRequest.Rule[] | null {
if (!rule.enabled) {
return null;
}
const conditions = this.buildConditions(rule.path);
if (conditions.length === 0) {
this.logger.warn("could not build condition for rule", { ruleName: rule.name, path: rule.path });
return null;
}
const dnrRules: chrome.declarativeNetRequest.Rule[] = [];
for (const condition of conditions) {
const ruleId = this.ruleIdCounter++;
// Chrome DNR 要求 priority >= 1
const dnrPriority = Math.max(1, rule.priority);
if (rule.action === "remove") {
dnrRules.push({
id: ruleId,
priority: dnrPriority,
action: {
type: "modifyHeaders" as chrome.declarativeNetRequest.RuleActionType,
responseHeaders: [
{
header: "Content-Security-Policy",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "Content-Security-Policy-Report-Only",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "X-Content-Security-Policy",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
{
header: "X-WebKit-CSP",
operation: "remove" as chrome.declarativeNetRequest.HeaderOperation,
},
],
},
condition: {
...condition,
resourceTypes: ["main_frame", "sub_frame"] as chrome.declarativeNetRequest.ResourceType[],
},
});
} else if (rule.action === "modify" && rule.actionValue) {
dnrRules.push({
id: ruleId,
priority: dnrPriority,
action: {
type: "modifyHeaders" as chrome.declarativeNetRequest.RuleActionType,
responseHeaders: [
{
header: "Content-Security-Policy",
operation: "set" as chrome.declarativeNetRequest.HeaderOperation,
value: rule.actionValue,
},
],
},
condition: {
...condition,
resourceTypes: ["main_frame", "sub_frame"] as chrome.declarativeNetRequest.ResourceType[],
},
});
}
}
return dnrRules;
}
private buildConditions(pattern: string): Partial<chrome.declarativeNetRequest.RuleCondition>[] {
const conditions: Partial<chrome.declarativeNetRequest.RuleCondition>[] = [];
try {
const filter = toDeclarativeNetRequestFilter(pattern);
if (filter.regexFilter) {
conditions.push({
regexFilter: filter.regexFilter,
});
this.logger.debug("using regex filter", { pattern, regexFilter: filter.regexFilter });
} else if (filter.urlFilter) {
conditions.push({
urlFilter: filter.urlFilter,
});
this.logger.debug("using url filter", { pattern, urlFilter: filter.urlFilter });
}
} catch (e) {
this.logger.warn("invalid pattern", { pattern, error: String(e) });
}
return conditions;
}
}