@meng-xi/vite-plugin 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-en.md +48 -774
- package/README.md +45 -769
- package/dist/common/html/index.cjs +2 -2
- package/dist/common/html/index.d.cts +268 -19
- package/dist/common/html/index.d.mts +268 -19
- package/dist/common/html/index.d.ts +268 -19
- package/dist/common/html/index.mjs +2 -2
- package/dist/common/index.cjs +1 -1
- package/dist/common/index.d.cts +4 -1
- package/dist/common/index.d.mts +4 -1
- package/dist/common/index.d.ts +4 -1
- package/dist/common/index.mjs +1 -1
- package/dist/common/ui/index.cjs +1 -0
- package/dist/common/ui/index.d.cts +132 -0
- package/dist/common/ui/index.d.mts +132 -0
- package/dist/common/ui/index.d.ts +132 -0
- package/dist/common/ui/index.mjs +1 -0
- package/dist/common/validation/index.cjs +1 -1
- package/dist/common/validation/index.d.cts +1 -0
- package/dist/common/validation/index.d.mts +1 -0
- package/dist/common/validation/index.d.ts +1 -0
- package/dist/common/validation/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +6 -2
- package/dist/index.d.mts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.mjs +1 -1
- package/dist/plugins/buildProgress/index.cjs +2 -2
- package/dist/plugins/buildProgress/index.mjs +2 -2
- package/dist/plugins/copyFile/index.d.cts +20 -2
- package/dist/plugins/copyFile/index.d.mts +20 -2
- package/dist/plugins/copyFile/index.d.ts +20 -2
- package/dist/plugins/envGuard/index.cjs +67 -0
- package/dist/plugins/envGuard/index.d.cts +156 -0
- package/dist/plugins/envGuard/index.d.mts +156 -0
- package/dist/plugins/envGuard/index.d.ts +156 -0
- package/dist/plugins/envGuard/index.mjs +67 -0
- package/dist/plugins/faviconManager/index.cjs +1 -1
- package/dist/plugins/faviconManager/index.d.cts +43 -5
- package/dist/plugins/faviconManager/index.d.mts +43 -5
- package/dist/plugins/faviconManager/index.d.ts +43 -5
- package/dist/plugins/faviconManager/index.mjs +1 -1
- package/dist/plugins/generateRouter/index.d.cts +61 -14
- package/dist/plugins/generateRouter/index.d.mts +61 -14
- package/dist/plugins/generateRouter/index.d.ts +61 -14
- package/dist/plugins/generateVersion/index.d.cts +12 -0
- package/dist/plugins/generateVersion/index.d.mts +12 -0
- package/dist/plugins/generateVersion/index.d.ts +12 -0
- package/dist/plugins/htmlInject/index.cjs +1 -7
- package/dist/plugins/htmlInject/index.d.cts +49 -194
- package/dist/plugins/htmlInject/index.d.mts +49 -194
- package/dist/plugins/htmlInject/index.d.ts +49 -194
- package/dist/plugins/htmlInject/index.mjs +1 -7
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +4 -1
- package/dist/plugins/index.d.mts +4 -1
- package/dist/plugins/index.d.ts +4 -1
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/loadingManager/index.cjs +2 -2
- package/dist/plugins/loadingManager/index.mjs +1 -1
- package/dist/plugins/versionUpdateChecker/index.cjs +2 -2
- package/dist/plugins/versionUpdateChecker/index.mjs +3 -3
- package/dist/shared/vite-plugin.BCuhU1au.mjs +7 -0
- package/dist/shared/vite-plugin.BrI73DHA.cjs +7 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.cts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.mts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.ts +261 -0
- package/dist/shared/vite-plugin.DnFDPjNf.mjs +1 -0
- package/dist/shared/vite-plugin.Dumot0up.mjs +1 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.cts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.mts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.ts +143 -0
- package/dist/shared/vite-plugin.soT9a-KD.cjs +1 -0
- package/dist/shared/vite-plugin.vwox4bU0.cjs +1 -0
- package/package.json +11 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";function injectBeforeTag(e,t,
|
|
2
|
-
${t}`),injected:!0}:{html:e,injected:!1}}function injectHtmlByPriority(e,t,
|
|
1
|
+
"use strict";const security=require("../../shared/vite-plugin.BrI73DHA.cjs");require("../script/index.cjs");function injectBeforeTag(e,t,r){const i=new RegExp(t,"i");return i.test(e)?{html:e.replace(i,`${r}
|
|
2
|
+
${t}`),injected:!0}:{html:e,injected:!1}}function injectHtmlByPriority(e,t,r=["</head>","</body>","</html>"]){for(const i of r){const n=injectBeforeTag(e,i,t);if(n.injected)return n}return{html:e+t,injected:!0}}function injectBeforeTagWithFallback(e,t,r){const i=injectBeforeTag(e,"</body>",t);if(i.injected)return{...i,usedFallback:!1};const n=injectBeforeTag(e,"</html>",t);return n.injected?{...n,usedFallback:!1}:{html:e+t,injected:!0,usedFallback:!0}}function injectHeadAndBody(e,t,r){let i=e,n=!1;if(t){const c=injectBeforeTag(i,"</head>",t);c.injected&&(i=c.html,n=!0)}const o=injectBeforeTagWithFallback(i,r);return{html:o.html,headInjected:n,bodyInjected:o.injected,usedFallback:o.usedFallback}}exports.DEFAULT_BLOCKED_ATTRIBUTES=security.DEFAULT_BLOCKED_ATTRIBUTES,exports.DEFAULT_BLOCKED_TAGS=security.DEFAULT_BLOCKED_TAGS,exports.applyTemplateVars=security.applyTemplateVars,exports.evaluateCondition=security.evaluateCondition,exports.findSelectorMatch=security.findSelectorMatch,exports.injectAtPosition=security.injectAtPosition,exports.sanitizeContent=security.sanitizeContent,exports.sortRulesByPriority=security.sortRulesByPriority,exports.validateSecurityConfig=security.validateSecurityConfig,exports.injectBeforeTag=injectBeforeTag,exports.injectBeforeTagWithFallback=injectBeforeTagWithFallback,exports.injectHeadAndBody=injectHeadAndBody,exports.injectHtmlByPriority=injectHtmlByPriority;
|
|
@@ -1,25 +1,274 @@
|
|
|
1
|
+
import { I as InjectCondition, b as SelectorMatch, a as InjectPosition, P as PositionInjectResult, S as SecurityConfig, H as HtmlInjectResult, D as DualInjectResult } from '../../shared/vite-plugin.FfJ-Wwfu.cjs';
|
|
2
|
+
export { C as ConditionType } from '../../shared/vite-plugin.FfJ-Wwfu.cjs';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* HTML
|
|
5
|
+
* HTML 注入工具模块
|
|
6
|
+
*
|
|
7
|
+
* @module common/html/inject
|
|
8
|
+
* @description 提供高级 HTML 内容注入功能,包括选择器匹配、模板变量替换、
|
|
9
|
+
* 条件判断、规则排序和多位置注入等能力,为插件提供灵活的 HTML 操作支持。
|
|
3
10
|
*/
|
|
4
|
-
|
|
5
|
-
/** 注入后的 HTML 内容 */
|
|
6
|
-
html: string;
|
|
7
|
-
/** 是否成功注入 */
|
|
8
|
-
injected: boolean;
|
|
9
|
-
}
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
13
|
+
* 在 HTML 中查找选择器匹配位置
|
|
14
|
+
*
|
|
15
|
+
* @param html - 要搜索的 HTML 字符串
|
|
16
|
+
* @param selector - 选择器字符串(普通字符串或正则表达式字符串)
|
|
17
|
+
* @param selectorMatch - 匹配模式,`'string'` 为精确字符串匹配,`'regex'` 为正则匹配
|
|
18
|
+
* @returns 匹配结果对象(包含 `index` 和 `length`),未匹配时返回 `null`
|
|
19
|
+
*
|
|
20
|
+
* @description 根据匹配模式在 HTML 中查找选择器的位置:
|
|
21
|
+
* - 字符串模式:使用 `indexOf` 进行精确匹配
|
|
22
|
+
* - 正则模式:使用 `RegExp` 进行正则匹配,正则语法错误时返回 `null`
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 字符串匹配
|
|
27
|
+
* findSelectorMatch('<div id="app">content</div>', '<div id="app">')
|
|
28
|
+
* // { index: 0, length: 14 }
|
|
29
|
+
*
|
|
30
|
+
* // 正则匹配
|
|
31
|
+
* findSelectorMatch('<div class="foo">bar</div>', 'class="\\w+"', 'regex')
|
|
32
|
+
* // { index: 5, length: 12 }
|
|
33
|
+
*
|
|
34
|
+
* // 未匹配
|
|
35
|
+
* findSelectorMatch('<div>hello</div>', '<span>')
|
|
36
|
+
* // null
|
|
37
|
+
* ```
|
|
12
38
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
39
|
+
declare function findSelectorMatch(html: string, selector: string, selectorMatch?: SelectorMatch): {
|
|
40
|
+
index: number;
|
|
41
|
+
length: number;
|
|
42
|
+
} | null;
|
|
43
|
+
/**
|
|
44
|
+
* 替换模板字符串中的变量占位符
|
|
45
|
+
*
|
|
46
|
+
* @param content - 包含 `{{变量名}}` 占位符的模板字符串
|
|
47
|
+
* @param ruleVars - 规则级变量映射(优先级高于全局变量)
|
|
48
|
+
* @param globalVars - 全局变量映射
|
|
49
|
+
* @returns 替换所有匹配占位符后的字符串
|
|
50
|
+
*
|
|
51
|
+
* @description 将模板字符串中形如 `{{key}}` 的占位符替换为对应的值。
|
|
52
|
+
* 规则变量(`ruleVars`)优先级高于全局变量(`globalVars`),
|
|
53
|
+
* 当两者存在相同键时,规则变量的值会覆盖全局变量的值。
|
|
54
|
+
* 变量名中的正则特殊字符会被自动转义,替换值中的 `$` 也会被安全处理。
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // 基本替换
|
|
59
|
+
* applyTemplateVars('<div>{{name}}</div>', { name: 'test' })
|
|
60
|
+
* // '<div>test</div>'
|
|
61
|
+
*
|
|
62
|
+
* // 规则变量覆盖全局变量
|
|
63
|
+
* applyTemplateVars('{{a}}-{{b}}', { a: '1' }, { a: '0', b: '2' })
|
|
64
|
+
* // '1-2'
|
|
65
|
+
*
|
|
66
|
+
* // 无匹配占位符
|
|
67
|
+
* applyTemplateVars('no vars', { x: 'y' })
|
|
68
|
+
* // 'no vars'
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function applyTemplateVars(content: string, ruleVars?: Record<string, string>, globalVars?: Record<string, string>): string;
|
|
72
|
+
/**
|
|
73
|
+
* 评估注入条件是否满足
|
|
74
|
+
*
|
|
75
|
+
* @param condition - 注入条件配置
|
|
76
|
+
* @param html - 当前 HTML 内容(用于 `file-contains` 类型判断)
|
|
77
|
+
* @returns 条件是否满足(布尔值)
|
|
78
|
+
*
|
|
79
|
+
* @description 根据条件类型评估注入条件:
|
|
80
|
+
* - `env`:检查环境变量是否存在且不为 `'false'` 或 `'0'`
|
|
81
|
+
* - `file-contains`:检查 HTML 内容是否包含指定字符串
|
|
82
|
+
* - `custom`:执行自定义函数,捕获异常时返回 `false`
|
|
83
|
+
* - 支持通过 `negate` 字段对结果取反
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // 环境变量判断
|
|
88
|
+
* evaluateCondition({ type: 'env', value: 'ENABLE_ANALYTICS' }, '')
|
|
89
|
+
*
|
|
90
|
+
* // 内容包含判断
|
|
91
|
+
* evaluateCondition({ type: 'file-contains', value: '<div id="app">' }, html)
|
|
92
|
+
*
|
|
93
|
+
* // 自定义函数(带取反)
|
|
94
|
+
* evaluateCondition(
|
|
95
|
+
* { type: 'custom', value: () => isDev(), negate: true },
|
|
96
|
+
* html
|
|
97
|
+
* )
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function evaluateCondition(condition: InjectCondition, html: string): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 按优先级排序规则列表
|
|
103
|
+
*
|
|
104
|
+
* @typeParam T - 规则类型,必须包含可选的 `priority` 字段
|
|
105
|
+
* @param rules - 待排序的规则数组
|
|
106
|
+
* @returns 按 `priority` 升序排列的新数组(不修改原数组)
|
|
107
|
+
*
|
|
108
|
+
* @description 将规则按 `priority` 字段升序排列,`priority` 越小优先级越高。
|
|
109
|
+
* 未指定 `priority` 的规则默认优先级为 100。
|
|
110
|
+
* 返回新数组,不修改原数组。
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const rules = [
|
|
115
|
+
* { priority: 30, name: 'C' },
|
|
116
|
+
* { priority: 10, name: 'A' },
|
|
117
|
+
* { name: 'D' }, // 默认 priority=100
|
|
118
|
+
* { priority: 20, name: 'B' }
|
|
119
|
+
* ]
|
|
120
|
+
* sortRulesByPriority(rules)
|
|
121
|
+
* // [{ priority: 10, name: 'A' }, { priority: 20, name: 'B' },
|
|
122
|
+
* // { priority: 30, name: 'C' }, { name: 'D' }]
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare function sortRulesByPriority<T extends {
|
|
126
|
+
priority?: number;
|
|
127
|
+
}>(rules: T[]): T[];
|
|
128
|
+
/**
|
|
129
|
+
* 在 HTML 的指定位置注入内容
|
|
130
|
+
*
|
|
131
|
+
* @param html - 原始 HTML 字符串
|
|
132
|
+
* @param content - 要注入的内容
|
|
133
|
+
* @param position - 注入位置
|
|
134
|
+
* @param selector - 选择器字符串(仅 `before-selector`、`after-selector`、`replace-selector` 位置需要)
|
|
135
|
+
* @param selectorMatch - 选择器匹配模式(默认为字符串匹配)
|
|
136
|
+
* @returns 注入结果对象,包含注入后的 HTML、是否成功标志和失败原因
|
|
137
|
+
*
|
|
138
|
+
* @description 根据 `position` 参数将内容注入到 HTML 的指定位置:
|
|
139
|
+
* - `head-start`:在 `<head>` 标签开始后注入
|
|
140
|
+
* - `head-end`:在 `</head>` 标签前注入
|
|
141
|
+
* - `body-start`:在 `<body>` 标签开始后注入
|
|
142
|
+
* - `body-end`:在 `</body>` 标签前注入
|
|
143
|
+
* - `before-selector`:在选择器匹配位置前注入
|
|
144
|
+
* - `after-selector`:在选择器匹配位置后注入
|
|
145
|
+
* - `replace-selector`:替换选择器匹配的内容
|
|
146
|
+
*
|
|
147
|
+
* 当目标标签或选择器未找到时,返回 `injected: false` 并附带 `reason` 说明。
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const html = '<html><head><title>Test</title></head><body><div id="app"></div></body></html>'
|
|
152
|
+
*
|
|
153
|
+
* // 在 head 开始后注入 meta 标签
|
|
154
|
+
* injectAtPosition(html, '<meta charset="utf-8">', 'head-start')
|
|
155
|
+
*
|
|
156
|
+
* // 在 body 结束前注入脚本
|
|
157
|
+
* injectAtPosition(html, '<script>app()</script>', 'body-end')
|
|
158
|
+
*
|
|
159
|
+
* // 在指定元素前注入
|
|
160
|
+
* injectAtPosition(html, '<nav>menu</nav>', 'before-selector', '<div id="app">')
|
|
161
|
+
*
|
|
162
|
+
* // 替换指定元素
|
|
163
|
+
* injectAtPosition(html, '<div id="root"></div>', 'replace-selector', '<div id="app">')
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
declare function injectAtPosition(html: string, content: string, position: InjectPosition, selector?: string, selectorMatch?: SelectorMatch): PositionInjectResult;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* HTML 安全过滤模块
|
|
170
|
+
*
|
|
171
|
+
* @module common/html/security
|
|
172
|
+
* @description 提供 HTML 内容安全过滤和验证功能,防止 XSS 攻击和危险内容注入,
|
|
173
|
+
* 包括危险标签检测、危险属性过滤和安全配置验证等能力。
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 默认阻止的 HTML 标签列表
|
|
178
|
+
*
|
|
179
|
+
* @description 包含可能导致安全风险的 HTML 标签:
|
|
180
|
+
* - `script`:可执行 JavaScript 代码
|
|
181
|
+
* - `iframe`:可嵌入外部页面
|
|
182
|
+
* - `object`/`embed`/`applet`:可嵌入插件
|
|
183
|
+
* - `form`/`input`/`textarea`/`select`/`button`:可创建表单
|
|
184
|
+
*/
|
|
185
|
+
declare const DEFAULT_BLOCKED_TAGS: string[];
|
|
186
|
+
/**
|
|
187
|
+
* 默认阻止的 HTML 事件属性列表
|
|
188
|
+
*
|
|
189
|
+
* @description 包含所有可能导致 JavaScript 执行的事件处理属性,
|
|
190
|
+
* 涵盖鼠标事件、键盘事件、表单事件、拖拽事件和动画事件等。
|
|
191
|
+
*/
|
|
192
|
+
declare const DEFAULT_BLOCKED_ATTRIBUTES: string[];
|
|
193
|
+
/**
|
|
194
|
+
* 验证安全配置的合法性
|
|
195
|
+
*
|
|
196
|
+
* @param security - 安全配置对象
|
|
197
|
+
* @throws 当 `blockedTags` 不是字符串数组时抛出错误
|
|
198
|
+
* @throws 当 `allowedTags` 不是字符串数组时抛出错误
|
|
199
|
+
* @throws 当 `blockedAttributes` 不是字符串数组时抛出错误
|
|
200
|
+
*
|
|
201
|
+
* @description 检查安全配置中的数组字段是否为合法的字符串数组类型,
|
|
202
|
+
* 传入 `undefined` 时直接跳过验证。
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* // 合法配置
|
|
207
|
+
* validateSecurityConfig({ blockedTags: ['iframe'], allowedTags: ['div'] })
|
|
208
|
+
*
|
|
209
|
+
* // 非法配置(抛出错误)
|
|
210
|
+
* validateSecurityConfig({ blockedTags: 'iframe' })
|
|
211
|
+
* // Error: security.blockedTags 必须是字符串数组
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare function validateSecurityConfig(security?: SecurityConfig): void;
|
|
215
|
+
/**
|
|
216
|
+
* 内容消毒规则选项
|
|
217
|
+
*
|
|
218
|
+
* @description 定义对单条注入规则内容进行安全过滤时的选项,
|
|
219
|
+
* 控制是否允许脚本注入等特殊行为。
|
|
220
|
+
*/
|
|
221
|
+
interface SanitizeRuleOptions {
|
|
222
|
+
/** 规则标识符,用于错误消息和警告日志中标识来源规则 */
|
|
223
|
+
id?: string;
|
|
224
|
+
/**
|
|
225
|
+
* 是否允许注入 `<script>` 标签和被阻止的标签/属性
|
|
226
|
+
*
|
|
227
|
+
* @description 设置为 `true` 时,将跳过对危险标签和属性的安全检查,
|
|
228
|
+
* 但仍会输出警告日志提醒开发者注意安全风险。
|
|
229
|
+
* 仅在注入内容来源可信时使用此选项。
|
|
230
|
+
*
|
|
231
|
+
* @default false
|
|
232
|
+
*/
|
|
233
|
+
allowScriptInjection?: boolean;
|
|
22
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* 对注入内容进行安全过滤
|
|
237
|
+
*
|
|
238
|
+
* @param content - 待过滤的 HTML 内容字符串
|
|
239
|
+
* @param rule - 当前注入规则的消毒选项
|
|
240
|
+
* @param security - 全局安全配置
|
|
241
|
+
* @param logger - 日志记录器(需提供 `warn` 方法),用于输出安全警告
|
|
242
|
+
* @returns 过滤后的安全 HTML 内容字符串
|
|
243
|
+
* @throws 当内容包含被阻止的标签且未启用 `allowScriptInjection` 时抛出错误
|
|
244
|
+
* @throws 当内容包含危险属性且未启用 `allowScriptInjection` 时抛出错误
|
|
245
|
+
*
|
|
246
|
+
* @description 对 HTML 内容进行安全过滤,防止 XSS 攻击:
|
|
247
|
+
* 1. **标签过滤**:检测并阻止默认阻止列表中的标签(如 script、iframe),
|
|
248
|
+
* 可通过 `security.allowedTags` 放行特定标签
|
|
249
|
+
* 2. **属性过滤**:检测并阻止事件处理属性(如 onclick、onerror)
|
|
250
|
+
* 3. **脚本检测**:特别检测 `<script>` 标签,需要显式启用 `allowScriptInjection`
|
|
251
|
+
*
|
|
252
|
+
* 当 `rule.allowScriptInjection` 为 `true` 时,跳过安全检查但输出警告日志。
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // 安全内容直接通过
|
|
257
|
+
* sanitizeContent('<div>safe</div>', { id: 'rule1' })
|
|
258
|
+
*
|
|
259
|
+
* // 包含 script 标签(抛出错误)
|
|
260
|
+
* sanitizeContent('<script>alert(1)</script>', { id: 'rule2' })
|
|
261
|
+
*
|
|
262
|
+
* // 允许脚本注入(通过但输出警告)
|
|
263
|
+
* sanitizeContent('<script>alert(1)</script>', { id: 'rule3', allowScriptInjection: true }, undefined, console)
|
|
264
|
+
*
|
|
265
|
+
* // 自定义安全配置
|
|
266
|
+
* sanitizeContent('<iframe>test</iframe>', { id: 'rule4' }, { allowedTags: ['iframe'] })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare function sanitizeContent(content: string, rule: SanitizeRuleOptions, security?: SecurityConfig, logger?: {
|
|
270
|
+
warn: (msg: string) => void;
|
|
271
|
+
}): string;
|
|
23
272
|
|
|
24
273
|
/**
|
|
25
274
|
* 在 HTML 中指定闭合标签前注入代码
|
|
@@ -105,5 +354,5 @@ declare function injectBeforeTagWithFallback(html: string, code: string, _fallba
|
|
|
105
354
|
*/
|
|
106
355
|
declare function injectHeadAndBody(html: string, headCode: string | undefined, bodyCode: string): DualInjectResult;
|
|
107
356
|
|
|
108
|
-
export { injectBeforeTag, injectBeforeTagWithFallback, injectHeadAndBody, injectHtmlByPriority };
|
|
109
|
-
export type {
|
|
357
|
+
export { DEFAULT_BLOCKED_ATTRIBUTES, DEFAULT_BLOCKED_TAGS, DualInjectResult, HtmlInjectResult, InjectCondition, InjectPosition, PositionInjectResult, SecurityConfig, SelectorMatch, applyTemplateVars, evaluateCondition, findSelectorMatch, injectAtPosition, injectBeforeTag, injectBeforeTagWithFallback, injectHeadAndBody, injectHtmlByPriority, sanitizeContent, sortRulesByPriority, validateSecurityConfig };
|
|
358
|
+
export type { SanitizeRuleOptions };
|
|
@@ -1,25 +1,274 @@
|
|
|
1
|
+
import { I as InjectCondition, b as SelectorMatch, a as InjectPosition, P as PositionInjectResult, S as SecurityConfig, H as HtmlInjectResult, D as DualInjectResult } from '../../shared/vite-plugin.FfJ-Wwfu.mjs';
|
|
2
|
+
export { C as ConditionType } from '../../shared/vite-plugin.FfJ-Wwfu.mjs';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* HTML
|
|
5
|
+
* HTML 注入工具模块
|
|
6
|
+
*
|
|
7
|
+
* @module common/html/inject
|
|
8
|
+
* @description 提供高级 HTML 内容注入功能,包括选择器匹配、模板变量替换、
|
|
9
|
+
* 条件判断、规则排序和多位置注入等能力,为插件提供灵活的 HTML 操作支持。
|
|
3
10
|
*/
|
|
4
|
-
|
|
5
|
-
/** 注入后的 HTML 内容 */
|
|
6
|
-
html: string;
|
|
7
|
-
/** 是否成功注入 */
|
|
8
|
-
injected: boolean;
|
|
9
|
-
}
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
13
|
+
* 在 HTML 中查找选择器匹配位置
|
|
14
|
+
*
|
|
15
|
+
* @param html - 要搜索的 HTML 字符串
|
|
16
|
+
* @param selector - 选择器字符串(普通字符串或正则表达式字符串)
|
|
17
|
+
* @param selectorMatch - 匹配模式,`'string'` 为精确字符串匹配,`'regex'` 为正则匹配
|
|
18
|
+
* @returns 匹配结果对象(包含 `index` 和 `length`),未匹配时返回 `null`
|
|
19
|
+
*
|
|
20
|
+
* @description 根据匹配模式在 HTML 中查找选择器的位置:
|
|
21
|
+
* - 字符串模式:使用 `indexOf` 进行精确匹配
|
|
22
|
+
* - 正则模式:使用 `RegExp` 进行正则匹配,正则语法错误时返回 `null`
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 字符串匹配
|
|
27
|
+
* findSelectorMatch('<div id="app">content</div>', '<div id="app">')
|
|
28
|
+
* // { index: 0, length: 14 }
|
|
29
|
+
*
|
|
30
|
+
* // 正则匹配
|
|
31
|
+
* findSelectorMatch('<div class="foo">bar</div>', 'class="\\w+"', 'regex')
|
|
32
|
+
* // { index: 5, length: 12 }
|
|
33
|
+
*
|
|
34
|
+
* // 未匹配
|
|
35
|
+
* findSelectorMatch('<div>hello</div>', '<span>')
|
|
36
|
+
* // null
|
|
37
|
+
* ```
|
|
12
38
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
39
|
+
declare function findSelectorMatch(html: string, selector: string, selectorMatch?: SelectorMatch): {
|
|
40
|
+
index: number;
|
|
41
|
+
length: number;
|
|
42
|
+
} | null;
|
|
43
|
+
/**
|
|
44
|
+
* 替换模板字符串中的变量占位符
|
|
45
|
+
*
|
|
46
|
+
* @param content - 包含 `{{变量名}}` 占位符的模板字符串
|
|
47
|
+
* @param ruleVars - 规则级变量映射(优先级高于全局变量)
|
|
48
|
+
* @param globalVars - 全局变量映射
|
|
49
|
+
* @returns 替换所有匹配占位符后的字符串
|
|
50
|
+
*
|
|
51
|
+
* @description 将模板字符串中形如 `{{key}}` 的占位符替换为对应的值。
|
|
52
|
+
* 规则变量(`ruleVars`)优先级高于全局变量(`globalVars`),
|
|
53
|
+
* 当两者存在相同键时,规则变量的值会覆盖全局变量的值。
|
|
54
|
+
* 变量名中的正则特殊字符会被自动转义,替换值中的 `$` 也会被安全处理。
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // 基本替换
|
|
59
|
+
* applyTemplateVars('<div>{{name}}</div>', { name: 'test' })
|
|
60
|
+
* // '<div>test</div>'
|
|
61
|
+
*
|
|
62
|
+
* // 规则变量覆盖全局变量
|
|
63
|
+
* applyTemplateVars('{{a}}-{{b}}', { a: '1' }, { a: '0', b: '2' })
|
|
64
|
+
* // '1-2'
|
|
65
|
+
*
|
|
66
|
+
* // 无匹配占位符
|
|
67
|
+
* applyTemplateVars('no vars', { x: 'y' })
|
|
68
|
+
* // 'no vars'
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function applyTemplateVars(content: string, ruleVars?: Record<string, string>, globalVars?: Record<string, string>): string;
|
|
72
|
+
/**
|
|
73
|
+
* 评估注入条件是否满足
|
|
74
|
+
*
|
|
75
|
+
* @param condition - 注入条件配置
|
|
76
|
+
* @param html - 当前 HTML 内容(用于 `file-contains` 类型判断)
|
|
77
|
+
* @returns 条件是否满足(布尔值)
|
|
78
|
+
*
|
|
79
|
+
* @description 根据条件类型评估注入条件:
|
|
80
|
+
* - `env`:检查环境变量是否存在且不为 `'false'` 或 `'0'`
|
|
81
|
+
* - `file-contains`:检查 HTML 内容是否包含指定字符串
|
|
82
|
+
* - `custom`:执行自定义函数,捕获异常时返回 `false`
|
|
83
|
+
* - 支持通过 `negate` 字段对结果取反
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // 环境变量判断
|
|
88
|
+
* evaluateCondition({ type: 'env', value: 'ENABLE_ANALYTICS' }, '')
|
|
89
|
+
*
|
|
90
|
+
* // 内容包含判断
|
|
91
|
+
* evaluateCondition({ type: 'file-contains', value: '<div id="app">' }, html)
|
|
92
|
+
*
|
|
93
|
+
* // 自定义函数(带取反)
|
|
94
|
+
* evaluateCondition(
|
|
95
|
+
* { type: 'custom', value: () => isDev(), negate: true },
|
|
96
|
+
* html
|
|
97
|
+
* )
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function evaluateCondition(condition: InjectCondition, html: string): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 按优先级排序规则列表
|
|
103
|
+
*
|
|
104
|
+
* @typeParam T - 规则类型,必须包含可选的 `priority` 字段
|
|
105
|
+
* @param rules - 待排序的规则数组
|
|
106
|
+
* @returns 按 `priority` 升序排列的新数组(不修改原数组)
|
|
107
|
+
*
|
|
108
|
+
* @description 将规则按 `priority` 字段升序排列,`priority` 越小优先级越高。
|
|
109
|
+
* 未指定 `priority` 的规则默认优先级为 100。
|
|
110
|
+
* 返回新数组,不修改原数组。
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const rules = [
|
|
115
|
+
* { priority: 30, name: 'C' },
|
|
116
|
+
* { priority: 10, name: 'A' },
|
|
117
|
+
* { name: 'D' }, // 默认 priority=100
|
|
118
|
+
* { priority: 20, name: 'B' }
|
|
119
|
+
* ]
|
|
120
|
+
* sortRulesByPriority(rules)
|
|
121
|
+
* // [{ priority: 10, name: 'A' }, { priority: 20, name: 'B' },
|
|
122
|
+
* // { priority: 30, name: 'C' }, { name: 'D' }]
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare function sortRulesByPriority<T extends {
|
|
126
|
+
priority?: number;
|
|
127
|
+
}>(rules: T[]): T[];
|
|
128
|
+
/**
|
|
129
|
+
* 在 HTML 的指定位置注入内容
|
|
130
|
+
*
|
|
131
|
+
* @param html - 原始 HTML 字符串
|
|
132
|
+
* @param content - 要注入的内容
|
|
133
|
+
* @param position - 注入位置
|
|
134
|
+
* @param selector - 选择器字符串(仅 `before-selector`、`after-selector`、`replace-selector` 位置需要)
|
|
135
|
+
* @param selectorMatch - 选择器匹配模式(默认为字符串匹配)
|
|
136
|
+
* @returns 注入结果对象,包含注入后的 HTML、是否成功标志和失败原因
|
|
137
|
+
*
|
|
138
|
+
* @description 根据 `position` 参数将内容注入到 HTML 的指定位置:
|
|
139
|
+
* - `head-start`:在 `<head>` 标签开始后注入
|
|
140
|
+
* - `head-end`:在 `</head>` 标签前注入
|
|
141
|
+
* - `body-start`:在 `<body>` 标签开始后注入
|
|
142
|
+
* - `body-end`:在 `</body>` 标签前注入
|
|
143
|
+
* - `before-selector`:在选择器匹配位置前注入
|
|
144
|
+
* - `after-selector`:在选择器匹配位置后注入
|
|
145
|
+
* - `replace-selector`:替换选择器匹配的内容
|
|
146
|
+
*
|
|
147
|
+
* 当目标标签或选择器未找到时,返回 `injected: false` 并附带 `reason` 说明。
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const html = '<html><head><title>Test</title></head><body><div id="app"></div></body></html>'
|
|
152
|
+
*
|
|
153
|
+
* // 在 head 开始后注入 meta 标签
|
|
154
|
+
* injectAtPosition(html, '<meta charset="utf-8">', 'head-start')
|
|
155
|
+
*
|
|
156
|
+
* // 在 body 结束前注入脚本
|
|
157
|
+
* injectAtPosition(html, '<script>app()</script>', 'body-end')
|
|
158
|
+
*
|
|
159
|
+
* // 在指定元素前注入
|
|
160
|
+
* injectAtPosition(html, '<nav>menu</nav>', 'before-selector', '<div id="app">')
|
|
161
|
+
*
|
|
162
|
+
* // 替换指定元素
|
|
163
|
+
* injectAtPosition(html, '<div id="root"></div>', 'replace-selector', '<div id="app">')
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
declare function injectAtPosition(html: string, content: string, position: InjectPosition, selector?: string, selectorMatch?: SelectorMatch): PositionInjectResult;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* HTML 安全过滤模块
|
|
170
|
+
*
|
|
171
|
+
* @module common/html/security
|
|
172
|
+
* @description 提供 HTML 内容安全过滤和验证功能,防止 XSS 攻击和危险内容注入,
|
|
173
|
+
* 包括危险标签检测、危险属性过滤和安全配置验证等能力。
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 默认阻止的 HTML 标签列表
|
|
178
|
+
*
|
|
179
|
+
* @description 包含可能导致安全风险的 HTML 标签:
|
|
180
|
+
* - `script`:可执行 JavaScript 代码
|
|
181
|
+
* - `iframe`:可嵌入外部页面
|
|
182
|
+
* - `object`/`embed`/`applet`:可嵌入插件
|
|
183
|
+
* - `form`/`input`/`textarea`/`select`/`button`:可创建表单
|
|
184
|
+
*/
|
|
185
|
+
declare const DEFAULT_BLOCKED_TAGS: string[];
|
|
186
|
+
/**
|
|
187
|
+
* 默认阻止的 HTML 事件属性列表
|
|
188
|
+
*
|
|
189
|
+
* @description 包含所有可能导致 JavaScript 执行的事件处理属性,
|
|
190
|
+
* 涵盖鼠标事件、键盘事件、表单事件、拖拽事件和动画事件等。
|
|
191
|
+
*/
|
|
192
|
+
declare const DEFAULT_BLOCKED_ATTRIBUTES: string[];
|
|
193
|
+
/**
|
|
194
|
+
* 验证安全配置的合法性
|
|
195
|
+
*
|
|
196
|
+
* @param security - 安全配置对象
|
|
197
|
+
* @throws 当 `blockedTags` 不是字符串数组时抛出错误
|
|
198
|
+
* @throws 当 `allowedTags` 不是字符串数组时抛出错误
|
|
199
|
+
* @throws 当 `blockedAttributes` 不是字符串数组时抛出错误
|
|
200
|
+
*
|
|
201
|
+
* @description 检查安全配置中的数组字段是否为合法的字符串数组类型,
|
|
202
|
+
* 传入 `undefined` 时直接跳过验证。
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* // 合法配置
|
|
207
|
+
* validateSecurityConfig({ blockedTags: ['iframe'], allowedTags: ['div'] })
|
|
208
|
+
*
|
|
209
|
+
* // 非法配置(抛出错误)
|
|
210
|
+
* validateSecurityConfig({ blockedTags: 'iframe' })
|
|
211
|
+
* // Error: security.blockedTags 必须是字符串数组
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare function validateSecurityConfig(security?: SecurityConfig): void;
|
|
215
|
+
/**
|
|
216
|
+
* 内容消毒规则选项
|
|
217
|
+
*
|
|
218
|
+
* @description 定义对单条注入规则内容进行安全过滤时的选项,
|
|
219
|
+
* 控制是否允许脚本注入等特殊行为。
|
|
220
|
+
*/
|
|
221
|
+
interface SanitizeRuleOptions {
|
|
222
|
+
/** 规则标识符,用于错误消息和警告日志中标识来源规则 */
|
|
223
|
+
id?: string;
|
|
224
|
+
/**
|
|
225
|
+
* 是否允许注入 `<script>` 标签和被阻止的标签/属性
|
|
226
|
+
*
|
|
227
|
+
* @description 设置为 `true` 时,将跳过对危险标签和属性的安全检查,
|
|
228
|
+
* 但仍会输出警告日志提醒开发者注意安全风险。
|
|
229
|
+
* 仅在注入内容来源可信时使用此选项。
|
|
230
|
+
*
|
|
231
|
+
* @default false
|
|
232
|
+
*/
|
|
233
|
+
allowScriptInjection?: boolean;
|
|
22
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* 对注入内容进行安全过滤
|
|
237
|
+
*
|
|
238
|
+
* @param content - 待过滤的 HTML 内容字符串
|
|
239
|
+
* @param rule - 当前注入规则的消毒选项
|
|
240
|
+
* @param security - 全局安全配置
|
|
241
|
+
* @param logger - 日志记录器(需提供 `warn` 方法),用于输出安全警告
|
|
242
|
+
* @returns 过滤后的安全 HTML 内容字符串
|
|
243
|
+
* @throws 当内容包含被阻止的标签且未启用 `allowScriptInjection` 时抛出错误
|
|
244
|
+
* @throws 当内容包含危险属性且未启用 `allowScriptInjection` 时抛出错误
|
|
245
|
+
*
|
|
246
|
+
* @description 对 HTML 内容进行安全过滤,防止 XSS 攻击:
|
|
247
|
+
* 1. **标签过滤**:检测并阻止默认阻止列表中的标签(如 script、iframe),
|
|
248
|
+
* 可通过 `security.allowedTags` 放行特定标签
|
|
249
|
+
* 2. **属性过滤**:检测并阻止事件处理属性(如 onclick、onerror)
|
|
250
|
+
* 3. **脚本检测**:特别检测 `<script>` 标签,需要显式启用 `allowScriptInjection`
|
|
251
|
+
*
|
|
252
|
+
* 当 `rule.allowScriptInjection` 为 `true` 时,跳过安全检查但输出警告日志。
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // 安全内容直接通过
|
|
257
|
+
* sanitizeContent('<div>safe</div>', { id: 'rule1' })
|
|
258
|
+
*
|
|
259
|
+
* // 包含 script 标签(抛出错误)
|
|
260
|
+
* sanitizeContent('<script>alert(1)</script>', { id: 'rule2' })
|
|
261
|
+
*
|
|
262
|
+
* // 允许脚本注入(通过但输出警告)
|
|
263
|
+
* sanitizeContent('<script>alert(1)</script>', { id: 'rule3', allowScriptInjection: true }, undefined, console)
|
|
264
|
+
*
|
|
265
|
+
* // 自定义安全配置
|
|
266
|
+
* sanitizeContent('<iframe>test</iframe>', { id: 'rule4' }, { allowedTags: ['iframe'] })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare function sanitizeContent(content: string, rule: SanitizeRuleOptions, security?: SecurityConfig, logger?: {
|
|
270
|
+
warn: (msg: string) => void;
|
|
271
|
+
}): string;
|
|
23
272
|
|
|
24
273
|
/**
|
|
25
274
|
* 在 HTML 中指定闭合标签前注入代码
|
|
@@ -105,5 +354,5 @@ declare function injectBeforeTagWithFallback(html: string, code: string, _fallba
|
|
|
105
354
|
*/
|
|
106
355
|
declare function injectHeadAndBody(html: string, headCode: string | undefined, bodyCode: string): DualInjectResult;
|
|
107
356
|
|
|
108
|
-
export { injectBeforeTag, injectBeforeTagWithFallback, injectHeadAndBody, injectHtmlByPriority };
|
|
109
|
-
export type {
|
|
357
|
+
export { DEFAULT_BLOCKED_ATTRIBUTES, DEFAULT_BLOCKED_TAGS, DualInjectResult, HtmlInjectResult, InjectCondition, InjectPosition, PositionInjectResult, SecurityConfig, SelectorMatch, applyTemplateVars, evaluateCondition, findSelectorMatch, injectAtPosition, injectBeforeTag, injectBeforeTagWithFallback, injectHeadAndBody, injectHtmlByPriority, sanitizeContent, sortRulesByPriority, validateSecurityConfig };
|
|
358
|
+
export type { SanitizeRuleOptions };
|