@ruckc/truss-code 1.54.1 → 1.55.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/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 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.
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`, `bottom`, `left`, `width`, `height`, `line-height`). `position` is accepted in one parsed shape only: Monaco's whitespace `<svg>` (from `renderWhitespace`, by default inside a selection) as the direct child of a top-level `<div style="top:…">` overlay line, holding only `<path>` elements, with exactly `bottom: 0`, `position: absolute`, `width` and `height`. Residual risk of that shape: markup that reaches a detached sink can place an absolutely positioned SVG box under 100px tall (one editor line; any width), filled with any paths in any colour (SVG presentation attributes such as `fill` are not governed by `style-src`), at the bottom edge of its nearest positioned ancestor, so it can cover nearby content and swallow clicks on it (UI redress). It cannot be moved by `top`/`left`/a non-zero `bottom`, cannot be `fixed`, and cannot hold `<text>`, links or `foreignObject` (paths can still draw letter shapes, but nothing clickable or selectable); no other element gets `position`. 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
 
@@ -11,7 +11,15 @@
11
11
  * style attribute is moved to the CSSOM, and the finished nodes are inserted, only when the target is inside a Monaco root.
12
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
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`);
14
+ * `<div style="top:` / `width:` / `text-indent:` and every declaration is a layout property with a plain value (`SAFE_DECLARATION`).
15
+ * `position` is never on that list, with one exception judged on the parsed DOM (#296): the whitespace `<svg>` Monaco draws in a
16
+ * view-overlay line (`renderWhitespace`, by default inside a selection) may carry exactly `bottom:0`, `position:absolute`, `width` and
17
+ * `height`, when its parent is a top-level `<div style="top:…">` line and it holds nothing but `<path>` elements (`OVERLAY_SVG`).
18
+ * Residual risk of that exception: markup in a detached sink can put an absolutely positioned box of SVG paths under 100px tall (any width)
19
+ * (filled with any colour through the `fill` presentation attribute, which `style-src` does not govern) at the bottom edge of the
20
+ * nearest positioned ancestor, so it can cover other content and swallow clicks on it (UI redress); paths can draw letter shapes, so a
21
+ * painted decoy is possible, but it cannot hold `<text>`, links, `foreignObject` or any other element, cannot be moved by
22
+ * `top`/`left`/a non-zero `bottom`, cannot be `fixed`, and nothing else gets `position`.
15
23
  * One exact string that is not Monaco's is also converted: SvelteKit's `#svelte-announcer` template (`KIT_ANNOUNCER`).
16
24
  * - `document.createElement('style')` gives the element the nonce before Monaco fills or inserts it. This one is not scoped: the
17
25
  * element cannot be told apart from page code's when it is created (see the "residual risk" note in the truss-code README).
@@ -11,7 +11,15 @@
11
11
  * style attribute is moved to the CSSOM, and the finished nodes are inserted, only when the target is inside a Monaco root.
12
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
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`);
14
+ * `<div style="top:` / `width:` / `text-indent:` and every declaration is a layout property with a plain value (`SAFE_DECLARATION`).
15
+ * `position` is never on that list, with one exception judged on the parsed DOM (#296): the whitespace `<svg>` Monaco draws in a
16
+ * view-overlay line (`renderWhitespace`, by default inside a selection) may carry exactly `bottom:0`, `position:absolute`, `width` and
17
+ * `height`, when its parent is a top-level `<div style="top:…">` line and it holds nothing but `<path>` elements (`OVERLAY_SVG`).
18
+ * Residual risk of that exception: markup in a detached sink can put an absolutely positioned box of SVG paths under 100px tall (any width)
19
+ * (filled with any colour through the `fill` presentation attribute, which `style-src` does not govern) at the bottom edge of the
20
+ * nearest positioned ancestor, so it can cover other content and swallow clicks on it (UI redress); paths can draw letter shapes, so a
21
+ * painted decoy is possible, but it cannot hold `<text>`, links, `foreignObject` or any other element, cannot be moved by
22
+ * `top`/`left`/a non-zero `bottom`, cannot be `fixed`, and nothing else gets `position`.
15
23
  * One exact string that is not Monaco's is also converted: SvelteKit's `#svelte-announcer` template (`KIT_ANNOUNCER`).
16
24
  * - `document.createElement('style')` gives the element the nonce before Monaco fills or inserts it. This one is not scoped: the
17
25
  * element cannot be told apart from page code's when it is created (see the "residual risk" note in the truss-code README).
@@ -23,10 +31,30 @@ const HAS_STYLE = /\sstyle\s*=/i;
23
31
  const MONACO_ROOT = '.monaco-editor, .monaco-diff-editor, .context-view';
24
32
  /** Where Monaco's own line markup starts (viewLine, viewOverlays, the word-wrap measuring container). */
25
33
  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)?$/,
34
+ /**
35
+ * The declarations Monaco writes into those strings, with the values it writes: positions and sizes, never colours, `position` or `display: none`.
36
+ * A `Map`, so a property named `constructor` or `__proto__` finds nothing instead of an `Object.prototype` member.
37
+ */
38
+ const SAFE_DECLARATION = new Map(Object.entries({
39
+ // `bottom`: the selection pieces, `<div class="cslr …" style="top:…;bottom:…;left:…;width:…">` (#296)
40
+ top: /^-?[\d.]+(px)?$/, bottom: /^-?[\d.]+(px)?$/, left: /^-?[\d.]+(px|%)?$/, width: /^[\d.]+(px|%)?$/, height: /^[\d.]+(px)?$/, 'line-height': /^[\d.]+(px)?$/,
29
41
  'text-indent': /^-?[\d.]+(px)?$/, display: /^inline-block$/, 'box-sizing': /^border-box$/, 'white-space': /^(nowrap|pre)$/, 'unicode-bidi': /^isolate$/
42
+ }));
43
+ /**
44
+ * The only declarations allowed on Monaco's whitespace `<svg>` (`<svg style="bottom:0;position:absolute;width:…;height:…" … fill="…">`, #296), and only
45
+ * in the shape `isOverlaySvg` checks. In place of the general list, so it cannot also take `top`, `left` or a non-zero `bottom`.
46
+ * Its height is one editor line, so it is capped below 100px: an svg in this shape can only ever cover a line-height strip at the bottom
47
+ * of its positioned ancestor, never the page (its width follows the line's whitespace, so it stays the general width pattern).
48
+ */
49
+ const OVERLAY_SVG = new Map([['bottom', /^0(px)?$/], ['position', /^absolute$/], ['width', SAFE_DECLARATION.get('width')], ['height', /^\d{1,2}(\.\d+)?(px)?$/]]);
50
+ /**
51
+ * Whether `el` is Monaco's whitespace `<svg>` in its exact place: the child of a view-overlay line (a top-level `<div style="top:…">`)
52
+ * holding only `<path>` elements. Judged on the parsed nodes.
53
+ */
54
+ const isOverlaySvg = (el, body) => {
55
+ const line = el.parentElement;
56
+ return el instanceof SVGSVGElement && line instanceof HTMLDivElement && line.parentElement === body && /^top:/.test(line.getAttribute('style') ?? '') &&
57
+ [...el.querySelectorAll('*')].every((c) => c instanceof SVGPathElement);
30
58
  };
31
59
  /** The page-free document a style-bearing string is parsed in (no browsing context, so no CSP applies while parsing). */
32
60
  const parse = (html) => new DOMParser().parseFromString(`<!doctype html><body>${html}`, 'text/html').body;
@@ -37,11 +65,12 @@ const parse = (html) => new DOMParser().parseFromString(`<!doctype html><body>${
37
65
  */
38
66
  function onlyMonacoDeclarations(body) {
39
67
  for (const el of body.querySelectorAll('[style]')) {
68
+ const allowed = isOverlaySvg(el, body) ? OVERLAY_SVG : SAFE_DECLARATION;
40
69
  for (const d of el.getAttribute('style').split(';')) {
41
70
  if (!d.trim())
42
71
  continue;
43
72
  const i = d.indexOf(':');
44
- if (!(i > 0 && SAFE_DECLARATION[d.slice(0, i).trim().toLowerCase()]?.test(d.slice(i + 1).trim())))
73
+ if (!(i > 0 && allowed.get(d.slice(0, i).trim().toLowerCase())?.test(d.slice(i + 1).trim())))
45
74
  return false;
46
75
  }
47
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-code",
3
- "version": "1.54.1",
3
+ "version": "1.55.1",
4
4
  "description": "Read-only code blocks, diff and merge views, plus an optional Monaco code editor, styled with Truss UI",
5
5
  "type": "module",
6
6
  "license": "MIT",