shapewall 0.0.0-stage → 0.2.2
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/CHANGELOG.md +139 -0
- package/Dockerfile.agent +45 -0
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +651 -2
- package/THIRD_PARTY.md +165 -0
- package/bin/shapewall.js +29 -0
- package/dist/agent/package-lock.json +8384 -0
- package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
- package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
- package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
- package/dist/client/assets/History-CEv6b-av.js +1 -0
- package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
- package/dist/client/assets/Management-DCfhcEUY.js +1 -0
- package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
- package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
- package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
- package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
- package/dist/client/assets/index-ByQRKVit.css +2 -0
- package/dist/client/assets/index-DweQ8Qtu.js +29 -0
- package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
- package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
- package/dist/client/assets/square-A8EnIYLR.js +1 -0
- package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
- package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
- package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
- package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
- package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
- package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
- package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
- package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
- package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
- package/dist/client/assets/ui-CLbws0rx.js +41 -0
- package/dist/client/build-info.json +1 -0
- package/dist/client/favicon.svg +7 -0
- package/dist/client/icons/apple-touch-icon.png +0 -0
- package/dist/client/icons/icon-192.png +0 -0
- package/dist/client/icons/icon-512.png +0 -0
- package/dist/client/icons/maskable-512.png +0 -0
- package/dist/client/index.html +25 -0
- package/dist/client/manifest.webmanifest +15 -0
- package/dist/client/sw.js +53 -0
- package/dist/client/theme-boot.js +27 -0
- package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
- package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
- package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
- package/dist/console/assets/console-C7uJLLOn.js +51 -0
- package/dist/console/assets/console-R8IBY3Am.css +2 -0
- package/dist/console/build-info.json +1 -0
- package/dist/console/console.html +21 -0
- package/dist/console/favicon.svg +7 -0
- package/dist/console/icons/apple-touch-icon.png +0 -0
- package/dist/console/icons/icon-192.png +0 -0
- package/dist/console/icons/icon-512.png +0 -0
- package/dist/console/icons/maskable-512.png +0 -0
- package/dist/console/manifest.webmanifest +15 -0
- package/dist/console/theme-boot.js +27 -0
- package/docs/agent-hardening.md +342 -0
- package/docs/agent-recipes.md +187 -0
- package/docs/agent-workflow.md +320 -0
- package/docs/agents.md +894 -0
- package/docs/architecture.md +242 -0
- package/docs/buzz.md +140 -0
- package/docs/capture-warnings.md +57 -0
- package/docs/config.md +489 -0
- package/docs/deployment.md +289 -0
- package/docs/design-system.md +162 -0
- package/docs/github.md +107 -0
- package/docs/hosted.md +764 -0
- package/docs/images/board-overview.png +0 -0
- package/docs/images/journeys.png +0 -0
- package/docs/images/live-comment.png +0 -0
- package/docs/images/phone-comment.png +0 -0
- package/docs/images/phone-overview.png +0 -0
- package/docs/images/variant.png +0 -0
- package/docs/interaction-audits.md +40 -0
- package/docs/journeys.md +137 -0
- package/docs/local-accounts.md +80 -0
- package/docs/notifications.md +237 -0
- package/docs/page-checks.md +69 -0
- package/docs/performance.md +174 -0
- package/docs/starter-agent.md +325 -0
- package/docs/updates.md +87 -0
- package/examples/audits/interaction.audit.json +17 -0
- package/package.json +132 -4
- package/src/accounts/avatar.js +121 -0
- package/src/accounts/local.js +464 -0
- package/src/agent/acp/command.js +204 -0
- package/src/agent/acp/detect.js +75 -0
- package/src/agent/acp/sandbox.js +160 -0
- package/src/agent/agent-health.js +75 -0
- package/src/agent/backends/acp.js +1297 -0
- package/src/agent/backends/index.js +61 -0
- package/src/agent/backends/openai.js +308 -0
- package/src/agent/composer.js +221 -0
- package/src/agent/config.js +338 -0
- package/src/agent/context.js +191 -0
- package/src/agent/control.js +122 -0
- package/src/agent/errors.js +17 -0
- package/src/agent/feedback-loop.js +137 -0
- package/src/agent/manage.js +94 -0
- package/src/agent/next-steps.js +51 -0
- package/src/agent/owner-mcp.js +38 -0
- package/src/agent/plain-text.js +52 -0
- package/src/agent/ratings-core.js +213 -0
- package/src/agent/ratings.js +100 -0
- package/src/agent/reply-text.js +67 -0
- package/src/agent/routes.js +1065 -0
- package/src/agent/run.js +326 -0
- package/src/agent/settings-routes.js +704 -0
- package/src/agent/showable.js +23 -0
- package/src/agent/starter/approvals.js +181 -0
- package/src/agent/starter/build-image.js +90 -0
- package/src/agent/starter/cli.js +163 -0
- package/src/agent/starter/disk.js +53 -0
- package/src/agent/starter/doctor.js +180 -0
- package/src/agent/starter/egress-proxy.js +351 -0
- package/src/agent/starter/gate.js +90 -0
- package/src/agent/starter/loop.js +114 -0
- package/src/agent/starter/model-relay.js +289 -0
- package/src/agent/starter/pause.js +14 -0
- package/src/agent/starter/probe.js +272 -0
- package/src/agent/starter/prompt.js +41 -0
- package/src/agent/starter/push-guard.js +146 -0
- package/src/agent/starter/rpc.js +164 -0
- package/src/agent/starter/runtime.js +287 -0
- package/src/agent/starter/sandbox-main.js +121 -0
- package/src/agent/starter/settings.js +290 -0
- package/src/agent/starter/source.js +69 -0
- package/src/agent/starter/supervisor.js +755 -0
- package/src/agent/starter/sweep.js +46 -0
- package/src/agent/starter/tools.js +524 -0
- package/src/agent/starter/watch.js +84 -0
- package/src/agent/starter/worktree.js +409 -0
- package/src/agent/text-diff.js +210 -0
- package/src/agent/thread-history.js +223 -0
- package/src/agent/threads.js +399 -0
- package/src/agent/tools.js +200 -0
- package/src/agent/turn-changes.js +424 -0
- package/src/agent/undo-state.js +26 -0
- package/src/agent/user-settings.js +308 -0
- package/src/auth/record.js +375 -0
- package/src/broken-links.js +164 -0
- package/src/buzz/client.js +75 -0
- package/src/buzz/config.js +151 -0
- package/src/buzz/environment.js +13 -0
- package/src/buzz/gateway.js +192 -0
- package/src/buzz/routes.js +61 -0
- package/src/buzz/signer-child.js +205 -0
- package/src/buzz/store.js +89 -0
- package/src/buzz/transport.js +60 -0
- package/src/buzz/verify.js +140 -0
- package/src/capture/actions.js +159 -0
- package/src/capture/capture.js +776 -0
- package/src/capture/dom-names.js +189 -0
- package/src/capture/element-map.js +509 -0
- package/src/capture/live.js +827 -0
- package/src/capture/media-scheme.js +317 -0
- package/src/capture/network-summary.js +86 -0
- package/src/capture/pacing.js +34 -0
- package/src/capture/requests.js +167 -0
- package/src/capture/sanitize.js +752 -0
- package/src/checks/accessibility.js +134 -0
- package/src/checks/budgets.js +58 -0
- package/src/checks/cli.js +55 -0
- package/src/checks/comparison.js +113 -0
- package/src/checks/environments.js +32 -0
- package/src/checks/flow-config.js +82 -0
- package/src/checks/flow-origin.js +53 -0
- package/src/checks/flows.js +398 -0
- package/src/checks/lighthouse-auth.js +145 -0
- package/src/checks/lighthouse-findings.js +130 -0
- package/src/checks/lighthouse.js +395 -0
- package/src/checks/robots.js +85 -0
- package/src/checks/routes.js +52 -0
- package/src/checks/run-control.js +259 -0
- package/src/checks/runner.js +724 -0
- package/src/checks/schema.js +133 -0
- package/src/checks/seo-analysis.js +443 -0
- package/src/checks/seo-probe.js +190 -0
- package/src/checks/seo-snapshot.js +99 -0
- package/src/checks/store.js +214 -0
- package/src/checks/transport.js +47 -0
- package/src/cli/args.js +319 -0
- package/src/cli/capture.js +79 -0
- package/src/cli/commands.js +92 -0
- package/src/cli/comments.js +84 -0
- package/src/cli/help.js +243 -0
- package/src/cli/init.js +214 -0
- package/src/cli/misc.js +64 -0
- package/src/cli/notify.js +87 -0
- package/src/cli/open.js +22 -0
- package/src/cli/serve.js +274 -0
- package/src/cli/variant.js +82 -0
- package/src/cli/watch.js +70 -0
- package/src/cli.js +130 -0
- package/src/comment-fields.js +76 -0
- package/src/comments-cli.js +208 -0
- package/src/config-reload.js +100 -0
- package/src/config.js +1061 -0
- package/src/devices-cli.js +21 -0
- package/src/diff.js +95 -0
- package/src/discover.js +284 -0
- package/src/doctor.js +295 -0
- package/src/event-log.js +268 -0
- package/src/events-cli.js +102 -0
- package/src/events.js +113 -0
- package/src/export.js +119 -0
- package/src/feedback.js +246 -0
- package/src/github.js +551 -0
- package/src/hosted/LICENSE.md +105 -0
- package/src/hosted/access-cli.js +78 -0
- package/src/hosted/admin.js +682 -0
- package/src/hosted/agent-approvals.js +163 -0
- package/src/hosted/agent-policy.js +189 -0
- package/src/hosted/app.js +510 -0
- package/src/hosted/auth-routes.js +540 -0
- package/src/hosted/auth.js +536 -0
- package/src/hosted/backup.js +483 -0
- package/src/hosted/board-context.js +35 -0
- package/src/hosted/board-routes.js +486 -0
- package/src/hosted/board-view.js +94 -0
- package/src/hosted/clean-worker.js +24 -0
- package/src/hosted/cleaner.js +89 -0
- package/src/hosted/comments.js +355 -0
- package/src/hosted/console-routes.js +401 -0
- package/src/hosted/cookies.js +18 -0
- package/src/hosted/crypto.js +80 -0
- package/src/hosted/db.js +496 -0
- package/src/hosted/device-routes.js +175 -0
- package/src/hosted/device.js +233 -0
- package/src/hosted/events.js +63 -0
- package/src/hosted/guards.js +172 -0
- package/src/hosted/guest-view.js +65 -0
- package/src/hosted/http.js +39 -0
- package/src/hosted/import.js +300 -0
- package/src/hosted/mail.js +73 -0
- package/src/hosted/mcp-http.js +71 -0
- package/src/hosted/membership.js +114 -0
- package/src/hosted/operator.js +478 -0
- package/src/hosted/ops-cli.js +345 -0
- package/src/hosted/pages.js +146 -0
- package/src/hosted/permissions.js +113 -0
- package/src/hosted/presence.js +372 -0
- package/src/hosted/projects.js +124 -0
- package/src/hosted/public.js +109 -0
- package/src/hosted/push.js +622 -0
- package/src/hosted/quotas.js +115 -0
- package/src/hosted/ratelimit.js +96 -0
- package/src/hosted/rebind.js +316 -0
- package/src/hosted/retention.js +128 -0
- package/src/hosted/server.js +260 -0
- package/src/hosted/setup-link.js +64 -0
- package/src/hosted/setup.js +323 -0
- package/src/hosted/shares.js +444 -0
- package/src/hosted/tailscale.js +204 -0
- package/src/hosted/tenancy.js +301 -0
- package/src/hosted/token-api.js +455 -0
- package/src/hosted/token-events.js +60 -0
- package/src/hosted/tokens.js +57 -0
- package/src/hosted/turnstile.js +49 -0
- package/src/hosted/usercontent.js +154 -0
- package/src/hosted/zip.js +155 -0
- package/src/invocation.js +58 -0
- package/src/journey-paths.js +282 -0
- package/src/journeys.js +189 -0
- package/src/links.js +106 -0
- package/src/local-projects.js +182 -0
- package/src/lock.js +193 -0
- package/src/mcp.js +417 -0
- package/src/mentions.js +151 -0
- package/src/net.js +15 -0
- package/src/notify/config.js +275 -0
- package/src/notify/index.js +301 -0
- package/src/notify/lease.js +73 -0
- package/src/notify/messages.js +212 -0
- package/src/notify/run.js +174 -0
- package/src/pixeldiff.js +49 -0
- package/src/png.js +143 -0
- package/src/reactions.js +111 -0
- package/src/remote/cli.js +133 -0
- package/src/remote/credentials.js +62 -0
- package/src/remote/ids.js +12 -0
- package/src/remote/login.js +155 -0
- package/src/remote/remote-mcp.js +237 -0
- package/src/remote/remote-watch.js +291 -0
- package/src/remote/remote.js +313 -0
- package/src/remote/share-state.js +78 -0
- package/src/remote/share.js +369 -0
- package/src/remote/terminal.js +13 -0
- package/src/serve-state.js +64 -0
- package/src/server/app.js +824 -0
- package/src/server/devices.js +186 -0
- package/src/server/local-console.js +28 -0
- package/src/server/local-user.js +59 -0
- package/src/server/pairing-routes.js +159 -0
- package/src/server/share-routes.js +89 -0
- package/src/server/tailscale.js +97 -0
- package/src/sitemap.js +431 -0
- package/src/store.js +983 -0
- package/src/tokens.js +11 -0
- package/src/update/routes.js +245 -0
- package/src/watch.js +403 -0
- package/src/which.js +58 -0
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
// Live HTML sanitizer. A captured page's DOM snapshot becomes one
|
|
2
|
+
// self-contained document that Shapewall serves into a sandboxed iframe. This
|
|
3
|
+
// is the strict, allowlist sanitizer that runs in Node on every snapshot
|
|
4
|
+
// before it is written (the in-page snapshot already drops scripts; this pass
|
|
5
|
+
// does not trust it).
|
|
6
|
+
//
|
|
7
|
+
// - Parsed with parse5 (the HTML spec parser), so what we check is exactly
|
|
8
|
+
// what a browser will build. Serialized back with parse5.
|
|
9
|
+
// - Elements: allowlist. Dangerous ones (script, noscript, iframe, object,
|
|
10
|
+
// embed, base, link, template, audio/video, svg animate/set/foreignObject,
|
|
11
|
+
// ...) are dropped with their content; unknown elements are unwrapped.
|
|
12
|
+
// - Attributes: allowlist per element; every `on*` handler, srcset, target,
|
|
13
|
+
// formaction and friends are dropped. URLs: links keep http(s)/mailto/tel/#
|
|
14
|
+
// only (absolute against the page URL); images keep data:image/* only.
|
|
15
|
+
// - Hidden inputs are dropped and password/hidden values are never kept, so
|
|
16
|
+
// CSRF tokens and typed secrets do not leave the page.
|
|
17
|
+
// - CSS (<style> text and style attributes): @import, expression(),
|
|
18
|
+
// -moz-binding and behavior are removed; url() keeps only data: images and
|
|
19
|
+
// fonts and #fragments. Remote URLs are handed to `resolveUrl`, which the
|
|
20
|
+
// capture uses to inline what it fetched (or to drop it).
|
|
21
|
+
// - meta: only charset and name=viewport survive (no refresh).
|
|
22
|
+
//
|
|
23
|
+
// The result is also served with a CSP that allows no script at all and is
|
|
24
|
+
// framed with sandbox="allow-same-origin" (no allow-scripts): three layers.
|
|
25
|
+
|
|
26
|
+
import { parse, serialize } from 'parse5'
|
|
27
|
+
import { CLOBBER_NAMES } from './dom-names.js'
|
|
28
|
+
|
|
29
|
+
const DROP_WITH_CONTENT = new Set([
|
|
30
|
+
'script',
|
|
31
|
+
'noscript',
|
|
32
|
+
'iframe',
|
|
33
|
+
'frame',
|
|
34
|
+
'frameset',
|
|
35
|
+
'object',
|
|
36
|
+
'embed',
|
|
37
|
+
'applet',
|
|
38
|
+
'base',
|
|
39
|
+
'link',
|
|
40
|
+
'template',
|
|
41
|
+
'audio',
|
|
42
|
+
'video',
|
|
43
|
+
'source',
|
|
44
|
+
'track',
|
|
45
|
+
'canvas',
|
|
46
|
+
'portal',
|
|
47
|
+
'param',
|
|
48
|
+
'math',
|
|
49
|
+
'dialog',
|
|
50
|
+
// SVG elements that can run or load things
|
|
51
|
+
'foreignobject',
|
|
52
|
+
'animate',
|
|
53
|
+
'animatemotion',
|
|
54
|
+
'animatetransform',
|
|
55
|
+
'set',
|
|
56
|
+
'discard',
|
|
57
|
+
'handler',
|
|
58
|
+
'listener',
|
|
59
|
+
])
|
|
60
|
+
|
|
61
|
+
const HTML_ELEMENTS = new Set([
|
|
62
|
+
'html',
|
|
63
|
+
'head',
|
|
64
|
+
'body',
|
|
65
|
+
'title',
|
|
66
|
+
'meta',
|
|
67
|
+
'style',
|
|
68
|
+
'div',
|
|
69
|
+
'span',
|
|
70
|
+
'p',
|
|
71
|
+
'a',
|
|
72
|
+
'img',
|
|
73
|
+
'br',
|
|
74
|
+
'hr',
|
|
75
|
+
'wbr',
|
|
76
|
+
'h1',
|
|
77
|
+
'h2',
|
|
78
|
+
'h3',
|
|
79
|
+
'h4',
|
|
80
|
+
'h5',
|
|
81
|
+
'h6',
|
|
82
|
+
'header',
|
|
83
|
+
'footer',
|
|
84
|
+
'nav',
|
|
85
|
+
'main',
|
|
86
|
+
'aside',
|
|
87
|
+
'section',
|
|
88
|
+
'article',
|
|
89
|
+
'address',
|
|
90
|
+
'figure',
|
|
91
|
+
'figcaption',
|
|
92
|
+
'blockquote',
|
|
93
|
+
'pre',
|
|
94
|
+
'code',
|
|
95
|
+
'kbd',
|
|
96
|
+
'samp',
|
|
97
|
+
'var',
|
|
98
|
+
'mark',
|
|
99
|
+
'time',
|
|
100
|
+
'abbr',
|
|
101
|
+
'cite',
|
|
102
|
+
'q',
|
|
103
|
+
'dfn',
|
|
104
|
+
'small',
|
|
105
|
+
'strong',
|
|
106
|
+
'em',
|
|
107
|
+
'b',
|
|
108
|
+
'i',
|
|
109
|
+
'u',
|
|
110
|
+
's',
|
|
111
|
+
'del',
|
|
112
|
+
'ins',
|
|
113
|
+
'sub',
|
|
114
|
+
'sup',
|
|
115
|
+
'bdi',
|
|
116
|
+
'bdo',
|
|
117
|
+
'ruby',
|
|
118
|
+
'rt',
|
|
119
|
+
'rp',
|
|
120
|
+
'data',
|
|
121
|
+
'ul',
|
|
122
|
+
'ol',
|
|
123
|
+
'li',
|
|
124
|
+
'dl',
|
|
125
|
+
'dt',
|
|
126
|
+
'dd',
|
|
127
|
+
'menu',
|
|
128
|
+
'table',
|
|
129
|
+
'caption',
|
|
130
|
+
'colgroup',
|
|
131
|
+
'col',
|
|
132
|
+
'thead',
|
|
133
|
+
'tbody',
|
|
134
|
+
'tfoot',
|
|
135
|
+
'tr',
|
|
136
|
+
'td',
|
|
137
|
+
'th',
|
|
138
|
+
'form',
|
|
139
|
+
'fieldset',
|
|
140
|
+
'legend',
|
|
141
|
+
'label',
|
|
142
|
+
'button',
|
|
143
|
+
'input',
|
|
144
|
+
'select',
|
|
145
|
+
'option',
|
|
146
|
+
'optgroup',
|
|
147
|
+
'textarea',
|
|
148
|
+
'output',
|
|
149
|
+
'progress',
|
|
150
|
+
'meter',
|
|
151
|
+
'details',
|
|
152
|
+
'summary',
|
|
153
|
+
'picture',
|
|
154
|
+
'center',
|
|
155
|
+
'font',
|
|
156
|
+
])
|
|
157
|
+
|
|
158
|
+
const SVG_ELEMENTS = new Set([
|
|
159
|
+
'svg',
|
|
160
|
+
'g',
|
|
161
|
+
'path',
|
|
162
|
+
'circle',
|
|
163
|
+
'ellipse',
|
|
164
|
+
'line',
|
|
165
|
+
'polyline',
|
|
166
|
+
'polygon',
|
|
167
|
+
'rect',
|
|
168
|
+
'text',
|
|
169
|
+
'tspan',
|
|
170
|
+
'textpath',
|
|
171
|
+
'defs',
|
|
172
|
+
'symbol',
|
|
173
|
+
'use',
|
|
174
|
+
'title',
|
|
175
|
+
'desc',
|
|
176
|
+
'lineargradient',
|
|
177
|
+
'radialgradient',
|
|
178
|
+
'stop',
|
|
179
|
+
'clippath',
|
|
180
|
+
'mask',
|
|
181
|
+
'pattern',
|
|
182
|
+
'marker',
|
|
183
|
+
'filter',
|
|
184
|
+
'fegaussianblur',
|
|
185
|
+
'feoffset',
|
|
186
|
+
'feblend',
|
|
187
|
+
'fecolormatrix',
|
|
188
|
+
'fecomposite',
|
|
189
|
+
'feflood',
|
|
190
|
+
'femerge',
|
|
191
|
+
'femergenode',
|
|
192
|
+
'femorphology',
|
|
193
|
+
'fedropshadow',
|
|
194
|
+
'image',
|
|
195
|
+
])
|
|
196
|
+
|
|
197
|
+
const GLOBAL_ATTRS = new Set([
|
|
198
|
+
'class',
|
|
199
|
+
'id',
|
|
200
|
+
'style',
|
|
201
|
+
'title',
|
|
202
|
+
'lang',
|
|
203
|
+
'dir',
|
|
204
|
+
'role',
|
|
205
|
+
'hidden',
|
|
206
|
+
'tabindex',
|
|
207
|
+
'translate',
|
|
208
|
+
'width',
|
|
209
|
+
'height',
|
|
210
|
+
'align',
|
|
211
|
+
'valign',
|
|
212
|
+
])
|
|
213
|
+
|
|
214
|
+
const ELEMENT_ATTRS = {
|
|
215
|
+
a: ['href'],
|
|
216
|
+
img: ['src', 'alt'],
|
|
217
|
+
meta: ['charset', 'name', 'content'],
|
|
218
|
+
td: ['colspan', 'rowspan', 'headers', 'scope'],
|
|
219
|
+
th: ['colspan', 'rowspan', 'headers', 'scope', 'abbr'],
|
|
220
|
+
col: ['span'],
|
|
221
|
+
colgroup: ['span'],
|
|
222
|
+
ol: ['start', 'reversed', 'type'],
|
|
223
|
+
li: ['value'],
|
|
224
|
+
time: ['datetime'],
|
|
225
|
+
data: ['value'],
|
|
226
|
+
label: ['for'],
|
|
227
|
+
button: ['type', 'value', 'disabled'],
|
|
228
|
+
input: ['type', 'value', 'placeholder', 'checked', 'disabled', 'readonly', 'multiple', 'min', 'max', 'step', 'size', 'maxlength'],
|
|
229
|
+
select: ['multiple', 'disabled', 'size'],
|
|
230
|
+
option: ['value', 'selected', 'disabled', 'label'],
|
|
231
|
+
optgroup: ['label', 'disabled'],
|
|
232
|
+
textarea: ['placeholder', 'rows', 'cols', 'disabled', 'readonly', 'maxlength'],
|
|
233
|
+
form: [],
|
|
234
|
+
fieldset: ['disabled'],
|
|
235
|
+
output: ['for'],
|
|
236
|
+
progress: ['value', 'max'],
|
|
237
|
+
meter: ['value', 'min', 'max', 'low', 'high', 'optimum'],
|
|
238
|
+
details: ['open'],
|
|
239
|
+
font: ['color', 'face', 'size'],
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const SVG_ATTRS = new Set([
|
|
243
|
+
'viewbox',
|
|
244
|
+
'preserveaspectratio',
|
|
245
|
+
'xmlns',
|
|
246
|
+
'version',
|
|
247
|
+
'd',
|
|
248
|
+
'fill',
|
|
249
|
+
'fill-rule',
|
|
250
|
+
'fill-opacity',
|
|
251
|
+
'stroke',
|
|
252
|
+
'stroke-width',
|
|
253
|
+
'stroke-linecap',
|
|
254
|
+
'stroke-linejoin',
|
|
255
|
+
'stroke-dasharray',
|
|
256
|
+
'stroke-dashoffset',
|
|
257
|
+
'stroke-opacity',
|
|
258
|
+
'stroke-miterlimit',
|
|
259
|
+
'clip-rule',
|
|
260
|
+
'clip-path',
|
|
261
|
+
'clippathunits',
|
|
262
|
+
'mask',
|
|
263
|
+
'maskunits',
|
|
264
|
+
'maskcontentunits',
|
|
265
|
+
'opacity',
|
|
266
|
+
'transform',
|
|
267
|
+
'x',
|
|
268
|
+
'y',
|
|
269
|
+
'x1',
|
|
270
|
+
'y1',
|
|
271
|
+
'x2',
|
|
272
|
+
'y2',
|
|
273
|
+
'cx',
|
|
274
|
+
'cy',
|
|
275
|
+
'r',
|
|
276
|
+
'rx',
|
|
277
|
+
'ry',
|
|
278
|
+
'fx',
|
|
279
|
+
'fy',
|
|
280
|
+
'points',
|
|
281
|
+
'offset',
|
|
282
|
+
'stop-color',
|
|
283
|
+
'stop-opacity',
|
|
284
|
+
'gradientunits',
|
|
285
|
+
'gradienttransform',
|
|
286
|
+
'spreadmethod',
|
|
287
|
+
'patternunits',
|
|
288
|
+
'patterncontentunits',
|
|
289
|
+
'patterntransform',
|
|
290
|
+
'markerwidth',
|
|
291
|
+
'markerheight',
|
|
292
|
+
'refx',
|
|
293
|
+
'refy',
|
|
294
|
+
'orient',
|
|
295
|
+
'markerunits',
|
|
296
|
+
'marker-start',
|
|
297
|
+
'marker-mid',
|
|
298
|
+
'marker-end',
|
|
299
|
+
'font-size',
|
|
300
|
+
'font-family',
|
|
301
|
+
'font-weight',
|
|
302
|
+
'font-style',
|
|
303
|
+
'text-anchor',
|
|
304
|
+
'dominant-baseline',
|
|
305
|
+
'alignment-baseline',
|
|
306
|
+
'letter-spacing',
|
|
307
|
+
'dx',
|
|
308
|
+
'dy',
|
|
309
|
+
'textlength',
|
|
310
|
+
'lengthadjust',
|
|
311
|
+
'vector-effect',
|
|
312
|
+
'focusable',
|
|
313
|
+
'aria-hidden',
|
|
314
|
+
'color',
|
|
315
|
+
'display',
|
|
316
|
+
'visibility',
|
|
317
|
+
'overflow',
|
|
318
|
+
'filter',
|
|
319
|
+
'stddeviation',
|
|
320
|
+
'in',
|
|
321
|
+
'in2',
|
|
322
|
+
'result',
|
|
323
|
+
'mode',
|
|
324
|
+
'operator',
|
|
325
|
+
'values',
|
|
326
|
+
'flood-color',
|
|
327
|
+
'flood-opacity',
|
|
328
|
+
'k1',
|
|
329
|
+
'k2',
|
|
330
|
+
'k3',
|
|
331
|
+
'k4',
|
|
332
|
+
'radius',
|
|
333
|
+
'filterunits',
|
|
334
|
+
'primitiveunits',
|
|
335
|
+
'href',
|
|
336
|
+
'shape-rendering',
|
|
337
|
+
'paint-order',
|
|
338
|
+
'mix-blend-mode',
|
|
339
|
+
'isolation',
|
|
340
|
+
])
|
|
341
|
+
|
|
342
|
+
const SAFE_DATA_IMAGE_RE = /^data:image\/(png|jpeg|jpg|gif|webp|avif|svg\+xml|bmp|x-icon|vnd\.microsoft\.icon)[;,]/i
|
|
343
|
+
const SAFE_DATA_FONT_RE =
|
|
344
|
+
/^data:(font\/[a-z0-9.+-]+|application\/(font-woff2?|x-font-[a-z0-9]+|font-sfnt|vnd\.ms-fontobject|octet-stream))[;,]/i
|
|
345
|
+
const LINK_SCHEME_RE = /^(https?:|mailto:|tel:)/i
|
|
346
|
+
const MAX_ATTR_LENGTH = 4000
|
|
347
|
+
// Data URIs are long by nature; everything else is capped above.
|
|
348
|
+
const MAX_DATA_URI_LENGTH = 4 * 1024 * 1024
|
|
349
|
+
|
|
350
|
+
function absoluteUrl(value, base) {
|
|
351
|
+
try {
|
|
352
|
+
return new URL(value, base).toString()
|
|
353
|
+
} catch (_) {
|
|
354
|
+
return null
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// The browser's own whitespace/control trimming on URL attributes, so
|
|
359
|
+
// "java\nscript:" style tricks are judged after normalization.
|
|
360
|
+
function normalizeUrlValue(value) {
|
|
361
|
+
// URL parsers delete tabs and newlines anywhere and trim C0 controls and
|
|
362
|
+
// spaces at both ends; do the same before judging the scheme.
|
|
363
|
+
return (
|
|
364
|
+
String(value || '')
|
|
365
|
+
.replace(/[\t\n\r]/g, '')
|
|
366
|
+
// eslint-disable-next-line no-control-regex
|
|
367
|
+
.replace(/^[\u0000- ]+|[\u0000- ]+$/g, '')
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function isSafeHref(raw, base) {
|
|
372
|
+
const value = normalizeUrlValue(raw)
|
|
373
|
+
if (!value) return null
|
|
374
|
+
if (value.startsWith('#')) return value
|
|
375
|
+
const absolute = absoluteUrl(value, base)
|
|
376
|
+
if (!absolute) return null
|
|
377
|
+
return LINK_SCHEME_RE.test(absolute) ? absolute : null
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// CSS: drop @import and legacy script-in-CSS hooks; route every url() through
|
|
381
|
+
// `resolveUrl(absoluteUrl, kind)` which returns a data: URI or null.
|
|
382
|
+
// CSS escapes can spell any identifier: `\75rl(` is `url(`, `@\69mport` is
|
|
383
|
+
// `@import`. Escapes that stand for letters, `-` or `_` (all those keywords
|
|
384
|
+
// are made of) are decoded in place first, with the same meaning to a CSS
|
|
385
|
+
// parser, so the matching below sees what the browser will see.
|
|
386
|
+
// Everything else stays escaped, because decoding it would change the meaning:
|
|
387
|
+
// an escaped `(`, `:`, `@`, `.`, `/` or digit is part of an identifier, never
|
|
388
|
+
// a function call, declaration, at-rule or number (`url\28 x)` is one ident,
|
|
389
|
+
// not url()), and Tailwind class selectors depend on them staying escaped
|
|
390
|
+
// (`.lg\:translate-x-0`, `.w-1\/2`, `.\32xl\:grid`, `.w-\[min\(85vw\2c 20rem\)\]`).
|
|
391
|
+
// Quotes, backslashes and newlines stay escaped so strings keep their shape.
|
|
392
|
+
const CSS_DECODABLE_RE = /[A-Za-z\-_]/
|
|
393
|
+
function decodeCssEscapes(text) {
|
|
394
|
+
return text.replace(/\\(?:([0-9a-fA-F]{1,6})[ \t\n\r\f]?|([^0-9a-fA-F\n\r\f]))/g, (whole, hex, literal) => {
|
|
395
|
+
const char = hex ? String.fromCodePoint(Math.min(parseInt(hex, 16), 0x10ffff) || 0xfffd) : literal
|
|
396
|
+
return CSS_DECODABLE_RE.test(char) && char.length === 1 ? char : whole
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// image-set() and src() take plain strings as URLs, not only url(): any of
|
|
401
|
+
// them with a string that is not a data: image (or an in-document #ref) goes.
|
|
402
|
+
function stripStringUrlFunctions(text) {
|
|
403
|
+
return text.replace(/(?:-webkit-)?image-set\((?:[^()]|\([^()]*\))*\)|\bsrc\((?:[^()]|\([^()]*\))*\)/gi, (whole) => {
|
|
404
|
+
const strings = whole.match(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g) || []
|
|
405
|
+
const safe = strings.every((quoted) => {
|
|
406
|
+
const value = quoted.slice(1, -1).trim()
|
|
407
|
+
return value.startsWith('#') || SAFE_DATA_IMAGE_RE.test(value)
|
|
408
|
+
})
|
|
409
|
+
return safe ? whole : 'none'
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// The scans below run in linear time on any input (a run of unterminated
|
|
414
|
+
// `url(` or `@font-face{` must not make each one rescan the rest of the
|
|
415
|
+
// text). `nextOf(text, chars)` answers "the next index at or after `start`
|
|
416
|
+
// holding one of `chars`" and remembers its last answer, so callers that ask
|
|
417
|
+
// with increasing starts walk the text once.
|
|
418
|
+
function nextOf(text, chars) {
|
|
419
|
+
let from = Infinity
|
|
420
|
+
let found = -1
|
|
421
|
+
return (start) => {
|
|
422
|
+
if (start >= from && (found === -1 || start <= found)) return found
|
|
423
|
+
from = start
|
|
424
|
+
found = -1
|
|
425
|
+
for (let i = start; i < text.length; i += 1) {
|
|
426
|
+
if (chars.includes(text[i])) {
|
|
427
|
+
found = i
|
|
428
|
+
break
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return found
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function skipSpace(text, index) {
|
|
436
|
+
let i = index
|
|
437
|
+
while (i < text.length && /\s/.test(text[i])) i += 1
|
|
438
|
+
return i
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// [start, end) of every complete `@font-face { ... }` block, in order.
|
|
442
|
+
function fontFaceRanges(text) {
|
|
443
|
+
const ranges = []
|
|
444
|
+
const closeBrace = nextOf(text, '}')
|
|
445
|
+
const re = /@font-face\s*\{/gi
|
|
446
|
+
let match
|
|
447
|
+
while ((match = re.exec(text))) {
|
|
448
|
+
const close = closeBrace(match.index + match[0].length)
|
|
449
|
+
if (close === -1) break
|
|
450
|
+
ranges.push([match.index, close + 1])
|
|
451
|
+
re.lastIndex = close + 1
|
|
452
|
+
}
|
|
453
|
+
return ranges
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Replaces each `url( "x" )`, `url('x')` or `url(x)` (the forms a browser
|
|
457
|
+
// reads as one URL token or a url() with a string) with `replace(dq, sq,
|
|
458
|
+
// bare, offset)`. Unterminated forms stay for the sweep after.
|
|
459
|
+
function replaceUrlTokens(text, replace) {
|
|
460
|
+
const closeQuote = { '"': nextOf(text, '"'), "'": nextOf(text, "'") }
|
|
461
|
+
const bareEnd = nextOf(text, ')\'"')
|
|
462
|
+
const re = /url\(/gi
|
|
463
|
+
let out = ''
|
|
464
|
+
let last = 0
|
|
465
|
+
let match
|
|
466
|
+
while ((match = re.exec(text))) {
|
|
467
|
+
const start = match.index
|
|
468
|
+
const open = skipSpace(text, start + 4)
|
|
469
|
+
const quote = text[open]
|
|
470
|
+
let end = -1
|
|
471
|
+
let parts = null
|
|
472
|
+
if (quote === '"' || quote === "'") {
|
|
473
|
+
const close = closeQuote[quote](open + 1)
|
|
474
|
+
if (close === -1) continue
|
|
475
|
+
const paren = skipSpace(text, close + 1)
|
|
476
|
+
if (text[paren] !== ')') continue
|
|
477
|
+
const value = text.slice(open + 1, close)
|
|
478
|
+
parts = quote === '"' ? [value, undefined, undefined] : [undefined, value, undefined]
|
|
479
|
+
end = paren + 1
|
|
480
|
+
} else {
|
|
481
|
+
const stop = bareEnd(open)
|
|
482
|
+
if (stop === -1 || text[stop] !== ')') continue
|
|
483
|
+
parts = [undefined, undefined, text.slice(open, stop)]
|
|
484
|
+
end = stop + 1
|
|
485
|
+
}
|
|
486
|
+
out += text.slice(last, start) + replace(...parts, start)
|
|
487
|
+
last = end
|
|
488
|
+
re.lastIndex = end
|
|
489
|
+
}
|
|
490
|
+
return out + text.slice(last)
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Any url( left that is not one sanitizeCss wrote: a quoted url("...") goes
|
|
494
|
+
// whole unless it is a kept data: or #ref, anything else goes up to the end
|
|
495
|
+
// of its value (the next `;` or `}`).
|
|
496
|
+
function sweepUrls(text) {
|
|
497
|
+
const valueEnd = nextOf(text, ';}')
|
|
498
|
+
const re = /url\(/gi
|
|
499
|
+
let out = ''
|
|
500
|
+
let last = 0
|
|
501
|
+
let match
|
|
502
|
+
while ((match = re.exec(text))) {
|
|
503
|
+
const start = match.index
|
|
504
|
+
let end = -1
|
|
505
|
+
if (text[start + 4] === '"') {
|
|
506
|
+
// The string, escapes included. Scans never overlap: a string that
|
|
507
|
+
// reaches the next `url("` stops at its quote.
|
|
508
|
+
let i = start + 5
|
|
509
|
+
while (i < text.length && text[i] !== '"') {
|
|
510
|
+
if (text[i] !== '\\') i += 1
|
|
511
|
+
else if (i + 1 < text.length && !/[\n\r\u2028\u2029]/.test(text[i + 1])) i += 2
|
|
512
|
+
else break
|
|
513
|
+
}
|
|
514
|
+
if (text[i] === '"' && text[i + 1] === ')') end = i + 2
|
|
515
|
+
}
|
|
516
|
+
let replacement = 'none'
|
|
517
|
+
if (end !== -1) {
|
|
518
|
+
const whole = text.slice(start, end)
|
|
519
|
+
if (/^url\("(data:|#)/i.test(whole)) replacement = whole
|
|
520
|
+
} else {
|
|
521
|
+
const stop = valueEnd(start + 4)
|
|
522
|
+
end = stop === -1 ? text.length : stop
|
|
523
|
+
}
|
|
524
|
+
out += text.slice(last, start) + replacement
|
|
525
|
+
last = end
|
|
526
|
+
re.lastIndex = Math.max(end, start + 1)
|
|
527
|
+
}
|
|
528
|
+
return out + text.slice(last)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function sanitizeCss(css, { base, resolveUrl = () => null, inFontFace = false } = {}) {
|
|
532
|
+
let text = decodeCssEscapes(String(css || ''))
|
|
533
|
+
text = text.replace(/@import\b[^;{}]*;?/gi, '').replace(/@import/gi, '')
|
|
534
|
+
text = text
|
|
535
|
+
.replace(/expression\s*\(/gi, 'x(')
|
|
536
|
+
.replace(/-moz-binding/gi, 'x-binding')
|
|
537
|
+
.replace(/(^|[^-\w])behavior\s*:/gi, '$1x-behavior:')
|
|
538
|
+
text = text.replace(/javascript\s*:/gi, 'x:').replace(/vbscript\s*:/gi, 'x:')
|
|
539
|
+
// Mark which url()s sit inside @font-face blocks (fonts), the rest are images.
|
|
540
|
+
// url()s come in order, so one pointer walks the font ranges.
|
|
541
|
+
const fontRanges = inFontFace ? [] : fontFaceRanges(text)
|
|
542
|
+
let fontIndex = 0
|
|
543
|
+
const inFont = (offset) => {
|
|
544
|
+
while (fontIndex < fontRanges.length && fontRanges[fontIndex][1] <= offset) fontIndex += 1
|
|
545
|
+
return fontIndex < fontRanges.length && offset >= fontRanges[fontIndex][0]
|
|
546
|
+
}
|
|
547
|
+
text = replaceUrlTokens(text, (dq, sq, bare, offset) => {
|
|
548
|
+
const value = normalizeUrlValue(dq ?? sq ?? bare ?? '')
|
|
549
|
+
const kind = inFontFace || inFont(offset) ? 'font' : 'image'
|
|
550
|
+
if (value.startsWith('#')) return `url("${value.replace(/["\\\n]/g, '')}")`
|
|
551
|
+
if (/^data:/i.test(value)) {
|
|
552
|
+
const ok =
|
|
553
|
+
value.length <= MAX_DATA_URI_LENGTH &&
|
|
554
|
+
(kind === 'font' ? SAFE_DATA_FONT_RE.test(value) || SAFE_DATA_IMAGE_RE.test(value) : SAFE_DATA_IMAGE_RE.test(value))
|
|
555
|
+
return ok ? `url("${value.replace(/["\\\n]/g, (c) => encodeURIComponent(c))}")` : 'none'
|
|
556
|
+
}
|
|
557
|
+
const absolute = absoluteUrl(value, base)
|
|
558
|
+
if (!absolute || !/^https?:/i.test(absolute)) return 'none'
|
|
559
|
+
const inlined = resolveUrl(absolute, kind)
|
|
560
|
+
return inlined &&
|
|
561
|
+
(kind === 'font' ? SAFE_DATA_FONT_RE.test(inlined) || SAFE_DATA_IMAGE_RE.test(inlined) : SAFE_DATA_IMAGE_RE.test(inlined))
|
|
562
|
+
? `url("${inlined.replace(/["\\\n]/g, (c) => encodeURIComponent(c))}")`
|
|
563
|
+
: 'none'
|
|
564
|
+
})
|
|
565
|
+
text = stripStringUrlFunctions(text)
|
|
566
|
+
// Any url( left that is not one we just wrote (an odd or unterminated form
|
|
567
|
+
// the pattern above did not take) is replaced, up to the end of its value.
|
|
568
|
+
text = sweepUrls(text)
|
|
569
|
+
// A literal end tag inside CSS would close the style element.
|
|
570
|
+
return text.replace(/<\/(style)/gi, '<\\/$1').replace(/<!--/g, '<\\!--')
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
export function placeholderSvg(width, height) {
|
|
574
|
+
const w = Math.max(1, Math.min(4000, Math.round(Number(width) || 0))) || 1
|
|
575
|
+
const h = Math.max(1, Math.min(8000, Math.round(Number(height) || 0))) || 1
|
|
576
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}"><rect width="100%" height="100%" fill="#e5e7eb"/></svg>`
|
|
577
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// data-* attributes can carry ids, tokens and state. Kept: data-testid
|
|
581
|
+
// (comment anchors use it) and short keyword values such as
|
|
582
|
+
// data-state="open" or data-side="left", which component libraries style
|
|
583
|
+
// with; nothing that could hold a token (digits, punctuation, length) survives.
|
|
584
|
+
function isKeptDataAttribute(name, value) {
|
|
585
|
+
if (!/^data-[a-z0-9-]{1,40}$/.test(name)) return false
|
|
586
|
+
if (name === 'data-testid') return value.length <= 120
|
|
587
|
+
return /^[a-zA-Z][a-zA-Z-]{0,23}$/.test(value) || value === ''
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function inputType(node) {
|
|
591
|
+
const attr = (node.attrs || []).find((item) => attrName(item) === 'type')
|
|
592
|
+
return String(attr?.value || 'text').trim()
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function attrName(attr) {
|
|
596
|
+
return (attr.prefix ? `${attr.prefix}:${attr.name}` : attr.name).toLowerCase()
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function sanitizeAttributes(node, tag, isSvg, options) {
|
|
600
|
+
const allowedForTag = new Set(ELEMENT_ATTRS[tag] || [])
|
|
601
|
+
const out = []
|
|
602
|
+
let width = null
|
|
603
|
+
let height = null
|
|
604
|
+
for (const attr of node.attrs || []) {
|
|
605
|
+
const name = attrName(attr)
|
|
606
|
+
const value = String(attr.value ?? '')
|
|
607
|
+
if (name === 'data-sw-w') {
|
|
608
|
+
width = value
|
|
609
|
+
continue
|
|
610
|
+
}
|
|
611
|
+
if (name === 'data-sw-h') {
|
|
612
|
+
height = value
|
|
613
|
+
continue
|
|
614
|
+
}
|
|
615
|
+
if (name.startsWith('on')) continue
|
|
616
|
+
// DOM clobbering: no id may shadow a document or form method the board
|
|
617
|
+
// calls (name attributes, which name document properties, are not kept).
|
|
618
|
+
if (name === 'id' && CLOBBER_NAMES.has(value.trim())) continue
|
|
619
|
+
const allowed =
|
|
620
|
+
GLOBAL_ATTRS.has(name) ||
|
|
621
|
+
allowedForTag.has(name) ||
|
|
622
|
+
name.startsWith('aria-') ||
|
|
623
|
+
isKeptDataAttribute(name, value) ||
|
|
624
|
+
(isSvg && (SVG_ATTRS.has(name) || name === 'xlink:href'))
|
|
625
|
+
if (!allowed) continue
|
|
626
|
+
// Secrets that ride in form fields (CSRF tokens, typed passwords) never leave.
|
|
627
|
+
if (tag === 'input' && name === 'value' && /^(password|hidden)$/i.test(inputType(node))) continue
|
|
628
|
+
if (name === 'style') {
|
|
629
|
+
countCss(options, value)
|
|
630
|
+
const css = sanitizeCss(value, { base: options.base, resolveUrl: options.resolveUrl })
|
|
631
|
+
if (css.trim()) out.push({ name: 'style', value: css })
|
|
632
|
+
continue
|
|
633
|
+
}
|
|
634
|
+
if (name === 'href' || name === 'xlink:href') {
|
|
635
|
+
if (isSvg) {
|
|
636
|
+
// SVG references stay inside the document.
|
|
637
|
+
const ref = normalizeUrlValue(value)
|
|
638
|
+
if (ref.startsWith('#') && ref.length < 200)
|
|
639
|
+
out.push({ name: attr.name, prefix: attr.prefix, namespace: attr.namespace, value: ref })
|
|
640
|
+
continue
|
|
641
|
+
}
|
|
642
|
+
let href = isSafeHref(value, options.base)
|
|
643
|
+
// Links to the capture base can be rewritten to a public URL; links to other private hosts go.
|
|
644
|
+
if (href && !href.startsWith('#') && options.rewriteHref) href = options.rewriteHref(href)
|
|
645
|
+
if (href) out.push({ name: 'href', value: href.slice(0, MAX_ATTR_LENGTH) })
|
|
646
|
+
continue
|
|
647
|
+
}
|
|
648
|
+
if (name === 'src') continue // handled by the img branch below
|
|
649
|
+
if (value.length > MAX_ATTR_LENGTH) continue
|
|
650
|
+
out.push({ name: attr.name, prefix: attr.prefix, namespace: attr.namespace, value })
|
|
651
|
+
}
|
|
652
|
+
if (tag === 'img' || (isSvg && tag === 'image')) {
|
|
653
|
+
const raw = (node.attrs || []).find(
|
|
654
|
+
(attr) => attrName(attr) === 'src' || (isSvg && (attrName(attr) === 'href' || attrName(attr) === 'xlink:href')),
|
|
655
|
+
)
|
|
656
|
+
const value = normalizeUrlValue(raw?.value)
|
|
657
|
+
let src = null
|
|
658
|
+
if (/^data:/i.test(value)) src = SAFE_DATA_IMAGE_RE.test(value) && value.length <= MAX_DATA_URI_LENGTH ? value : null
|
|
659
|
+
else if (value) {
|
|
660
|
+
const absolute = absoluteUrl(value, options.base)
|
|
661
|
+
const inlined = absolute && /^https?:/i.test(absolute) ? options.resolveUrl(absolute, 'image') : null
|
|
662
|
+
src = inlined && SAFE_DATA_IMAGE_RE.test(inlined) ? inlined : null
|
|
663
|
+
}
|
|
664
|
+
if (!src) src = placeholderSvg(width, height)
|
|
665
|
+
if (isSvg) {
|
|
666
|
+
const filtered = out.filter((attr) => !['href', 'xlink:href'].includes(attrName(attr)))
|
|
667
|
+
filtered.push({ name: 'href', value: src })
|
|
668
|
+
return filtered
|
|
669
|
+
}
|
|
670
|
+
out.push({ name: 'src', value: src })
|
|
671
|
+
}
|
|
672
|
+
if (tag === 'meta') {
|
|
673
|
+
const names = new Set(out.map((attr) => attr.name.toLowerCase()))
|
|
674
|
+
const nameAttr = out.find((attr) => attr.name.toLowerCase() === 'name')
|
|
675
|
+
if (!names.has('charset') && !(nameAttr && nameAttr.value.toLowerCase() === 'viewport')) return null
|
|
676
|
+
}
|
|
677
|
+
return out
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function sanitizeChildren(parent, options, inSvg) {
|
|
681
|
+
const kept = []
|
|
682
|
+
for (const child of parent.childNodes || []) {
|
|
683
|
+
const result = sanitizeNode(child, options, inSvg)
|
|
684
|
+
if (!result) continue
|
|
685
|
+
if (Array.isArray(result)) kept.push(...result)
|
|
686
|
+
else kept.push(result)
|
|
687
|
+
}
|
|
688
|
+
kept.forEach((child) => {
|
|
689
|
+
child.parentNode = parent
|
|
690
|
+
})
|
|
691
|
+
parent.childNodes = kept
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Returns the node (cleaned), an array of nodes (unwrapped children), or null.
|
|
695
|
+
function sanitizeNode(node, options, inSvg = false) {
|
|
696
|
+
if (node.nodeName === '#text') return node
|
|
697
|
+
if (node.nodeName === '#comment' || node.nodeName === '#documentType') return null
|
|
698
|
+
if (!node.tagName) return null
|
|
699
|
+
const tag = node.tagName.toLowerCase()
|
|
700
|
+
const isSvg = inSvg || tag === 'svg' || node.namespaceURI === 'http://www.w3.org/2000/svg'
|
|
701
|
+
if (DROP_WITH_CONTENT.has(tag)) return null
|
|
702
|
+
// Hidden inputs render nothing and usually hold tokens: drop them.
|
|
703
|
+
if (tag === 'input' && /^hidden$/i.test(inputType(node))) return null
|
|
704
|
+
if (node.namespaceURI && !['http://www.w3.org/1999/xhtml', 'http://www.w3.org/2000/svg'].includes(node.namespaceURI)) return null
|
|
705
|
+
const known = isSvg ? SVG_ELEMENTS.has(tag) : HTML_ELEMENTS.has(tag)
|
|
706
|
+
if (!known) {
|
|
707
|
+
// Unknown and custom elements: keep the content, lose the wrapper.
|
|
708
|
+
sanitizeChildren(node, options, isSvg)
|
|
709
|
+
return node.childNodes
|
|
710
|
+
}
|
|
711
|
+
if (tag === 'style') {
|
|
712
|
+
const css = (node.childNodes || [])
|
|
713
|
+
.filter((child) => child.nodeName === '#text')
|
|
714
|
+
.map((child) => child.value)
|
|
715
|
+
.join('')
|
|
716
|
+
countCss(options, css)
|
|
717
|
+
const clean = sanitizeCss(css, { base: options.base, resolveUrl: options.resolveUrl })
|
|
718
|
+
node.attrs = []
|
|
719
|
+
node.childNodes = clean ? [{ nodeName: '#text', value: clean, parentNode: node }] : []
|
|
720
|
+
return node
|
|
721
|
+
}
|
|
722
|
+
const attrs = sanitizeAttributes(node, tag, isSvg, options)
|
|
723
|
+
if (attrs === null) return null
|
|
724
|
+
node.attrs = attrs
|
|
725
|
+
if (tag === 'form') node.attrs = node.attrs.filter((attr) => attr.name !== 'action' && attr.name !== 'method')
|
|
726
|
+
sanitizeChildren(node, options, isSvg)
|
|
727
|
+
return node
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// `maxCss` caps the CSS text of one document (style elements and style
|
|
731
|
+
// attributes together); over it, sanitizeDocument throws a CssTooLargeError.
|
|
732
|
+
export class CssTooLargeError extends Error {}
|
|
733
|
+
function countCss(options, text) {
|
|
734
|
+
options.cssBytes += text.length
|
|
735
|
+
if (options.cssBytes > options.maxCss) throw new CssTooLargeError('the page CSS is over the cap')
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// Sanitizes a whole document. `base` resolves relative links; `resolveUrl`
|
|
739
|
+
// turns a remote URL into a data: URI (or null to drop it).
|
|
740
|
+
export function sanitizeDocument(html, { base, resolveUrl = () => null, rewriteHref = null, maxCss = Infinity } = {}) {
|
|
741
|
+
const document = parse(String(html || ''))
|
|
742
|
+
const options = { base, resolveUrl, rewriteHref, maxCss, cssBytes: 0 }
|
|
743
|
+
const htmlNode = (document.childNodes || []).find((child) => child.tagName === 'html')
|
|
744
|
+
if (htmlNode) {
|
|
745
|
+
const cleaned = sanitizeNode(htmlNode, options)
|
|
746
|
+
document.childNodes = [
|
|
747
|
+
{ nodeName: '#documentType', name: 'html', publicId: '', systemId: '', parentNode: document },
|
|
748
|
+
...(cleaned && !Array.isArray(cleaned) ? [cleaned] : []),
|
|
749
|
+
]
|
|
750
|
+
}
|
|
751
|
+
return serialize(document)
|
|
752
|
+
}
|