@ruckc/truss-code 1.50.0 → 1.51.0
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.md +1 -1
- package/dist/monaco-csp.d.ts +14 -6
- package/dist/monaco-csp.js +68 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ npm install monaco-editor # only if you use the editors
|
|
|
29
29
|
|
|
30
30
|
## Strict CSP
|
|
31
31
|
|
|
32
|
-
Under `style-src 'self' 'nonce-…'` (no `'unsafe-inline'`) the editors work once a nonce is configured: `setCspNonce(nonce)` from `@ruckc/truss-ui` in the root layout, `configureCsp({ nonce })`, or `<meta property="csp-nonce" nonce="…">`. Monaco 0.57 has no nonce support of its own, so with a nonce present the package wraps
|
|
32
|
+
Under `style-src 'self' 'nonce-…'` (no `'unsafe-inline'`) the editors work once a nonce is configured: `setCspNonce(nonce)` from `@ruckc/truss-ui` in the root layout, `configureCsp({ nonce })`, or `<meta property="csp-nonce" nonce="…">`. Monaco 0.57 has no nonce support of its own, so with a nonce present the package wraps four page-wide functions, and applies the conversion only to Monaco's own DOM (an editor root, `.monaco-editor` / `.monaco-diff-editor`, or the `.context-view` Monaco appends to `body`): `setAttribute('style', …)` becomes a CSSOM assignment, and `innerHTML` / `insertAdjacentHTML` strings that contain `style="…"` are parsed in a policy-free document with each style attribute moved to the CSSOM. Monaco also fills a detached scratch element before moving the nodes in; that is converted only for Monaco's own line markup (a `<div style="top:…` start and layout-only declarations such as `top`, `left`, `width`, `height`, `line-height`). Every other call, such as injected `<b style="…">` markup elsewhere on the page, takes the browser's native path and is blocked by the policy as usual. `document.createElement('style')` gives the element the nonce and is not scoped: it cannot be told apart from page code's, so the residual risk is limited to script that is already running (which `script-src` governs). SvelteKit's own `#svelte-announcer` template is converted too, as an exact-string exception. Without a nonce nothing is wrapped. Monaco's own CSS is a normal stylesheet in a production build; in Vite dev, Vite reads its nonce from the same meta tag (or `html.cspNonce`). The Truss documentation has a Content Security Policy page with the details.
|
|
33
33
|
|
|
34
34
|
## License
|
|
35
35
|
|
package/dist/monaco-csp.d.ts
CHANGED
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
* elements with `document.createElement('style')`, sets `style` attributes with `setAttribute('style', …)`, and renders every text line
|
|
4
4
|
* through `innerHTML` / `insertAdjacentHTML` strings that carry `style="top:…"`. All three are blocked by such a policy.
|
|
5
5
|
*
|
|
6
|
-
* `installMonacoCsp(nonce)` wraps those entry points (only once a nonce is known; without one nothing is touched)
|
|
7
|
-
* -
|
|
8
|
-
*
|
|
9
|
-
* - `
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* `installMonacoCsp(nonce)` wraps those entry points (only once a nonce is known; without one nothing is touched). The wrappers are
|
|
7
|
+
* page-wide functions, so every conversion is limited to Monaco's own DOM; anything else takes the browser's native path, where the
|
|
8
|
+
* policy blocks `style` attributes in injected markup as usual:
|
|
9
|
+
* - `Element.setAttribute('style', v)` becomes `el.style.cssText = v` (CSSOM, which CSP allows) only inside a Monaco root (`isMonacoDom`);
|
|
10
|
+
* - `innerHTML = …` and `insertAdjacentHTML(…)` strings holding `style="…"` are parsed in a policy-free document (`DOMParser`), each
|
|
11
|
+
* style attribute is moved to the CSSOM, and the finished nodes are inserted, only when the target is inside a Monaco root.
|
|
12
|
+
* Monaco also renders lines into a detached scratch `<div>` and moves the result in; that target is in no root yet, so a detached
|
|
13
|
+
* target (a `<template>` included, its nodes go to `content`) is converted only for Monaco's own line markup: the string starts with
|
|
14
|
+
* `<div style="top:` / `width:` / `text-indent:` and every declaration is a layout property with a plain value (`SAFE_DECLARATION`);
|
|
15
|
+
* One exact string that is not Monaco's is also converted: SvelteKit's `#svelte-announcer` template (`KIT_ANNOUNCER`).
|
|
16
|
+
* - `document.createElement('style')` gives the element the nonce before Monaco fills or inserts it. This one is not scoped: the
|
|
17
|
+
* element cannot be told apart from page code's when it is created (see the "residual risk" note in the truss-code README).
|
|
12
18
|
*/
|
|
19
|
+
/** Whether `node` is part of Monaco's own DOM: inside an editor root or the context view. */
|
|
20
|
+
export declare function isMonacoDom(node: Node | null): boolean;
|
|
13
21
|
/** Wraps Monaco's style creation so that it works under a nonce-only `style-src`. Idempotent; a later call replaces the nonce. Returns `uninstallMonacoCsp`. */
|
|
14
22
|
export declare function installMonacoCsp(nonce: string | undefined): () => void;
|
|
15
23
|
/** Restores the browser's own functions. Meant for tests; Monaco is global, so the editors leave the wrappers installed. */
|
package/dist/monaco-csp.js
CHANGED
|
@@ -3,22 +3,77 @@
|
|
|
3
3
|
* elements with `document.createElement('style')`, sets `style` attributes with `setAttribute('style', …)`, and renders every text line
|
|
4
4
|
* through `innerHTML` / `insertAdjacentHTML` strings that carry `style="top:…"`. All three are blocked by such a policy.
|
|
5
5
|
*
|
|
6
|
-
* `installMonacoCsp(nonce)` wraps those entry points (only once a nonce is known; without one nothing is touched)
|
|
7
|
-
* -
|
|
8
|
-
*
|
|
9
|
-
* - `
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* `installMonacoCsp(nonce)` wraps those entry points (only once a nonce is known; without one nothing is touched). The wrappers are
|
|
7
|
+
* page-wide functions, so every conversion is limited to Monaco's own DOM; anything else takes the browser's native path, where the
|
|
8
|
+
* policy blocks `style` attributes in injected markup as usual:
|
|
9
|
+
* - `Element.setAttribute('style', v)` becomes `el.style.cssText = v` (CSSOM, which CSP allows) only inside a Monaco root (`isMonacoDom`);
|
|
10
|
+
* - `innerHTML = …` and `insertAdjacentHTML(…)` strings holding `style="…"` are parsed in a policy-free document (`DOMParser`), each
|
|
11
|
+
* style attribute is moved to the CSSOM, and the finished nodes are inserted, only when the target is inside a Monaco root.
|
|
12
|
+
* Monaco also renders lines into a detached scratch `<div>` and moves the result in; that target is in no root yet, so a detached
|
|
13
|
+
* target (a `<template>` included, its nodes go to `content`) is converted only for Monaco's own line markup: the string starts with
|
|
14
|
+
* `<div style="top:` / `width:` / `text-indent:` and every declaration is a layout property with a plain value (`SAFE_DECLARATION`);
|
|
15
|
+
* One exact string that is not Monaco's is also converted: SvelteKit's `#svelte-announcer` template (`KIT_ANNOUNCER`).
|
|
16
|
+
* - `document.createElement('style')` gives the element the nonce before Monaco fills or inserts it. This one is not scoped: the
|
|
17
|
+
* element cannot be told apart from page code's when it is created (see the "residual risk" note in the truss-code README).
|
|
12
18
|
*/
|
|
13
19
|
import { cspNonce } from './csp.js';
|
|
14
20
|
let current;
|
|
15
21
|
const HAS_STYLE = /\sstyle\s*=/i;
|
|
22
|
+
/** What Monaco builds: its editor roots, plus the context view it appends to `body`. */
|
|
23
|
+
const MONACO_ROOT = '.monaco-editor, .monaco-diff-editor, .context-view';
|
|
24
|
+
/** Where Monaco's own line markup starts (viewLine, viewOverlays, the word-wrap measuring container). */
|
|
25
|
+
const MONACO_MARKUP = /^\s*<div style="(?:top|width|text-indent):/;
|
|
26
|
+
/** The declarations Monaco writes into those strings, with the values it writes: positions and sizes, never colours, `position` or `display: none`. */
|
|
27
|
+
const SAFE_DECLARATION = {
|
|
28
|
+
top: /^-?[\d.]+(px)?$/, left: /^-?[\d.]+(px|%)?$/, width: /^[\d.]+(px|%)?$/, height: /^[\d.]+(px)?$/, 'line-height': /^[\d.]+(px)?$/,
|
|
29
|
+
'text-indent': /^-?[\d.]+(px)?$/, display: /^inline-block$/, 'box-sizing': /^border-box$/, 'white-space': /^(nowrap|pre)$/, 'unicode-bidi': /^isolate$/
|
|
30
|
+
};
|
|
31
|
+
/** The page-free document a style-bearing string is parsed in (no browsing context, so no CSP applies while parsing). */
|
|
32
|
+
const parse = (html) => new DOMParser().parseFromString(`<!doctype html><body>${html}`, 'text/html').body;
|
|
33
|
+
/**
|
|
34
|
+
* Whether every element the parser produced with a `style` attribute holds only declarations Monaco writes itself. This checks the parsed
|
|
35
|
+
* DOM, not the string: the HTML parser accepts forms a string scan misses (`<b/style="…">`, entity-encoded values), so only what the
|
|
36
|
+
* browser would actually apply is judged.
|
|
37
|
+
*/
|
|
38
|
+
function onlyMonacoDeclarations(body) {
|
|
39
|
+
for (const el of body.querySelectorAll('[style]')) {
|
|
40
|
+
for (const d of el.getAttribute('style').split(';')) {
|
|
41
|
+
if (!d.trim())
|
|
42
|
+
continue;
|
|
43
|
+
const i = d.indexOf(':');
|
|
44
|
+
if (!(i > 0 && SAFE_DECLARATION[d.slice(0, i).trim().toLowerCase()]?.test(d.slice(i + 1).trim())))
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/** Whether `node` is part of Monaco's own DOM: inside an editor root or the context view. */
|
|
51
|
+
export function isMonacoDom(node) {
|
|
52
|
+
const el = node instanceof Element ? node : node?.parentElement;
|
|
53
|
+
return !!el?.closest(MONACO_ROOT);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* SvelteKit's own screen-reader announcer, `<div id="svelte-announcer" … style="…">`, is a static template with an inline style (a visually hidden
|
|
57
|
+
* element). Before the shim was scoped it was converted along with everything else, so keep it working: this exact string, and no other style attribute.
|
|
58
|
+
*/
|
|
59
|
+
const KIT_ANNOUNCER = '<div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0; top: 0; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px">';
|
|
60
|
+
// exactly the announcer: the only element the parser gives a style attribute is the announcer itself
|
|
61
|
+
const isKitAnnouncer = (html, body) => html.startsWith(KIT_ANNOUNCER) && body.querySelectorAll('[style]').length === 1;
|
|
62
|
+
/** Whether a style string may be converted for this target: Monaco's DOM, or a detached scratch element given Monaco's own markup. */
|
|
63
|
+
function mayConvert(target, html) {
|
|
64
|
+
if (isMonacoDom(target))
|
|
65
|
+
return true;
|
|
66
|
+
if (!target || target.isConnected)
|
|
67
|
+
return false;
|
|
68
|
+
const body = parse(html);
|
|
69
|
+
return (MONACO_MARKUP.test(html) && onlyMonacoDeclarations(body)) || isKitAnnouncer(html, body);
|
|
70
|
+
}
|
|
16
71
|
/** Parses `html` in a policy-free document and replaces every `style="…"` attribute with the equivalent CSSOM declarations. */
|
|
17
72
|
function toFragment(html) {
|
|
18
73
|
// DOMParser documents have no browsing context, so no CSP applies to the attributes while parsing (a <template> in the page is not exempt)
|
|
19
|
-
const body =
|
|
74
|
+
const body = parse(html);
|
|
20
75
|
for (const el of body.querySelectorAll('[style]')) {
|
|
21
|
-
const css = el.getAttribute('style')
|
|
76
|
+
const css = el.getAttribute('style');
|
|
22
77
|
el.removeAttribute('style');
|
|
23
78
|
el.style.cssText = css;
|
|
24
79
|
}
|
|
@@ -49,7 +104,7 @@ export function installMonacoCsp(nonce) {
|
|
|
49
104
|
return el;
|
|
50
105
|
};
|
|
51
106
|
Element.prototype.setAttribute = function (name, value) {
|
|
52
|
-
if (String(name).toLowerCase() === 'style' && 'style' in this)
|
|
107
|
+
if (String(name).toLowerCase() === 'style' && 'style' in this && isMonacoDom(this))
|
|
53
108
|
this.style.cssText = String(value);
|
|
54
109
|
else
|
|
55
110
|
undo.setAttribute.call(this, name, value);
|
|
@@ -58,7 +113,7 @@ export function installMonacoCsp(nonce) {
|
|
|
58
113
|
...undo.innerHTML,
|
|
59
114
|
set(v) {
|
|
60
115
|
const html = String(v);
|
|
61
|
-
if (!HAS_STYLE.test(html))
|
|
116
|
+
if (!HAS_STYLE.test(html) || !mayConvert(this, html))
|
|
62
117
|
rawSet.call(this, v);
|
|
63
118
|
else
|
|
64
119
|
(this instanceof HTMLTemplateElement ? this.content : this).replaceChildren(toFragment(html));
|
|
@@ -66,7 +121,9 @@ export function installMonacoCsp(nonce) {
|
|
|
66
121
|
});
|
|
67
122
|
Element.prototype.insertAdjacentHTML = function (where, v) {
|
|
68
123
|
const html = String(v);
|
|
69
|
-
|
|
124
|
+
// beforebegin / afterend put the markup next to the target, so it is the parent that has to be Monaco's
|
|
125
|
+
const beside = /^(beforebegin|afterend)$/i.test(String(where));
|
|
126
|
+
if (!HAS_STYLE.test(html) || !mayConvert(beside ? this.parentElement : this, html))
|
|
70
127
|
return undo.insertAdjacentHTML.call(this, where, v);
|
|
71
128
|
const frag = toFragment(html);
|
|
72
129
|
switch (String(where).toLowerCase()) {
|
package/package.json
CHANGED