@mouryaamit/nx-polystack 1.24.0 → 1.24.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/README.md +37 -4
- package/USER-MANUAL.md +9 -2
- package/package.json +9 -3
- package/runtime/csp-nonce.mjs +129 -0
- package/runtime/csp-style-attr.mjs +51 -0
package/README.md
CHANGED
|
@@ -228,8 +228,11 @@ import '@mouryaamit/nx-polystack/runtime/csp-style-attr';
|
|
|
228
228
|
It replaces `Element.prototype.setAttribute` and `setAttributeNS` so that only
|
|
229
229
|
`setAttribute('style', v)` (any case) and `setAttributeNS(null, 'style', v)` become
|
|
230
230
|
`el.style.cssText = v`; every other attribute and namespace goes to the original method. It does
|
|
231
|
-
not depend on the nonce.
|
|
232
|
-
|
|
231
|
+
not depend on the nonce. Both runtime files ship twice with the same content: an ES module
|
|
232
|
+
(`.mjs`, the `import` condition that Angular's application builder and webpack follow) and a
|
|
233
|
+
CommonJS file (`.js`, for `require()`). Up to 1.24.0 they were CommonJS only, and Angular 21's builder
|
|
234
|
+
warned that they are not ESM (harmless; `allowedCommonJsDependencies` silenced it); since 1.24.1
|
|
235
|
+
no entry is needed.
|
|
233
236
|
|
|
234
237
|
Conditions of use (any change to the patch needs a new security review):
|
|
235
238
|
|
|
@@ -247,12 +250,42 @@ Conditions of use (any change to the patch needs a new security review):
|
|
|
247
250
|
remove the line.
|
|
248
251
|
|
|
249
252
|
Not covered (still blocked by `style-src-attr`): `style="…"` in markup parsed by the HTML parser,
|
|
250
|
-
i.e. `innerHTML`, `insertAdjacentHTML`, `[innerHTML]` content and static `index.html` markup
|
|
251
|
-
|
|
253
|
+
i.e. `innerHTML`, `insertAdjacentHTML`, `[innerHTML]` content and static `index.html` markup.
|
|
254
|
+
This is by design: the parser creates those attributes without calling `setAttribute`.
|
|
252
255
|
Static `style="…"` written in Angular templates is not parser markup: Angular sets it with
|
|
253
256
|
`setAttribute`, so it is covered. Trusted Types and inline event handlers
|
|
254
257
|
(`setAttribute('onclick', …)`) are unaffected.
|
|
255
258
|
|
|
259
|
+
How much a library still reports depends on how it builds its markup, so check the version you
|
|
260
|
+
use. ngx-extended-pdf-viewer on Angular 15 (Chromium, strict nonce CSP, measured per page load):
|
|
261
|
+
|
|
262
|
+
| ngx-extended-pdf-viewer | Nonce runtime only | With csp-style-attr | What remains |
|
|
263
|
+
| ----------------------------------- | ------------------ | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
264
|
+
| 15.x and older (15.2.2, 3-page PDF) | 60 | 3 | the thumbnail selection rings, about one report per page; the viewer builds them with `innerHTML` |
|
|
265
|
+
| 18.x (18.1.15, an app's pdf route) | 79 | 60 (71 with sidebar, navigation or find) | mostly the toolbar icon SVGs, which the viewer inserts through `[innerHTML]` in its `pdf-shy-button` template; none from thumbnails |
|
|
266
|
+
|
|
267
|
+
In 15.x the toolbar icon sizes are static template styles, so the patch covers them. In 18.x the
|
|
268
|
+
same `style="width…; height…; margin-top…"` arrives as parser markup and stays blocked, so the
|
|
269
|
+
icons lose their sizes (oversized, the rotate icons overlap). Restore them in a global stylesheet
|
|
270
|
+
of the app with one rule per blocked value: each rule matches the icons whose `style` attribute
|
|
271
|
+
holds that value and sets the same value, for example
|
|
272
|
+
|
|
273
|
+
```css
|
|
274
|
+
ngx-extended-pdf-viewer
|
|
275
|
+
:is(pdf-shy-button > .toolbarButton, .secondaryToolbarButton > .icon)
|
|
276
|
+
> svg[style*='width: 24px'] {
|
|
277
|
+
width: 24px;
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
with one such rule for every `width`, `height` and `margin-top` value the viewer version uses (the
|
|
282
|
+
icons range from 16 to 27 px). Do not use one fixed size for all icons: it changes the icons that
|
|
283
|
+
have other sizes, and it also overrides the `width`/`height` attributes of newer viewer versions
|
|
284
|
+
(32.x on Angular 21), which do not need the fix. A rule set like this was checked to be
|
|
285
|
+
pixel-identical to the intended layout on 18.1.15 and to have no effect on 32.x; check it again
|
|
286
|
+
for any other 18.x patch version. The Angular 21 test app did not include the pdf viewer, so
|
|
287
|
+
there are no Angular 21 figures for it.
|
|
288
|
+
|
|
256
289
|
## Contributing and security
|
|
257
290
|
|
|
258
291
|
See [CONTRIBUTING.md](CONTRIBUTING.md) and [SECURITY.md](SECURITY.md). Changes are listed in
|
package/USER-MANUAL.md
CHANGED
|
@@ -962,10 +962,17 @@ blocks them (Angular 15 has no built-in nonce support).
|
|
|
962
962
|
`@mouryaamit/nx-polystack/runtime/csp-style-attr`, imported right after `runtime/csp-nonce`, routes
|
|
963
963
|
`setAttribute('style', v)` and `setAttributeNS(null, 'style', v)` to `el.style.cssText = v`, which
|
|
964
964
|
`style-src` without `'unsafe-inline'` allows. That covers static template and host styles,
|
|
965
|
-
`[attr.style]` and libraries such as NgbModal, NgbTooltip
|
|
966
|
-
|
|
965
|
+
`[attr.style]` and libraries such as NgbModal, NgbTooltip and NgbPopover, on Angular 15–21. Style
|
|
966
|
+
attributes created by the HTML parser stay blocked: ngx-extended-pdf-viewer 18.x inserts its toolbar
|
|
967
|
+
icons that way (about 60 reports per page load; the icons lose their sizes until a global
|
|
968
|
+
stylesheet restores each blocked value with a matching rule, not one fixed size), 15.x its
|
|
969
|
+
thumbnail rings (about one per page); figures and the rule pattern in the README.
|
|
970
|
+
Its conditions of use (no user-controlled data, no CSP reports for these writes,
|
|
967
971
|
what stays blocked, normalised `getAttribute('style')`, import order and rollback) are in the
|
|
968
972
|
README section "Style attributes under a strict CSP". pstack does not add the import; apps opt in.
|
|
973
|
+
Since v1.24.1 both runtime files also ship as ES modules (`.mjs`, the `import` condition; same
|
|
974
|
+
content), so Angular 21 builds need no `allowedCommonJsDependencies` entry for them; `require()`
|
|
975
|
+
keeps the CommonJS `.js`.
|
|
969
976
|
|
|
970
977
|
#### Env encryption settings (v0.25.0)
|
|
971
978
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mouryaamit/nx-polystack",
|
|
3
|
-
"version": "1.24.
|
|
3
|
+
"version": "1.24.2",
|
|
4
4
|
"description": "Nx plugin and pstack CLI that scaffold and build multi-tech monorepos (Angular, Ionic, Flutter) with a hardened release pipeline.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -15,8 +15,14 @@
|
|
|
15
15
|
"types": "./dist/src/migrate-sdk.d.ts",
|
|
16
16
|
"default": "./dist/src/migrate-sdk.js"
|
|
17
17
|
},
|
|
18
|
-
"./runtime/csp-nonce":
|
|
19
|
-
|
|
18
|
+
"./runtime/csp-nonce": {
|
|
19
|
+
"import": "./runtime/csp-nonce.mjs",
|
|
20
|
+
"default": "./runtime/csp-nonce.js"
|
|
21
|
+
},
|
|
22
|
+
"./runtime/csp-style-attr": {
|
|
23
|
+
"import": "./runtime/csp-style-attr.mjs",
|
|
24
|
+
"default": "./runtime/csp-style-attr.js"
|
|
25
|
+
},
|
|
20
26
|
"./runtime/env-secret": {
|
|
21
27
|
"types": "./runtime/env-secret.d.ts",
|
|
22
28
|
"import": "./runtime/env-secret.mjs",
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
/**
|
|
3
|
+
* @mouryaamit/nx-polystack/runtime/csp-nonce
|
|
4
|
+
*
|
|
5
|
+
* Import first in an Angular app's main.ts:
|
|
6
|
+
* import '@mouryaamit/nx-polystack/runtime/csp-nonce';
|
|
7
|
+
*
|
|
8
|
+
* Under a strict Content-Security-Policy (script/style elements need a nonce), Angular 15 and
|
|
9
|
+
* webpack add <style>, <script> and <link rel="stylesheet"> elements at runtime without one, so
|
|
10
|
+
* the browser blocks them. This patch gives every such element the page's nonce when it is
|
|
11
|
+
* inserted: Node appendChild/insertBefore/replaceChild (document, elements, shadow roots) and the
|
|
12
|
+
* append/prepend/before/after/insertAdjacentElement methods.
|
|
13
|
+
*
|
|
14
|
+
* The nonce is read from `window.__cspNonce`, else from <meta property="csp-nonce" content="…">
|
|
15
|
+
* (written by the pstack release build) or <meta name="csp-nonce">. A value the server did not
|
|
16
|
+
* fill in (an SSI directive left as is, or a placeholder such as "random-csp-nonce") counts as no
|
|
17
|
+
* nonce: nothing is patched, and one console warning says the nonce was not filled in.
|
|
18
|
+
*
|
|
19
|
+
* Plain browser JavaScript with no imports, safe to load more than once.
|
|
20
|
+
*/
|
|
21
|
+
(function pstackCspNonce() {
|
|
22
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
|
23
|
+
if (window.__pstackCspNoncePatched) return;
|
|
24
|
+
|
|
25
|
+
var PLACEHOLDER = /^(random-csp-nonce|nonce|csp-nonce|\{\{.*\}\}|\$\{.*\}|%.*%)$/i;
|
|
26
|
+
|
|
27
|
+
function fromMeta() {
|
|
28
|
+
if (typeof document.querySelector !== 'function') return undefined;
|
|
29
|
+
var meta =
|
|
30
|
+
document.querySelector('meta[property="csp-nonce"]') ||
|
|
31
|
+
document.querySelector('meta[name="csp-nonce"]') ||
|
|
32
|
+
document.querySelector('meta[name="CSP-NONCE"]');
|
|
33
|
+
return meta && typeof meta.getAttribute === 'function'
|
|
34
|
+
? meta.getAttribute('content')
|
|
35
|
+
: undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Anything that looks like a filled-in nonce: no markup, quotes or spaces, not a placeholder. */
|
|
39
|
+
function valid(value) {
|
|
40
|
+
return (
|
|
41
|
+
typeof value === 'string' &&
|
|
42
|
+
value.length >= 8 &&
|
|
43
|
+
!/[\s<>"'`]/.test(value) &&
|
|
44
|
+
!PLACEHOLDER.test(value)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var raw = window.__cspNonce;
|
|
49
|
+
var nonce = valid(raw) ? raw : undefined;
|
|
50
|
+
var meta;
|
|
51
|
+
if (!nonce) {
|
|
52
|
+
meta = fromMeta();
|
|
53
|
+
nonce = valid(meta) ? meta : undefined;
|
|
54
|
+
}
|
|
55
|
+
if (!nonce) {
|
|
56
|
+
// A value is there but was not filled in (the server did not replace it): say so once.
|
|
57
|
+
// No value at all (e.g. ng serve without a CSP) stays silent; '' is what this runtime writes.
|
|
58
|
+
var unfilled =
|
|
59
|
+
typeof raw === 'string' && raw !== '' ? raw : typeof meta === 'string' ? meta : undefined;
|
|
60
|
+
if (
|
|
61
|
+
unfilled !== undefined &&
|
|
62
|
+
!window.__pstackCspNonceWarned &&
|
|
63
|
+
typeof console !== 'undefined'
|
|
64
|
+
) {
|
|
65
|
+
window.__pstackCspNonceWarned = true;
|
|
66
|
+
console.warn(
|
|
67
|
+
'[pstack csp-nonce] The CSP nonce was not filled in (found "' +
|
|
68
|
+
unfilled.slice(0, 40) +
|
|
69
|
+
'"), so no nonce is added: under a nonce-based CSP, runtime <style>, <script> and ' +
|
|
70
|
+
'<link rel="stylesheet"> elements will be blocked. The server must replace the ' +
|
|
71
|
+
'placeholder in index.html on every response (e.g. Apache SSI: Options +IncludesNOEXEC ' +
|
|
72
|
+
'and the INCLUDES output filter for index.html).',
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
window.__cspNonce = '';
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
window.__cspNonce = nonce;
|
|
79
|
+
window.__pstackCspNoncePatched = true;
|
|
80
|
+
|
|
81
|
+
function needsNonce(node) {
|
|
82
|
+
if (!node || node.nodeType !== 1 || typeof node.getAttribute !== 'function') return false;
|
|
83
|
+
if (node.getAttribute('nonce')) return false;
|
|
84
|
+
var name = String(node.localName || node.nodeName || '').toLowerCase();
|
|
85
|
+
if (name === 'script' || name === 'style') return true;
|
|
86
|
+
return name === 'link' && /(^|\s)stylesheet(\s|$)/i.test(node.getAttribute('rel') || '');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function stamp(node, depth) {
|
|
90
|
+
if (needsNonce(node)) node.setAttribute('nonce', nonce);
|
|
91
|
+
// A fragment carries its descendants in (walked a few levels deep; elements alone are not
|
|
92
|
+
// walked, so moving a large subtree stays cheap).
|
|
93
|
+
var walk = depth === 0 ? node && node.nodeType === 11 : depth < 8;
|
|
94
|
+
if (walk && node.childNodes) {
|
|
95
|
+
for (var i = 0; i < node.childNodes.length; i++) stamp(node.childNodes[i], depth + 1);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function wrap(proto, method, nodeArgs) {
|
|
100
|
+
if (!proto || typeof proto[method] !== 'function') return;
|
|
101
|
+
var original = proto[method];
|
|
102
|
+
proto[method] = function () {
|
|
103
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
104
|
+
if (nodeArgs === 'all' || nodeArgs.indexOf(i) !== -1) stamp(arguments[i], 0);
|
|
105
|
+
}
|
|
106
|
+
return original.apply(this, arguments);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var nodeProto = typeof Node !== 'undefined' ? Node.prototype : undefined;
|
|
111
|
+
wrap(nodeProto, 'appendChild', [0]);
|
|
112
|
+
wrap(nodeProto, 'insertBefore', [0]);
|
|
113
|
+
wrap(nodeProto, 'replaceChild', [0]);
|
|
114
|
+
|
|
115
|
+
var parents = [];
|
|
116
|
+
if (typeof Element !== 'undefined') parents.push(Element.prototype);
|
|
117
|
+
if (typeof DocumentFragment !== 'undefined') parents.push(DocumentFragment.prototype);
|
|
118
|
+
if (typeof Document !== 'undefined') parents.push(Document.prototype);
|
|
119
|
+
for (var p = 0; p < parents.length; p++) {
|
|
120
|
+
wrap(parents[p], 'append', 'all');
|
|
121
|
+
wrap(parents[p], 'prepend', 'all');
|
|
122
|
+
}
|
|
123
|
+
if (typeof Element !== 'undefined') {
|
|
124
|
+
wrap(Element.prototype, 'before', 'all');
|
|
125
|
+
wrap(Element.prototype, 'after', 'all');
|
|
126
|
+
wrap(Element.prototype, 'replaceWith', 'all');
|
|
127
|
+
wrap(Element.prototype, 'insertAdjacentElement', [1]);
|
|
128
|
+
}
|
|
129
|
+
})();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
/**
|
|
3
|
+
* @mouryaamit/nx-polystack/runtime/csp-style-attr (opt-in)
|
|
4
|
+
*
|
|
5
|
+
* Import right after runtime/csp-nonce in main.ts:
|
|
6
|
+
* import '@mouryaamit/nx-polystack/runtime/csp-nonce';
|
|
7
|
+
* import '@mouryaamit/nx-polystack/runtime/csp-style-attr';
|
|
8
|
+
*
|
|
9
|
+
* Under style-src without 'unsafe-inline', element.setAttribute('style', v) is blocked
|
|
10
|
+
* (style-src-attr), but the same declarations written through the CSSOM (style.cssText = v) are
|
|
11
|
+
* allowed. Angular writes static template / host style attributes with setAttribute('style'), so
|
|
12
|
+
* this routes setAttribute('style', v) and setAttributeNS(null, 'style', v) to style.cssText.
|
|
13
|
+
* Nothing else changes: other attributes, setAttributeNode, attributes.setNamedItem, Attr.value
|
|
14
|
+
* and style attributes parsed by the HTML parser (innerHTML, insertAdjacentHTML, static
|
|
15
|
+
* index.html markup) stay blocked. Static style="" in Angular templates is covered: Angular
|
|
16
|
+
* writes it with setAttribute.
|
|
17
|
+
* getAttribute('style') then returns the browser's normalised text, without declarations it does
|
|
18
|
+
* not support. Never pass user-controlled data to these writes. Rollback: remove the import.
|
|
19
|
+
*
|
|
20
|
+
* Plain browser JavaScript with no imports, safe to load more than once.
|
|
21
|
+
*/
|
|
22
|
+
(function pstackCspStyleAttr() {
|
|
23
|
+
if (typeof window === 'undefined' || typeof Element === 'undefined') return;
|
|
24
|
+
if (window.__pstackCspStyleAttrPatched) return;
|
|
25
|
+
window.__pstackCspStyleAttrPatched = true;
|
|
26
|
+
|
|
27
|
+
var proto = Element.prototype;
|
|
28
|
+
var setAttribute = proto.setAttribute;
|
|
29
|
+
var setAttributeNS = proto.setAttributeNS;
|
|
30
|
+
|
|
31
|
+
/** The element's CSSOM declaration when name is the style attribute, else undefined. */
|
|
32
|
+
function styleOf(el, name) {
|
|
33
|
+
if (typeof name !== 'string' || name.toLowerCase() !== 'style') return undefined;
|
|
34
|
+
var style = el.style;
|
|
35
|
+
return style && typeof style === 'object' && 'cssText' in style ? style : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
proto.setAttribute = function (name, value) {
|
|
39
|
+
var style = arguments.length >= 2 ? styleOf(this, name) : undefined;
|
|
40
|
+
if (!style) return setAttribute.apply(this, arguments);
|
|
41
|
+
style.cssText = String(value);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
if (typeof setAttributeNS === 'function') {
|
|
45
|
+
proto.setAttributeNS = function (ns, name, value) {
|
|
46
|
+
var style = arguments.length >= 3 && !ns ? styleOf(this, name) : undefined;
|
|
47
|
+
if (!style) return setAttributeNS.apply(this, arguments);
|
|
48
|
+
style.cssText = String(value);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
})();
|