-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdomObserver.js
272 lines (229 loc) · 7.84 KB
/
domObserver.js
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { ConfigManager } from './config.js';
import { xssDetector } from './xssDetector.js';
class DOMObserver {
constructor() {
this.config = null;
this.observer = null;
this.mutationQueue = [];
this.processing = false;
this.frameworkDetector = null;
}
async initialize() {
this.config = await ConfigManager.load();
this.initializeFrameworkDetector();
this.startObserving();
}
// 初始化框架检测器
initializeFrameworkDetector() {
this.frameworkDetector = {
detectVue() {
return !!window.__VUE__;
},
detectReact() {
return !!window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
},
detectAngular() {
return !!window.angular;
},
detectSvelte() {
return !!window.__svelte;
}
};
}
// 开始观察DOM变化
startObserving() {
const { throttle, batchSize, maxDepth } = this.config.performance.domObserver;
this.observer = new MutationObserver((mutations) => {
// 将变化添加到队列
this.mutationQueue.push(...mutations);
// 使用节流处理队列
if (!this.processing) {
this.processMutationQueue();
}
});
// 配置观察选项
const options = {
childList: true, // 观察子节点的添加和删除
subtree: true, // 观察所有后代节点
attributes: true, // 观察属性变化
characterData: true, // 观察文本内容变化
attributeFilter: ['value', 'innerHTML', 'textContent'] // 只观察特定属性
};
// 开始观察
this.observer.observe(document.documentElement, options);
}
// 处理变化队列
async processMutationQueue() {
if (this.mutationQueue.length === 0) return;
this.processing = true;
// 获取一批变化进行处理
const batch = this.mutationQueue.splice(0, this.config.performance.domObserver.batchSize);
// 处理每个变化
for (const mutation of batch) {
await this.processMutation(mutation);
}
// 等待节流时间
await new Promise(resolve =>
setTimeout(resolve, this.config.performance.domObserver.throttle)
);
this.processing = false;
// 如果队列中还有变化,继续处理
if (this.mutationQueue.length > 0) {
this.processMutationQueue();
}
}
// 处理单个变化
async processMutation(mutation) {
// 处理添加的节点
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
await this.analyzeNode(node);
}
}
// 处理属性变化
if (mutation.type === 'attributes') {
await this.analyzeAttributeChange(mutation.target, mutation.attributeName);
}
// 处理文本内容变化
if (mutation.type === 'characterData') {
await this.analyzeTextContent(mutation.target);
}
}
// 分析节点
async analyzeNode(node, depth = 0) {
if (depth > this.config.performance.domObserver.maxDepth) return;
// 检查框架特定的注入点
if (this.detectFramework()) {
await this.analyzeFrameworkSpecific(node);
}
// 分析节点属性
for (const attr of node.attributes) {
await this.analyzeAttribute(node, attr);
}
// 分析子节点
for (const child of node.children) {
await this.analyzeNode(child, depth + 1);
}
}
// 分析属性
async analyzeAttribute(node, attribute) {
const { name, value } = attribute;
// 检查敏感属性
if (this.isSensitiveAttribute(name)) {
const result = await xssDetector.detectXSS(value, {
type: 'attribute',
name: name,
node: node
});
if (result) {
this.reportVulnerability(result);
}
}
}
// 分析属性变化
async analyzeAttributeChange(node, attributeName) {
if (this.isSensitiveAttribute(attributeName)) {
const value = node.getAttribute(attributeName);
const result = await xssDetector.detectXSS(value, {
type: 'attribute',
name: attributeName,
node: node
});
if (result) {
this.reportVulnerability(result);
}
}
}
// 分析文本内容
async analyzeTextContent(node) {
const result = await xssDetector.detectXSS(node.textContent, {
type: 'text',
node: node
});
if (result) {
this.reportVulnerability(result);
}
}
// 分析框架特定的注入点
async analyzeFrameworkSpecific(node) {
// Vue特定分析
if (this.frameworkDetector.detectVue()) {
await this.analyzeVueNode(node);
}
// React特定分析
if (this.frameworkDetector.detectReact()) {
await this.analyzeReactNode(node);
}
// Angular特定分析
if (this.frameworkDetector.detectAngular()) {
await this.analyzeAngularNode(node);
}
// Svelte特定分析
if (this.frameworkDetector.detectSvelte()) {
await this.analyzeSvelteNode(node);
}
}
// 检测使用的框架
detectFramework() {
return Object.values(this.frameworkDetector).some(detector => detector());
}
// 检查敏感属性
isSensitiveAttribute(name) {
const sensitiveAttributes = [
'innerHTML',
'outerHTML',
'value',
'src',
'href',
'data-*',
'v-html', // Vue
'dangerouslySetInnerHTML', // React
'ng-bind-html' // Angular
];
return sensitiveAttributes.some(attr =>
name === attr || name.startsWith('data-') ||
name.startsWith('v-') || name.startsWith('ng-')
);
}
// 报告漏洞
reportVulnerability(result) {
// 这里实现漏洞报告逻辑
console.log('XSS Vulnerability detected:', result);
// 可以添加视觉提示
this.highlightVulnerableElement(result.context.node);
}
// 高亮显示易受攻击的元素
highlightVulnerableElement(node) {
const originalStyle = node.style.cssText;
node.style.cssText = `
${originalStyle}
outline: 3px solid red !important;
animation: xss-warning 1s infinite;
`;
// 添加警告动画
const style = document.createElement('style');
style.textContent = `
@keyframes xss-warning {
0% { outline-color: red; }
50% { outline-color: yellow; }
100% { outline-color: red; }
}
`;
document.head.appendChild(style);
// 3秒后移除高亮
setTimeout(() => {
node.style.cssText = originalStyle;
style.remove();
}, 3000);
}
// 停止观察
stopObserving() {
if (this.observer) {
this.observer.disconnect();
this.observer = null;
}
this.mutationQueue = [];
this.processing = false;
}
}
export const domObserver = new DOMObserver();