leakshield 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +546 -2
- package/README.zh-TW.md +565 -0
- package/dist/cli.js +61 -0
- package/dist/config/loader.js +94 -0
- package/dist/decision/deidentify.js +195 -0
- package/dist/decision/ingress.js +136 -0
- package/dist/decision/mapping-key.js +318 -0
- package/dist/decision/mapping-store.js +555 -0
- package/dist/decision/model.js +76 -0
- package/dist/decision/pseudonymization.js +1230 -0
- package/dist/decision/redaction.js +185 -0
- package/dist/decision/schema-strings.js +42 -0
- package/dist/doctor.js +438 -0
- package/dist/env-queries.js +584 -0
- package/dist/forget.js +56 -0
- package/dist/help.js +37 -0
- package/dist/i18n/en.js +71 -0
- package/dist/i18n/index.js +28 -0
- package/dist/i18n/zh-TW.js +71 -0
- package/dist/index.js +12 -0
- package/dist/opencode-env-tools.js +243 -0
- package/dist/opencode-hooks.js +84 -0
- package/dist/opencode-transparent-aisdk.js +100 -0
- package/dist/opencode-transparent-http.js +2315 -0
- package/dist/opencode-transparent-stream.js +192 -0
- package/dist/opencode-transparent-ws.js +298 -0
- package/dist/opencode-transparent.js +1492 -0
- package/dist/opencode-v2.js +216 -0
- package/dist/opencode.js +175 -0
- package/dist/plugin-options.js +100 -0
- package/dist/rules/builtin.js +283 -0
- package/dist/rules/dynamic.js +68 -0
- package/dist/scanner/encoding.js +274 -0
- package/dist/scanner/engine.js +321 -0
- package/dist/scanner/exact-values.js +99 -0
- package/dist/scanner/redaction-verifier.js +82 -0
- package/dist/scanner/unicode.js +157 -0
- package/dist/scanner/utils.js +60 -0
- package/dist/transports/cli.js +447 -0
- package/dist/transports/web-ui.js +540 -0
- package/dist/transports/web.js +200 -0
- package/dist/types.js +23 -0
- package/dist/utils/files.js +36 -0
- package/dist/utils/path-security.js +30 -0
- package/dist/utils/win-encoding.js +45 -0
- package/package.json +71 -4
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
export const WEB_UI_HTML = String.raw `<!DOCTYPE html>
|
|
2
|
+
<html lang="zh-TW">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<meta name="color-scheme" content="dark">
|
|
7
|
+
<title>LeakShield | Local Redaction Workbench</title>
|
|
8
|
+
<style>
|
|
9
|
+
:root {
|
|
10
|
+
--bg: #090b0d;
|
|
11
|
+
--surface: #111519;
|
|
12
|
+
--surface-raised: #171c21;
|
|
13
|
+
--line: #283038;
|
|
14
|
+
--line-strong: #3b4650;
|
|
15
|
+
--text: #edf2f4;
|
|
16
|
+
--muted: #8b98a3;
|
|
17
|
+
--dim: #5d6972;
|
|
18
|
+
--accent: #b9f227;
|
|
19
|
+
--accent-ink: #101500;
|
|
20
|
+
--danger: #ff665c;
|
|
21
|
+
--warning: #ffc857;
|
|
22
|
+
--safe: #57d98b;
|
|
23
|
+
--radius: 3px;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
* { box-sizing: border-box; }
|
|
27
|
+
html { min-width: 320px; background: var(--bg); }
|
|
28
|
+
body {
|
|
29
|
+
margin: 0;
|
|
30
|
+
min-height: 100vh;
|
|
31
|
+
color: var(--text);
|
|
32
|
+
background:
|
|
33
|
+
linear-gradient(rgba(255,255,255,.018) 1px, transparent 1px),
|
|
34
|
+
linear-gradient(90deg, rgba(255,255,255,.018) 1px, transparent 1px),
|
|
35
|
+
var(--bg);
|
|
36
|
+
background-size: 32px 32px;
|
|
37
|
+
font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
button, textarea { font: inherit; }
|
|
41
|
+
button:focus-visible, textarea:focus-visible {
|
|
42
|
+
outline: 2px solid var(--accent);
|
|
43
|
+
outline-offset: 2px;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.shell { width: min(1480px, 100%); margin: 0 auto; padding: 28px; }
|
|
47
|
+
.topbar {
|
|
48
|
+
display: flex;
|
|
49
|
+
align-items: flex-start;
|
|
50
|
+
justify-content: space-between;
|
|
51
|
+
gap: 24px;
|
|
52
|
+
margin-bottom: 24px;
|
|
53
|
+
padding-bottom: 20px;
|
|
54
|
+
border-bottom: 1px solid var(--line);
|
|
55
|
+
}
|
|
56
|
+
.brand { display: flex; align-items: center; gap: 13px; }
|
|
57
|
+
.mark {
|
|
58
|
+
width: 38px;
|
|
59
|
+
height: 38px;
|
|
60
|
+
display: grid;
|
|
61
|
+
place-items: center;
|
|
62
|
+
color: var(--accent);
|
|
63
|
+
border: 1px solid var(--accent);
|
|
64
|
+
clip-path: polygon(12% 0, 100% 0, 100% 72%, 72% 100%, 0 100%, 0 12%);
|
|
65
|
+
}
|
|
66
|
+
h1 { margin: 0; font-size: 17px; letter-spacing: .08em; text-transform: uppercase; }
|
|
67
|
+
.tagline { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
|
|
68
|
+
.tagline-part { display: inline-block; }
|
|
69
|
+
.local-badge {
|
|
70
|
+
display: flex;
|
|
71
|
+
align-items: center;
|
|
72
|
+
gap: 8px;
|
|
73
|
+
padding: 7px 10px;
|
|
74
|
+
color: var(--safe);
|
|
75
|
+
border: 1px solid #245d3b;
|
|
76
|
+
background: #0d1d14;
|
|
77
|
+
font-size: 11px;
|
|
78
|
+
letter-spacing: .06em;
|
|
79
|
+
text-transform: uppercase;
|
|
80
|
+
}
|
|
81
|
+
.local-badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
|
|
82
|
+
|
|
83
|
+
.workflow {
|
|
84
|
+
display: grid;
|
|
85
|
+
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
|
|
86
|
+
gap: 16px;
|
|
87
|
+
}
|
|
88
|
+
.panel { min-width: 0; border: 1px solid var(--line); background: rgba(17, 21, 25, .96); }
|
|
89
|
+
.panel-head {
|
|
90
|
+
min-height: 48px;
|
|
91
|
+
padding: 12px 14px;
|
|
92
|
+
display: flex;
|
|
93
|
+
align-items: center;
|
|
94
|
+
justify-content: space-between;
|
|
95
|
+
gap: 12px;
|
|
96
|
+
border-bottom: 1px solid var(--line);
|
|
97
|
+
}
|
|
98
|
+
.panel-title { display: flex; align-items: center; gap: 9px; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; }
|
|
99
|
+
.step { color: var(--accent); }
|
|
100
|
+
.metrics { color: var(--dim); font-size: 11px; white-space: nowrap; }
|
|
101
|
+
textarea {
|
|
102
|
+
display: block;
|
|
103
|
+
width: 100%;
|
|
104
|
+
min-height: 440px;
|
|
105
|
+
padding: 18px;
|
|
106
|
+
resize: vertical;
|
|
107
|
+
border: 0;
|
|
108
|
+
color: #dbe4e8;
|
|
109
|
+
background: #0d1013;
|
|
110
|
+
font-size: 13px;
|
|
111
|
+
line-height: 1.65;
|
|
112
|
+
tab-size: 2;
|
|
113
|
+
white-space: pre-wrap;
|
|
114
|
+
word-break: break-all;
|
|
115
|
+
}
|
|
116
|
+
textarea::placeholder { color: #45515a; }
|
|
117
|
+
#output { color: #c9e7d4; }
|
|
118
|
+
#output:placeholder-shown { background: linear-gradient(135deg, #0d1013, #10151a); }
|
|
119
|
+
|
|
120
|
+
.toolbar {
|
|
121
|
+
min-height: 58px;
|
|
122
|
+
padding: 10px 12px;
|
|
123
|
+
display: flex;
|
|
124
|
+
align-items: center;
|
|
125
|
+
justify-content: space-between;
|
|
126
|
+
gap: 10px;
|
|
127
|
+
border-top: 1px solid var(--line);
|
|
128
|
+
background: var(--surface-raised);
|
|
129
|
+
}
|
|
130
|
+
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
|
|
131
|
+
button {
|
|
132
|
+
min-height: 36px;
|
|
133
|
+
padding: 8px 13px;
|
|
134
|
+
cursor: pointer;
|
|
135
|
+
color: var(--text);
|
|
136
|
+
border: 1px solid var(--line-strong);
|
|
137
|
+
border-radius: var(--radius);
|
|
138
|
+
background: #20262c;
|
|
139
|
+
font-size: 12px;
|
|
140
|
+
}
|
|
141
|
+
button:hover:not(:disabled) { border-color: #687782; background: #283038; }
|
|
142
|
+
button:disabled { cursor: not-allowed; opacity: .42; }
|
|
143
|
+
.primary { color: var(--accent-ink); border-color: var(--accent); background: var(--accent); font-weight: 700; }
|
|
144
|
+
.primary:hover:not(:disabled) { border-color: #d1ff5b; background: #d1ff5b; }
|
|
145
|
+
.shortcut { color: var(--dim); font-size: 10px; }
|
|
146
|
+
|
|
147
|
+
.report {
|
|
148
|
+
margin-top: 16px;
|
|
149
|
+
border: 1px solid var(--line);
|
|
150
|
+
background: rgba(17, 21, 25, .96);
|
|
151
|
+
}
|
|
152
|
+
.report[aria-busy="true"] { position: relative; }
|
|
153
|
+
.report[aria-busy="true"]::after {
|
|
154
|
+
content: "";
|
|
155
|
+
position: absolute;
|
|
156
|
+
top: 14px;
|
|
157
|
+
right: 14px;
|
|
158
|
+
width: 14px;
|
|
159
|
+
height: 14px;
|
|
160
|
+
border: 2px solid var(--line-strong);
|
|
161
|
+
border-top-color: var(--accent);
|
|
162
|
+
border-radius: 50%;
|
|
163
|
+
animation: spin .7s linear infinite;
|
|
164
|
+
}
|
|
165
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
166
|
+
.report-head {
|
|
167
|
+
padding: 14px;
|
|
168
|
+
display: flex;
|
|
169
|
+
align-items: center;
|
|
170
|
+
justify-content: space-between;
|
|
171
|
+
gap: 16px;
|
|
172
|
+
border-bottom: 1px solid var(--line);
|
|
173
|
+
}
|
|
174
|
+
.decision { display: flex; align-items: center; gap: 12px; min-width: 0; }
|
|
175
|
+
.decision-chip {
|
|
176
|
+
padding: 5px 8px;
|
|
177
|
+
color: var(--muted);
|
|
178
|
+
border: 1px solid var(--line-strong);
|
|
179
|
+
font-size: 11px;
|
|
180
|
+
font-weight: 700;
|
|
181
|
+
letter-spacing: .08em;
|
|
182
|
+
white-space: nowrap;
|
|
183
|
+
}
|
|
184
|
+
.decision-chip.ALLOW { color: var(--safe); border-color: #2d7048; }
|
|
185
|
+
.decision-chip.WARN { color: var(--warning); border-color: #765f2a; }
|
|
186
|
+
.decision-chip.CONFIRM { color: var(--danger); border-color: #74342f; }
|
|
187
|
+
.summary { overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
|
|
188
|
+
.status { color: var(--muted); font-size: 11px; text-align: right; }
|
|
189
|
+
.status.error { color: var(--danger); }
|
|
190
|
+
.status.success { color: var(--safe); }
|
|
191
|
+
|
|
192
|
+
.findings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
|
|
193
|
+
.finding {
|
|
194
|
+
min-width: 0;
|
|
195
|
+
padding: 14px;
|
|
196
|
+
border: 0;
|
|
197
|
+
border-right: 1px solid var(--line);
|
|
198
|
+
border-bottom: 1px solid var(--line);
|
|
199
|
+
border-radius: 0;
|
|
200
|
+
background: transparent;
|
|
201
|
+
text-align: left;
|
|
202
|
+
}
|
|
203
|
+
.finding:hover { background: var(--surface-raised); }
|
|
204
|
+
.finding-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
|
|
205
|
+
.severity { font-size: 10px; font-weight: 700; letter-spacing: .06em; }
|
|
206
|
+
.severity.HIGH { color: var(--danger); }
|
|
207
|
+
.severity.MEDIUM { color: var(--warning); }
|
|
208
|
+
.severity.LOW { color: #8ea1ff; }
|
|
209
|
+
.location { color: var(--dim); font-size: 10px; }
|
|
210
|
+
.finding-type { display: block; margin-top: 8px; font-size: 12px; }
|
|
211
|
+
.finding-desc { display: block; margin-top: 4px; color: var(--muted); font-size: 11px; line-height: 1.5; }
|
|
212
|
+
.empty { padding: 28px 14px; color: var(--dim); font-size: 12px; text-align: center; }
|
|
213
|
+
.notice { padding: 10px 14px; color: var(--warning); border-top: 1px solid #58491f; background: #1b180d; font-size: 11px; }
|
|
214
|
+
|
|
215
|
+
@media (max-width: 980px) {
|
|
216
|
+
.workflow { grid-template-columns: 1fr; }
|
|
217
|
+
textarea { min-height: 330px; }
|
|
218
|
+
.findings { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
|
219
|
+
}
|
|
220
|
+
@media (max-width: 620px) {
|
|
221
|
+
.shell { padding: 16px; }
|
|
222
|
+
.topbar { align-items: stretch; flex-direction: column; }
|
|
223
|
+
.local-badge { align-self: flex-start; }
|
|
224
|
+
textarea { min-height: 280px; }
|
|
225
|
+
.toolbar, .report-head { align-items: stretch; flex-direction: column; }
|
|
226
|
+
.decision { align-items: flex-start; flex-wrap: wrap; }
|
|
227
|
+
.summary { width: 100%; overflow: visible; text-overflow: clip; white-space: normal; line-height: 1.5; }
|
|
228
|
+
.toolbar .actions { width: 100%; }
|
|
229
|
+
.toolbar button { flex: 1; }
|
|
230
|
+
.shortcut, .status { text-align: left; }
|
|
231
|
+
.findings { grid-template-columns: 1fr; }
|
|
232
|
+
}
|
|
233
|
+
</style>
|
|
234
|
+
</head>
|
|
235
|
+
<body>
|
|
236
|
+
<main class="shell">
|
|
237
|
+
<header class="topbar">
|
|
238
|
+
<div class="brand">
|
|
239
|
+
<div class="mark" aria-hidden="true">SG</div>
|
|
240
|
+
<div>
|
|
241
|
+
<h1>LeakShield</h1>
|
|
242
|
+
<p class="tagline"><span class="tagline-part">本機敏感內容檢查台</span> <span class="tagline-part" lang="en">/ Local redaction workbench</span></p>
|
|
243
|
+
</div>
|
|
244
|
+
</div>
|
|
245
|
+
<div class="local-badge"><span lang="en">Local only</span> · 127.0.0.1</div>
|
|
246
|
+
</header>
|
|
247
|
+
|
|
248
|
+
<section class="workflow" aria-label="敏感內容處理流程">
|
|
249
|
+
<article class="panel">
|
|
250
|
+
<div class="panel-head">
|
|
251
|
+
<div class="panel-title"><span class="step">01</span><span>原始內容</span></div>
|
|
252
|
+
<span class="metrics" id="inputMetrics">0 字元 · 0 行</span>
|
|
253
|
+
</div>
|
|
254
|
+
<textarea id="input" aria-label="原始內容" spellcheck="false" placeholder="貼上準備傳給 AI 的程式碼、設定或文件…"></textarea>
|
|
255
|
+
<div class="toolbar">
|
|
256
|
+
<div class="actions">
|
|
257
|
+
<button class="primary" id="scanBtn" type="button">掃描內容</button>
|
|
258
|
+
<button id="cancelBtn" type="button" hidden>取消</button>
|
|
259
|
+
<button id="sampleBtn" type="button">載入範例</button>
|
|
260
|
+
<button id="clearBtn" type="button">清除</button>
|
|
261
|
+
</div>
|
|
262
|
+
<span class="shortcut"><span lang="en">⌘/Ctrl + Enter</span> · 上限 512 KB</span>
|
|
263
|
+
</div>
|
|
264
|
+
</article>
|
|
265
|
+
|
|
266
|
+
<article class="panel">
|
|
267
|
+
<div class="panel-head">
|
|
268
|
+
<div class="panel-title"><span class="step">02</span><span>安全版本</span></div>
|
|
269
|
+
<span class="metrics" id="outputMetrics">等待掃描</span>
|
|
270
|
+
</div>
|
|
271
|
+
<textarea id="output" aria-label="脫敏後內容" readonly placeholder="掃描後的安全內容會出現在這裡"></textarea>
|
|
272
|
+
<div class="toolbar">
|
|
273
|
+
<div class="actions">
|
|
274
|
+
<button class="primary" id="copyBtn" type="button" disabled>脫敏並複製</button>
|
|
275
|
+
</div>
|
|
276
|
+
<span class="status" id="copyStatus" aria-live="polite"></span>
|
|
277
|
+
</div>
|
|
278
|
+
</article>
|
|
279
|
+
</section>
|
|
280
|
+
|
|
281
|
+
<section class="report" id="report" aria-labelledby="reportTitle">
|
|
282
|
+
<div class="report-head">
|
|
283
|
+
<div class="decision">
|
|
284
|
+
<span class="panel-title" id="reportTitle">風險判定</span>
|
|
285
|
+
<span class="decision-chip" id="decisionChip"><span lang="en">NOT SCANNED</span></span>
|
|
286
|
+
<span class="summary" id="summary">貼上內容後執行掃描</span>
|
|
287
|
+
</div>
|
|
288
|
+
<span class="status" id="scanStatus" aria-live="polite"></span>
|
|
289
|
+
</div>
|
|
290
|
+
<div class="findings" id="findings" aria-live="polite" role="status"><div class="empty">尚無掃描結果</div></div>
|
|
291
|
+
<div class="notice" id="notice" hidden></div>
|
|
292
|
+
</section>
|
|
293
|
+
</main>
|
|
294
|
+
|
|
295
|
+
<script>
|
|
296
|
+
const MAX_BODY_BYTES = 512 * 1024;
|
|
297
|
+
const input = document.getElementById('input');
|
|
298
|
+
const output = document.getElementById('output');
|
|
299
|
+
const scanBtn = document.getElementById('scanBtn');
|
|
300
|
+
const cancelBtn = document.getElementById('cancelBtn');
|
|
301
|
+
const copyBtn = document.getElementById('copyBtn');
|
|
302
|
+
const scanStatus = document.getElementById('scanStatus');
|
|
303
|
+
const copyStatus = document.getElementById('copyStatus');
|
|
304
|
+
const decisionChip = document.getElementById('decisionChip');
|
|
305
|
+
const summary = document.getElementById('summary');
|
|
306
|
+
const findings = document.getElementById('findings');
|
|
307
|
+
const notice = document.getElementById('notice');
|
|
308
|
+
const report = document.getElementById('report');
|
|
309
|
+
let activeController = null;
|
|
310
|
+
let metricsTimer = null;
|
|
311
|
+
|
|
312
|
+
function replaceFindingsWithMessage(message) {
|
|
313
|
+
const empty = document.createElement('div');
|
|
314
|
+
empty.className = 'empty';
|
|
315
|
+
empty.textContent = message;
|
|
316
|
+
findings.replaceChildren(empty);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function createFindingElement(finding) {
|
|
320
|
+
const button = document.createElement('button');
|
|
321
|
+
button.className = 'finding';
|
|
322
|
+
button.type = 'button';
|
|
323
|
+
button.dataset.line = finding.line ? String(Number(finding.line)) : '';
|
|
324
|
+
|
|
325
|
+
const top = document.createElement('span');
|
|
326
|
+
top.className = 'finding-top';
|
|
327
|
+
|
|
328
|
+
const severity = document.createElement('span');
|
|
329
|
+
severity.className = 'severity ' + String(finding.severity);
|
|
330
|
+
severity.textContent = String(finding.severity);
|
|
331
|
+
|
|
332
|
+
const location = document.createElement('span');
|
|
333
|
+
location.className = 'location';
|
|
334
|
+
location.textContent = finding.line ? '第 ' + Number(finding.line) + ' 行' : '位置未提供';
|
|
335
|
+
top.append(severity, location);
|
|
336
|
+
|
|
337
|
+
const type = document.createElement('span');
|
|
338
|
+
type.className = 'finding-type';
|
|
339
|
+
type.textContent = String(finding.type);
|
|
340
|
+
|
|
341
|
+
const description = document.createElement('span');
|
|
342
|
+
description.className = 'finding-desc';
|
|
343
|
+
description.textContent = String(finding.description);
|
|
344
|
+
|
|
345
|
+
button.append(top, type, description);
|
|
346
|
+
return button;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function updateMetrics() {
|
|
350
|
+
const content = input.value;
|
|
351
|
+
const lines = content ? content.split('\n').length : 0;
|
|
352
|
+
document.getElementById('inputMetrics').textContent = content.length + ' 字元 · ' + lines + ' 行';
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function scheduleMetrics() {
|
|
356
|
+
clearTimeout(metricsTimer);
|
|
357
|
+
metricsTimer = setTimeout(updateMetrics, 150);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function setStatus(element, message, type) {
|
|
361
|
+
element.textContent = message;
|
|
362
|
+
element.className = 'status' + (type ? ' ' + type : '');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function resetResult() {
|
|
366
|
+
output.value = '';
|
|
367
|
+
copyBtn.disabled = true;
|
|
368
|
+
copyBtn.textContent = '脫敏並複製';
|
|
369
|
+
decisionChip.textContent = 'NOT SCANNED';
|
|
370
|
+
decisionChip.className = 'decision-chip';
|
|
371
|
+
summary.textContent = '貼上內容後執行掃描';
|
|
372
|
+
replaceFindingsWithMessage('尚無掃描結果');
|
|
373
|
+
document.getElementById('outputMetrics').textContent = '等待掃描';
|
|
374
|
+
notice.hidden = true;
|
|
375
|
+
notice.textContent = '';
|
|
376
|
+
setStatus(scanStatus, '', '');
|
|
377
|
+
setStatus(copyStatus, '', '');
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function lineBounds(text, line) {
|
|
381
|
+
const target = Number(line);
|
|
382
|
+
if (!target || target < 1) return [0, 0];
|
|
383
|
+
let start = 0;
|
|
384
|
+
for (let current = 1; current < target; current += 1) {
|
|
385
|
+
const next = text.indexOf('\n', start);
|
|
386
|
+
if (next === -1) return [text.length, text.length];
|
|
387
|
+
start = next + 1;
|
|
388
|
+
}
|
|
389
|
+
const end = text.indexOf('\n', start);
|
|
390
|
+
return [start, end === -1 ? text.length : end];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function focusFinding(line) {
|
|
394
|
+
const [start, end] = lineBounds(input.value, line);
|
|
395
|
+
input.focus();
|
|
396
|
+
input.setSelectionRange(start, Math.max(start, end));
|
|
397
|
+
input.scrollTop = input.scrollHeight * (start / Math.max(1, input.value.length));
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function renderResult(data) {
|
|
401
|
+
decisionChip.textContent = data.decision + ' · ' + data.riskLevel;
|
|
402
|
+
decisionChip.className = 'decision-chip ' + data.decision;
|
|
403
|
+
summary.textContent = data.summary;
|
|
404
|
+
report.setAttribute('aria-busy', 'false');
|
|
405
|
+
|
|
406
|
+
const isAllowed = data.decision === 'ALLOW';
|
|
407
|
+
const safeContent = isAllowed ? input.value : (data.redactedContent || '');
|
|
408
|
+
output.value = safeContent;
|
|
409
|
+
document.getElementById('outputMetrics').textContent = safeContent.length + ' 字元';
|
|
410
|
+
|
|
411
|
+
const truncated = data.wasTruncated === true;
|
|
412
|
+
const redactionFailed = data.redactionVerified === false;
|
|
413
|
+
copyBtn.disabled = !safeContent || redactionFailed || truncated;
|
|
414
|
+
copyBtn.textContent = isAllowed ? '複製原始內容' : '脫敏並複製';
|
|
415
|
+
|
|
416
|
+
if (!data.findings.length) {
|
|
417
|
+
replaceFindingsWithMessage('未偵測到敏感內容');
|
|
418
|
+
} else {
|
|
419
|
+
findings.replaceChildren(...data.findings.map(createFindingElement));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const showNotice = redactionFailed || truncated;
|
|
423
|
+
notice.hidden = !showNotice;
|
|
424
|
+
notice.textContent = redactionFailed
|
|
425
|
+
? '脫敏驗證未通過,安全版本不可複製。請縮小內容或手動檢查殘留項目。'
|
|
426
|
+
: truncated
|
|
427
|
+
? '內容超過掃描上限,僅檢查了部分內容。請縮小範圍後重新掃描。'
|
|
428
|
+
: '';
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async function scan() {
|
|
432
|
+
const content = input.value;
|
|
433
|
+
if (!content.trim()) {
|
|
434
|
+
setStatus(scanStatus, '請先輸入內容', 'error');
|
|
435
|
+
input.focus();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (content.length > MAX_BODY_BYTES) {
|
|
439
|
+
setStatus(scanStatus, '內容超過 512 KB 上限,請縮小後再掃描', 'error');
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (activeController) activeController.abort();
|
|
444
|
+
activeController = new AbortController();
|
|
445
|
+
scanBtn.disabled = true;
|
|
446
|
+
cancelBtn.hidden = false;
|
|
447
|
+
copyBtn.disabled = true;
|
|
448
|
+
report.setAttribute('aria-busy', 'true');
|
|
449
|
+
setStatus(scanStatus, '掃描中…', '');
|
|
450
|
+
try {
|
|
451
|
+
const response = await fetch('/scan', {
|
|
452
|
+
method: 'POST',
|
|
453
|
+
headers: { 'Content-Type': 'application/json' },
|
|
454
|
+
body: JSON.stringify({ content }),
|
|
455
|
+
signal: activeController.signal,
|
|
456
|
+
});
|
|
457
|
+
const data = await response.json();
|
|
458
|
+
if (!response.ok) throw new Error(data.error || '掃描失敗');
|
|
459
|
+
renderResult(data);
|
|
460
|
+
setStatus(scanStatus, data.findings.length + ' 個項目', data.decision === 'ALLOW' && !data.wasTruncated ? 'success' : '');
|
|
461
|
+
document.getElementById('reportTitle').scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
462
|
+
} catch (error) {
|
|
463
|
+
if (error instanceof DOMException && error.name === 'AbortError') {
|
|
464
|
+
resetResult();
|
|
465
|
+
setStatus(scanStatus, '已取消掃描', 'error');
|
|
466
|
+
} else {
|
|
467
|
+
resetResult();
|
|
468
|
+
setStatus(scanStatus, error instanceof Error ? error.message : '掃描失敗', 'error');
|
|
469
|
+
}
|
|
470
|
+
} finally {
|
|
471
|
+
activeController = null;
|
|
472
|
+
scanBtn.disabled = false;
|
|
473
|
+
cancelBtn.hidden = true;
|
|
474
|
+
report.setAttribute('aria-busy', 'false');
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function cancelScan() {
|
|
479
|
+
if (activeController) activeController.abort();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async function copyOutput() {
|
|
483
|
+
if (!output.value) return;
|
|
484
|
+
try {
|
|
485
|
+
await navigator.clipboard.writeText(output.value);
|
|
486
|
+
setStatus(copyStatus, '已複製安全版本', 'success');
|
|
487
|
+
return;
|
|
488
|
+
} catch {
|
|
489
|
+
// fall through to legacy path
|
|
490
|
+
}
|
|
491
|
+
output.focus();
|
|
492
|
+
output.select();
|
|
493
|
+
try {
|
|
494
|
+
if (document.execCommand('copy')) {
|
|
495
|
+
setStatus(copyStatus, '已複製安全版本', 'success');
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
} catch {
|
|
499
|
+
// execCommand unavailable
|
|
500
|
+
}
|
|
501
|
+
setStatus(copyStatus, '請按 ⌘/Ctrl + C 複製', 'error');
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
input.addEventListener('input', () => {
|
|
505
|
+
scheduleMetrics();
|
|
506
|
+
if (output.value) resetResult();
|
|
507
|
+
});
|
|
508
|
+
input.addEventListener('keydown', event => {
|
|
509
|
+
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') scan();
|
|
510
|
+
});
|
|
511
|
+
scanBtn.addEventListener('click', scan);
|
|
512
|
+
cancelBtn.addEventListener('click', cancelScan);
|
|
513
|
+
copyBtn.addEventListener('click', copyOutput);
|
|
514
|
+
document.getElementById('clearBtn').addEventListener('click', () => {
|
|
515
|
+
input.value = '';
|
|
516
|
+
updateMetrics();
|
|
517
|
+
resetResult();
|
|
518
|
+
input.focus();
|
|
519
|
+
});
|
|
520
|
+
document.getElementById('sampleBtn').addEventListener('click', () => {
|
|
521
|
+
input.value = [
|
|
522
|
+
'const apiKey = "sk-' + 'demo'.repeat(6) + '";',
|
|
523
|
+
'const dbUrl = " Server=10.0.0.1;User=admin;Password=s3cr3t; ";',
|
|
524
|
+
'const token = "ghp_' + 'x'.repeat(20) + '";',
|
|
525
|
+
'const contact = { email: "user@example.com", phone: "0912345678" };',
|
|
526
|
+
'const awsKey = "AKIA" + "1234567890";',
|
|
527
|
+
'const message = "hello world";'
|
|
528
|
+
].join('\n');
|
|
529
|
+
updateMetrics();
|
|
530
|
+
resetResult();
|
|
531
|
+
input.focus();
|
|
532
|
+
});
|
|
533
|
+
findings.addEventListener('click', event => {
|
|
534
|
+
const target = event.target.closest('[data-line]');
|
|
535
|
+
if (target && target.dataset.line) focusFinding(target.dataset.line);
|
|
536
|
+
});
|
|
537
|
+
updateMetrics();
|
|
538
|
+
</script>
|
|
539
|
+
</body>
|
|
540
|
+
</html>`;
|