astro-helmet 1.4.0-beta.2 → 1.4.0-beta.4
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 +25 -4
- package/index.d.ts +3 -0
- package/index.ts +3 -0
- package/package.json +9 -2
- package/src/main.ts +157 -42
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ Any attribute can be added to a head item. Simply provide the attribute as a key
|
|
|
60
60
|
|
|
61
61
|
`'innerHTML', 'textContent', 'priority', 'tagName', 'key'` are reserved keys and cannot be used as attributes.
|
|
62
62
|
|
|
63
|
-
To add content to a tag, prefer `textContent`. For `<script>` and `<style>`, `textContent` preserves the code text while escaping closing-tag sequences such as `</script>`. For tags like `<noscript>`, `textContent` is HTML-escaped.
|
|
63
|
+
To add content to a tag, prefer `textContent`. For `<script>` and `<style>`, `textContent` preserves the code text while escaping closing-tag sequences such as `</script>`. Script text that contains the HTML parser sequence `<!--` followed by `<script>` is rejected because it can consume the rest of the document; use an external script instead. For tags like `<noscript>`, `textContent` is HTML-escaped.
|
|
64
64
|
|
|
65
65
|
Use `innerHTML` only when you intentionally need raw, trusted markup. Do not pass CMS or user-provided strings into `innerHTML` or inline event-handler attributes.
|
|
66
66
|
|
|
@@ -79,6 +79,8 @@ See `applyPriority()` in the [Options](#options) section for more information on
|
|
|
79
79
|
|
|
80
80
|
Resource helpers return ordinary `link` or `script` items. They are optional, but they make common performance-sensitive tags harder to mistype.
|
|
81
81
|
|
|
82
|
+
`modulepreload()` accepts the standardized `as` destinations. Browser support beyond the default JavaScript module destination is still emerging, so verify non-script module types in your target browsers.
|
|
83
|
+
|
|
82
84
|
```ts
|
|
83
85
|
import {
|
|
84
86
|
externalScript,
|
|
@@ -120,6 +122,25 @@ preloadImage({
|
|
|
120
122
|
|
|
121
123
|
Use this when you want browsers that support responsive preloads to choose from `imagesrcset` without causing an additional fallback preload in browsers that do not. Width-descriptor `imagesrcset` values such as `640w` should include `imagesizes`; density descriptors such as `1x, 2x` do not need it.
|
|
122
124
|
|
|
125
|
+
The preload's `imagesrcset` and `imagesizes` must match the rendered `<img>` attributes so the browser can reuse the preload. When an `<img>` uses `src` as the density fallback, include that URL as an explicit `1x` candidate in both source sets:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
preloadImage({
|
|
129
|
+
imagesrcset: '/images/hero.jpg 1x, /images/hero@2x.jpg 2x',
|
|
130
|
+
fetchpriority: 'high'
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```html
|
|
135
|
+
<img
|
|
136
|
+
src="/images/hero.jpg"
|
|
137
|
+
srcset="/images/hero.jpg 1x, /images/hero@2x.jpg 2x"
|
|
138
|
+
alt=""
|
|
139
|
+
/>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
If the `<img>` instead has `srcset="/images/hero@1.5x.jpg 1.5x, /images/hero@2x.jpg 2x"`, a DPR-1 browser selects the `1.5x` candidate rather than its separate `src`. Preloading `src` in that case will not match the image request.
|
|
143
|
+
|
|
123
144
|
### SEO Helper
|
|
124
145
|
|
|
125
146
|
Use `createSeoHead()` when you want a small typed layer for common SEO tags while still keeping the raw `HeadItems` escape hatch.
|
|
@@ -209,7 +230,7 @@ const headItems: HeadItems = {
|
|
|
209
230
|
|
|
210
231
|
### JSON-LD / Structured Data
|
|
211
232
|
|
|
212
|
-
Use the `jsonLd` property to add [JSON-LD structured data](https://developers.google.com/search/docs/appearance/structured-data/intro-structured-data) to your pages. `@context` is set to `https://schema.org` automatically, `JSON.stringify()` is handled internally, and
|
|
233
|
+
Use the `jsonLd` property to add [JSON-LD structured data](https://developers.google.com/search/docs/appearance/structured-data/intro-structured-data) to your pages. `@context` is set to `https://schema.org` automatically, `JSON.stringify()` is handled internally, and less-than characters in values are escaped as JSON Unicode sequences so embedded data cannot change the HTML parser state.
|
|
213
234
|
|
|
214
235
|
```ts
|
|
215
236
|
const headItems: HeadItems = {
|
|
@@ -344,7 +365,7 @@ const issues = validateHeadItems(headItems, {
|
|
|
344
365
|
})
|
|
345
366
|
```
|
|
346
367
|
|
|
347
|
-
Validation returns structured issues with `code`, `severity`, `message`, `tagName`, and `path`. It checks for common mistakes such as missing titles/descriptions,
|
|
368
|
+
Validation returns structured issues with `code`, `severity`, `message`, `tagName`, and `path`. It checks for common mistakes such as missing or empty titles/descriptions, empty or relative canonicals, invalid preload and module-preload destinations, preloads without a usable source, responsive image preloads that use width descriptors without `imagesizes`, font preloads without `crossorigin`, incomplete or empty Open Graph basics, relative Open Graph/Twitter image URLs, raw `innerHTML`, unsafe script parser sequences, inline event attributes, JSON-LD serialization errors, and optional missing SRI on external scripts/styles.
|
|
348
369
|
|
|
349
370
|
Set `requireOpenGraphImage: true` if every shareable page in your project should provide `og:image`. By default, image-less website pages are allowed so dev validation does not warn on simple pages that still have useful `og:title`, `og:type`, and `og:url` tags.
|
|
350
371
|
|
|
@@ -428,7 +449,7 @@ Astro CSP is available in Astro 6 and newer. In Astro 4 and 5 this option is a n
|
|
|
428
449
|
- External `<script src>`.
|
|
429
450
|
- Stylesheet links.
|
|
430
451
|
- `rel="preload"` links with `as="script"` or `as="style"`.
|
|
431
|
-
- `rel="modulepreload"` links as script resources.
|
|
452
|
+
- `rel="modulepreload"` links with script-like destinations as script resources and `as="style"` as a style resource. JSON and text module destinations are not registered because Astro's helper API does not expose their `connect-src` directives.
|
|
432
453
|
|
|
433
454
|
It does not infer broader CSP directives from icons, images, fonts, analytics endpoints, or connection hints.
|
|
434
455
|
|
package/index.d.ts
CHANGED
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-helmet",
|
|
3
|
-
"version": "1.4.0-beta.
|
|
3
|
+
"version": "1.4.0-beta.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A document head manager for astro.",
|
|
6
6
|
"author": "Ryan Voitiskis <ryanvoitiskis@pm.me> (https://ryanvoitiskis.com/)",
|
|
@@ -29,14 +29,19 @@
|
|
|
29
29
|
],
|
|
30
30
|
"scripts": {
|
|
31
31
|
"test": "vitest",
|
|
32
|
-
"
|
|
32
|
+
"test:browser": "playwright test",
|
|
33
|
+
"coverage": "vitest run --coverage --coverage.thresholds.statements=95 --coverage.thresholds.branches=90 --coverage.thresholds.functions=95 --coverage.thresholds.lines=95",
|
|
33
34
|
"typecheck": "astro check",
|
|
34
35
|
"lint": "eslint .",
|
|
36
|
+
"lint:package": "publint --pack npm --strict && attw --pack . --profile esm-only",
|
|
37
|
+
"verify": "npm run lint && npm run typecheck && npm run coverage && npm run lint:package && npm pack --dry-run && npm audit --audit-level=moderate",
|
|
35
38
|
"release": "semantic-release"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
41
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
38
42
|
"@astrojs/check": "^0.9.9",
|
|
39
43
|
"@eslint/js": "^10.0.1",
|
|
44
|
+
"@playwright/test": "^1.61.1",
|
|
40
45
|
"@semantic-release/changelog": "^6.0.3",
|
|
41
46
|
"@semantic-release/git": "^10.0.1",
|
|
42
47
|
"@semantic-release/github": "^12.0.9",
|
|
@@ -44,8 +49,10 @@
|
|
|
44
49
|
"@vitest/coverage-v8": "^4.1.10",
|
|
45
50
|
"astro": "^7.0.6",
|
|
46
51
|
"eslint": "^10.6.0",
|
|
52
|
+
"parse5": "^8.0.1",
|
|
47
53
|
"prettier": "^3.9.4",
|
|
48
54
|
"prettier-plugin-astro": "^0.14.1",
|
|
55
|
+
"publint": "^0.3.21",
|
|
49
56
|
"semantic-release": "^25.0.5",
|
|
50
57
|
"typescript": "^6.0.3",
|
|
51
58
|
"typescript-eslint": "^8.63.0",
|
package/src/main.ts
CHANGED
|
@@ -5,18 +5,32 @@ const DEFAULT_VIEWPORT = {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
const VALID_PRELOAD_AS_VALUES = new Set([
|
|
8
|
-
'audio',
|
|
9
|
-
'document',
|
|
10
|
-
'embed',
|
|
11
8
|
'fetch',
|
|
12
9
|
'font',
|
|
13
10
|
'image',
|
|
14
|
-
'object',
|
|
15
|
-
'provider',
|
|
16
11
|
'script',
|
|
17
12
|
'style',
|
|
18
|
-
'track'
|
|
19
|
-
|
|
13
|
+
'track'
|
|
14
|
+
])
|
|
15
|
+
|
|
16
|
+
const VALID_MODULE_PRELOAD_AS_VALUES = new Set([
|
|
17
|
+
'audioworklet',
|
|
18
|
+
'json',
|
|
19
|
+
'paintworklet',
|
|
20
|
+
'script',
|
|
21
|
+
'serviceworker',
|
|
22
|
+
'sharedworker',
|
|
23
|
+
'style',
|
|
24
|
+
'text',
|
|
25
|
+
'worker'
|
|
26
|
+
])
|
|
27
|
+
|
|
28
|
+
const SCRIPT_LIKE_MODULE_PRELOAD_AS_VALUES = new Set([
|
|
29
|
+
'audioworklet',
|
|
30
|
+
'paintworklet',
|
|
31
|
+
'script',
|
|
32
|
+
'serviceworker',
|
|
33
|
+
'sharedworker',
|
|
20
34
|
'worker'
|
|
21
35
|
])
|
|
22
36
|
|
|
@@ -46,6 +60,20 @@ export type CrossOrigin = 'anonymous' | 'use-credentials' | ''
|
|
|
46
60
|
|
|
47
61
|
export type FetchPriority = 'high' | 'low' | 'auto'
|
|
48
62
|
|
|
63
|
+
export type ModulePreloadAs =
|
|
64
|
+
| 'audioworklet'
|
|
65
|
+
| 'json'
|
|
66
|
+
| 'paintworklet'
|
|
67
|
+
| 'script'
|
|
68
|
+
| 'serviceworker'
|
|
69
|
+
| 'sharedworker'
|
|
70
|
+
| 'style'
|
|
71
|
+
| 'text'
|
|
72
|
+
| 'worker'
|
|
73
|
+
|
|
74
|
+
export type PreloadAs =
|
|
75
|
+
'fetch' | 'font' | 'image' | 'script' | 'style' | 'track'
|
|
76
|
+
|
|
49
77
|
export type ReferrerPolicy =
|
|
50
78
|
| 'no-referrer'
|
|
51
79
|
| 'no-referrer-when-downgrade'
|
|
@@ -103,7 +131,6 @@ export type HeadValidationSeverity = 'warning' | 'error'
|
|
|
103
131
|
export type HeadValidationCode =
|
|
104
132
|
| 'missing-title'
|
|
105
133
|
| 'missing-description'
|
|
106
|
-
| 'duplicate-canonical'
|
|
107
134
|
| 'relative-canonical'
|
|
108
135
|
| 'canonical-with-noindex'
|
|
109
136
|
| 'invalid-url'
|
|
@@ -111,13 +138,15 @@ export type HeadValidationCode =
|
|
|
111
138
|
| 'invalid-preload-as'
|
|
112
139
|
| 'missing-preload-as'
|
|
113
140
|
| 'responsive-image-missing-sizes'
|
|
141
|
+
| 'responsive-image-missing-1x'
|
|
114
142
|
| 'font-preload-missing-crossorigin'
|
|
115
|
-
| 'modulepreload-
|
|
143
|
+
| 'invalid-modulepreload-as'
|
|
116
144
|
| 'preconnect-missing-href'
|
|
117
145
|
| 'dns-prefetch-missing-href'
|
|
118
146
|
| 'sri-missing-crossorigin'
|
|
119
147
|
| 'missing-sri'
|
|
120
148
|
| 'unsafe-inner-html'
|
|
149
|
+
| 'unsafe-script-text'
|
|
121
150
|
| 'unsafe-event-attribute'
|
|
122
151
|
| 'missing-og-required'
|
|
123
152
|
| 'relative-og-url'
|
|
@@ -147,7 +176,11 @@ export type ResourceHintOptions = Omit<LinkItem, 'rel' | 'href' | 'as'> & {
|
|
|
147
176
|
}
|
|
148
177
|
|
|
149
178
|
export type PreloadOptions = ResourceHintOptions & {
|
|
150
|
-
as:
|
|
179
|
+
as: PreloadAs
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export type ModulePreloadOptions = ResourceHintOptions & {
|
|
183
|
+
as?: ModulePreloadAs
|
|
151
184
|
}
|
|
152
185
|
|
|
153
186
|
export type PreloadFontOptions = ResourceHintOptions & {
|
|
@@ -465,7 +498,11 @@ export function getExternalResources(
|
|
|
465
498
|
if (hasRel(link, 'stylesheet') && link.href)
|
|
466
499
|
result.push({ type: 'style', url: toUrlString(link.href) })
|
|
467
500
|
else if (hasRel(link, 'modulepreload') && link.href) {
|
|
468
|
-
|
|
501
|
+
const as = normaliseAttrValue(link.as).toLowerCase() || 'script'
|
|
502
|
+
if (as === 'style')
|
|
503
|
+
result.push({ type: 'style', url: toUrlString(link.href) })
|
|
504
|
+
else if (SCRIPT_LIKE_MODULE_PRELOAD_AS_VALUES.has(as))
|
|
505
|
+
result.push({ type: 'script', url: toUrlString(link.href) })
|
|
469
506
|
} else if (hasRel(link, 'preload') && link.href) {
|
|
470
507
|
if (attrEquals(link.as, 'script'))
|
|
471
508
|
result.push({ type: 'script', url: toUrlString(link.href) })
|
|
@@ -538,7 +575,7 @@ export function preloadImage(
|
|
|
538
575
|
|
|
539
576
|
export function modulepreload(
|
|
540
577
|
href: string | URL,
|
|
541
|
-
options:
|
|
578
|
+
options: ModulePreloadOptions = {}
|
|
542
579
|
): LinkItem {
|
|
543
580
|
return { ...options, rel: 'modulepreload', href: toUrlString(href) }
|
|
544
581
|
}
|
|
@@ -658,7 +695,10 @@ export function validateHeadItems(
|
|
|
658
695
|
|
|
659
696
|
if (
|
|
660
697
|
validationOptions.requireDescription &&
|
|
661
|
-
!items.meta.some(
|
|
698
|
+
!items.meta.some(
|
|
699
|
+
(meta) =>
|
|
700
|
+
attrEquals(meta.name, 'description') && hasNonEmptyAttr(meta, 'content')
|
|
701
|
+
)
|
|
662
702
|
) {
|
|
663
703
|
issues.push({
|
|
664
704
|
code: 'missing-description',
|
|
@@ -687,6 +727,7 @@ function validateMetaItems(
|
|
|
687
727
|
options: HeadValidationOptions
|
|
688
728
|
): void {
|
|
689
729
|
const ogProperties = new Set<string>()
|
|
730
|
+
const populatedOgProperties = new Set<string>()
|
|
690
731
|
|
|
691
732
|
metaItems.forEach((meta, index) => {
|
|
692
733
|
validateUnsafeAttributes(meta, 'meta', `meta[${index}]`, issues, options)
|
|
@@ -694,7 +735,10 @@ function validateMetaItems(
|
|
|
694
735
|
const name = normaliseAttrValue(meta.name).toLowerCase()
|
|
695
736
|
const content = normaliseAttrValue(meta.content)
|
|
696
737
|
|
|
697
|
-
if (property.startsWith('og:'))
|
|
738
|
+
if (property.startsWith('og:')) {
|
|
739
|
+
ogProperties.add(property)
|
|
740
|
+
if (content) populatedOgProperties.add(property)
|
|
741
|
+
}
|
|
698
742
|
|
|
699
743
|
const ogUrlValid =
|
|
700
744
|
property === 'og:url'
|
|
@@ -748,7 +792,8 @@ function validateMetaItems(
|
|
|
748
792
|
|
|
749
793
|
const hasActiveOpenGraph =
|
|
750
794
|
ogProperties.size > 1 ||
|
|
751
|
-
[...ogProperties].some((property) => property !== 'og:type')
|
|
795
|
+
[...ogProperties].some((property) => property !== 'og:type') ||
|
|
796
|
+
(ogProperties.has('og:type') && !populatedOgProperties.has('og:type'))
|
|
752
797
|
|
|
753
798
|
if (hasActiveOpenGraph) {
|
|
754
799
|
const requiredProperties = [
|
|
@@ -758,7 +803,7 @@ function validateMetaItems(
|
|
|
758
803
|
...(options.requireOpenGraphImage ? ['og:image'] : [])
|
|
759
804
|
]
|
|
760
805
|
for (const property of requiredProperties) {
|
|
761
|
-
if (!
|
|
806
|
+
if (!populatedOgProperties.has(property)) {
|
|
762
807
|
issues.push({
|
|
763
808
|
code: 'missing-og-required',
|
|
764
809
|
severity: 'warning',
|
|
@@ -777,8 +822,6 @@ function validateLinkItems(
|
|
|
777
822
|
options: HeadValidationOptions,
|
|
778
823
|
hasNoindex: boolean
|
|
779
824
|
): void {
|
|
780
|
-
let canonicalCount = 0
|
|
781
|
-
|
|
782
825
|
linkItems.forEach((link, index) => {
|
|
783
826
|
validateUnsafeAttributes(link, 'link', `link[${index}]`, issues, options)
|
|
784
827
|
const rels = getRelTokens(link)
|
|
@@ -786,16 +829,33 @@ function validateLinkItems(
|
|
|
786
829
|
const path = `link[${index}]`
|
|
787
830
|
|
|
788
831
|
if (rels.includes('canonical')) {
|
|
789
|
-
|
|
790
|
-
const hrefValid = validateUrlValue(href, 'link', path, issues, options)
|
|
791
|
-
if (href && hrefValid && isRelativeUrl(href)) {
|
|
832
|
+
if (!href) {
|
|
792
833
|
issues.push({
|
|
793
|
-
code: '
|
|
834
|
+
code: 'invalid-url',
|
|
794
835
|
severity: 'warning',
|
|
795
836
|
tagName: 'link',
|
|
796
837
|
path,
|
|
797
|
-
message: 'Canonical links
|
|
838
|
+
message: 'Canonical links need a non-empty absolute HTTP(S) URL.'
|
|
798
839
|
})
|
|
840
|
+
} else {
|
|
841
|
+
const hrefValid = validateUrlValue(href, 'link', path, issues, options)
|
|
842
|
+
if (hrefValid && isRelativeUrl(href)) {
|
|
843
|
+
issues.push({
|
|
844
|
+
code: 'relative-canonical',
|
|
845
|
+
severity: 'warning',
|
|
846
|
+
tagName: 'link',
|
|
847
|
+
path,
|
|
848
|
+
message: 'Canonical links should be absolute URLs.'
|
|
849
|
+
})
|
|
850
|
+
} else if (hrefValid && !isAbsoluteHttpUrl(href)) {
|
|
851
|
+
issues.push({
|
|
852
|
+
code: 'invalid-url',
|
|
853
|
+
severity: 'warning',
|
|
854
|
+
tagName: 'link',
|
|
855
|
+
path,
|
|
856
|
+
message: 'Canonical links must use an absolute HTTP(S) URL.'
|
|
857
|
+
})
|
|
858
|
+
}
|
|
799
859
|
}
|
|
800
860
|
|
|
801
861
|
if (hasNoindex) {
|
|
@@ -832,13 +892,16 @@ function validateLinkItems(
|
|
|
832
892
|
if (rels.includes('preload')) validatePreload(link, issues, path)
|
|
833
893
|
|
|
834
894
|
if (rels.includes('modulepreload') && hasNonEmptyAttr(link, 'as')) {
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
895
|
+
const as = normaliseAttrValue(link.as).toLowerCase()
|
|
896
|
+
if (!VALID_MODULE_PRELOAD_AS_VALUES.has(as)) {
|
|
897
|
+
issues.push({
|
|
898
|
+
code: 'invalid-modulepreload-as',
|
|
899
|
+
severity: 'warning',
|
|
900
|
+
tagName: 'link',
|
|
901
|
+
path,
|
|
902
|
+
message: `modulepreload as="${as}" is not a standard destination.`
|
|
903
|
+
})
|
|
904
|
+
}
|
|
842
905
|
}
|
|
843
906
|
|
|
844
907
|
validateSubresourceIntegrity(
|
|
@@ -850,16 +913,6 @@ function validateLinkItems(
|
|
|
850
913
|
isSriRelevantLink(link)
|
|
851
914
|
)
|
|
852
915
|
})
|
|
853
|
-
|
|
854
|
-
if (canonicalCount > 1) {
|
|
855
|
-
issues.push({
|
|
856
|
-
code: 'duplicate-canonical',
|
|
857
|
-
severity: 'warning',
|
|
858
|
-
tagName: 'link',
|
|
859
|
-
path: 'link[rel="canonical"]',
|
|
860
|
-
message: 'Multiple canonical links were provided; the last one wins.'
|
|
861
|
-
})
|
|
862
|
-
}
|
|
863
916
|
}
|
|
864
917
|
|
|
865
918
|
function validatePreload(
|
|
@@ -919,6 +972,22 @@ function validatePreload(
|
|
|
919
972
|
})
|
|
920
973
|
}
|
|
921
974
|
|
|
975
|
+
if (
|
|
976
|
+
as === 'image' &&
|
|
977
|
+
!href &&
|
|
978
|
+
hasDensityDescriptorImageSrcset(imagesrcset) &&
|
|
979
|
+
!hasOneXImageSrcsetCandidate(imagesrcset)
|
|
980
|
+
) {
|
|
981
|
+
issues.push({
|
|
982
|
+
code: 'responsive-image-missing-1x',
|
|
983
|
+
severity: 'warning',
|
|
984
|
+
tagName: 'link',
|
|
985
|
+
path,
|
|
986
|
+
message:
|
|
987
|
+
'Density-descriptor responsive image preloads should include the rendered image src as an explicit 1x candidate when src provides its fallback.'
|
|
988
|
+
})
|
|
989
|
+
}
|
|
990
|
+
|
|
922
991
|
if (as === 'font' && !hasAttr(link, 'crossorigin')) {
|
|
923
992
|
issues.push({
|
|
924
993
|
code: 'font-preload-missing-crossorigin',
|
|
@@ -937,7 +1006,6 @@ function validateScriptItems(
|
|
|
937
1006
|
): void {
|
|
938
1007
|
scriptItems.forEach((script, index) => {
|
|
939
1008
|
const path = `script[${index}]`
|
|
940
|
-
validateUnsafeAttributes(script, 'script', path, issues, options)
|
|
941
1009
|
validateSubresourceIntegrity(script, 'script', path, issues, options, true)
|
|
942
1010
|
})
|
|
943
1011
|
}
|
|
@@ -951,6 +1019,20 @@ function validateContentItems(
|
|
|
951
1019
|
items.forEach((item, index) => {
|
|
952
1020
|
const path = `${tagName}[${index}]`
|
|
953
1021
|
validateUnsafeAttributes(item, tagName, path, issues, options)
|
|
1022
|
+
if (
|
|
1023
|
+
tagName === 'script' &&
|
|
1024
|
+
item.textContent !== undefined &&
|
|
1025
|
+
hasUnsafeScriptTextContent(String(item.textContent))
|
|
1026
|
+
) {
|
|
1027
|
+
issues.push({
|
|
1028
|
+
code: 'unsafe-script-text',
|
|
1029
|
+
severity: 'error',
|
|
1030
|
+
tagName,
|
|
1031
|
+
path,
|
|
1032
|
+
message:
|
|
1033
|
+
'script.textContent contains an HTML parser sequence that can consume the rest of the document. Use an external script instead.'
|
|
1034
|
+
})
|
|
1035
|
+
}
|
|
954
1036
|
if (options.warnOnInnerHTML !== false && item.innerHTML !== undefined) {
|
|
955
1037
|
issues.push({
|
|
956
1038
|
code: 'unsafe-inner-html',
|
|
@@ -1112,6 +1194,21 @@ function hasWidthDescriptorImageSrcset(imagesrcset: string): boolean {
|
|
|
1112
1194
|
})
|
|
1113
1195
|
}
|
|
1114
1196
|
|
|
1197
|
+
function hasDensityDescriptorImageSrcset(imagesrcset: string): boolean {
|
|
1198
|
+
return imagesrcset.split(',').some((candidate) => {
|
|
1199
|
+
const descriptor = candidate.trim().split(/\s+/).at(-1) || ''
|
|
1200
|
+
return /^\d*\.?\d+x$/.test(descriptor)
|
|
1201
|
+
})
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function hasOneXImageSrcsetCandidate(imagesrcset: string): boolean {
|
|
1205
|
+
return imagesrcset.split(',').some((candidate) => {
|
|
1206
|
+
const parts = candidate.trim().split(/\s+/)
|
|
1207
|
+
const descriptor = parts.at(-1) || ''
|
|
1208
|
+
return parts.length === 1 || /^(?:1|1\.0+)x$/.test(descriptor)
|
|
1209
|
+
})
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1115
1212
|
function hasRobotsDirective(metaItems: MetaItem[], directive: string): boolean {
|
|
1116
1213
|
return metaItems.some(
|
|
1117
1214
|
(meta) =>
|
|
@@ -1342,6 +1439,15 @@ function isRelativeUrl(value: string): boolean {
|
|
|
1342
1439
|
}
|
|
1343
1440
|
}
|
|
1344
1441
|
|
|
1442
|
+
function isAbsoluteHttpUrl(value: string): boolean {
|
|
1443
|
+
try {
|
|
1444
|
+
const url = new URL(value)
|
|
1445
|
+
return url.protocol === 'http:' || url.protocol === 'https:'
|
|
1446
|
+
} catch {
|
|
1447
|
+
return false
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1345
1451
|
function isExternalHttpUrl(value: string, baseUrl?: string | URL): boolean {
|
|
1346
1452
|
let url: URL
|
|
1347
1453
|
try {
|
|
@@ -1361,7 +1467,7 @@ function isExternalHttpUrl(value: string, baseUrl?: string | URL): boolean {
|
|
|
1361
1467
|
}
|
|
1362
1468
|
|
|
1363
1469
|
function escapeJsonLd(json: string): string {
|
|
1364
|
-
return json.replace(
|
|
1470
|
+
return json.replace(/</g, '\\u003c')
|
|
1365
1471
|
}
|
|
1366
1472
|
|
|
1367
1473
|
function deduplicateMetaItems(metaItems: BaseItem[]): BaseItem[] {
|
|
@@ -1526,9 +1632,18 @@ function getTagContent(item: BaseItem | ContentItem): string {
|
|
|
1526
1632
|
}
|
|
1527
1633
|
|
|
1528
1634
|
function escapeRawText(content: string, tagName: 'script' | 'style'): string {
|
|
1635
|
+
if (tagName === 'script' && hasUnsafeScriptTextContent(content)) {
|
|
1636
|
+
throw new Error(
|
|
1637
|
+
'script.textContent contains an HTML parser sequence that can consume the rest of the document. Use an external script or trusted innerHTML instead.'
|
|
1638
|
+
)
|
|
1639
|
+
}
|
|
1529
1640
|
return content.replace(new RegExp(`</${tagName}`, 'gi'), `<\\/${tagName}`)
|
|
1530
1641
|
}
|
|
1531
1642
|
|
|
1643
|
+
function hasUnsafeScriptTextContent(content: string): boolean {
|
|
1644
|
+
return /<!--[\s\S]*<script(?=[\t\n\f />])/i.test(content)
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1532
1647
|
function hasAttr(item: BaseItem | ContentItem, key: string): boolean {
|
|
1533
1648
|
return item[key] !== null && item[key] !== undefined
|
|
1534
1649
|
}
|