astro-helmet 1.4.0-beta.1 → 1.4.0-beta.3
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 +24 -5
- package/index.d.ts +4 -0
- package/index.ts +4 -0
- package/package.json +9 -2
- package/src/main.ts +191 -44
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,
|
|
@@ -96,6 +98,11 @@ const headItems: HeadItems = {
|
|
|
96
98
|
preconnect('https://cdn.example.com'),
|
|
97
99
|
preloadFont('/fonts/Inter.woff2', { type: 'font/woff2' }),
|
|
98
100
|
preloadImage('/images/hero.jpg', { fetchpriority: 'high' }),
|
|
101
|
+
preloadImage({
|
|
102
|
+
imagesrcset: '/images/hero-640.jpg 640w, /images/hero-1280.jpg 1280w',
|
|
103
|
+
imagesizes: '100vw',
|
|
104
|
+
fetchpriority: 'high'
|
|
105
|
+
}),
|
|
99
106
|
modulepreload('/scripts/entry.js'),
|
|
100
107
|
stylesheet('/styles/site.css')
|
|
101
108
|
],
|
|
@@ -103,6 +110,18 @@ const headItems: HeadItems = {
|
|
|
103
110
|
}
|
|
104
111
|
```
|
|
105
112
|
|
|
113
|
+
`preloadImage()` also supports responsive image preloads without a fallback `href`:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
preloadImage({
|
|
117
|
+
imagesrcset: '/images/hero-640.jpg 640w, /images/hero-1280.jpg 1280w',
|
|
118
|
+
imagesizes: '100vw',
|
|
119
|
+
fetchpriority: 'high'
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
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.
|
|
124
|
+
|
|
106
125
|
### SEO Helper
|
|
107
126
|
|
|
108
127
|
Use `createSeoHead()` when you want a small typed layer for common SEO tags while still keeping the raw `HeadItems` escape hatch.
|
|
@@ -192,7 +211,7 @@ const headItems: HeadItems = {
|
|
|
192
211
|
|
|
193
212
|
### JSON-LD / Structured Data
|
|
194
213
|
|
|
195
|
-
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
|
|
214
|
+
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.
|
|
196
215
|
|
|
197
216
|
```ts
|
|
198
217
|
const headItems: HeadItems = {
|
|
@@ -259,7 +278,7 @@ Meta items later in the array replace earlier items. `name` and `http-equiv` mat
|
|
|
259
278
|
|
|
260
279
|
- `rel="canonical"` links, keeping the last one.
|
|
261
280
|
- `rel="preconnect"` and `rel="dns-prefetch"` links with the same `href`, keeping the last one.
|
|
262
|
-
- `rel="preload"` and `rel="modulepreload"` links with the same `href`, `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`, keeping the last one.
|
|
281
|
+
- `rel="preload"` and `rel="modulepreload"` links with the same `href`, `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`, keeping the last one. Responsive image preloads without `href` are deduplicated by `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`.
|
|
263
282
|
|
|
264
283
|
Any `meta`, `link`, `style`, `script`, or `noscript` item may provide `key` to opt into explicit last-write-wins deduplication:
|
|
265
284
|
|
|
@@ -327,7 +346,7 @@ const issues = validateHeadItems(headItems, {
|
|
|
327
346
|
})
|
|
328
347
|
```
|
|
329
348
|
|
|
330
|
-
Validation returns structured issues with `code`, `severity`, `message`, `tagName`, and `path`. It checks for common mistakes such as missing titles/descriptions,
|
|
349
|
+
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.
|
|
331
350
|
|
|
332
351
|
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.
|
|
333
352
|
|
|
@@ -411,7 +430,7 @@ Astro CSP is available in Astro 6 and newer. In Astro 4 and 5 this option is a n
|
|
|
411
430
|
- External `<script src>`.
|
|
412
431
|
- Stylesheet links.
|
|
413
432
|
- `rel="preload"` links with `as="script"` or `as="style"`.
|
|
414
|
-
- `rel="modulepreload"` links as script resources.
|
|
433
|
+
- `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.
|
|
415
434
|
|
|
416
435
|
It does not infer broader CSP directives from icons, images, fonts, analytics endpoints, or connection hints.
|
|
417
436
|
|
package/index.d.ts
CHANGED
|
@@ -34,12 +34,16 @@ export type {
|
|
|
34
34
|
JsonLdItem,
|
|
35
35
|
LinkItem,
|
|
36
36
|
MetaItem,
|
|
37
|
+
ModulePreloadAs,
|
|
38
|
+
ModulePreloadOptions,
|
|
37
39
|
NoscriptItem,
|
|
38
40
|
OpenGraphOptions,
|
|
41
|
+
PreloadAs,
|
|
39
42
|
PreloadFontOptions,
|
|
40
43
|
PreloadImageOptions,
|
|
41
44
|
PreloadOptions,
|
|
42
45
|
ReferrerPolicy,
|
|
46
|
+
ResponsivePreloadImageOptions,
|
|
43
47
|
ResourceHintOptions,
|
|
44
48
|
RobotsOptions,
|
|
45
49
|
ScriptItem,
|
package/index.ts
CHANGED
|
@@ -29,12 +29,16 @@ export type {
|
|
|
29
29
|
JsonLdItem,
|
|
30
30
|
LinkItem,
|
|
31
31
|
MetaItem,
|
|
32
|
+
ModulePreloadAs,
|
|
33
|
+
ModulePreloadOptions,
|
|
32
34
|
NoscriptItem,
|
|
33
35
|
OpenGraphOptions,
|
|
36
|
+
PreloadAs,
|
|
34
37
|
PreloadFontOptions,
|
|
35
38
|
PreloadImageOptions,
|
|
36
39
|
PreloadOptions,
|
|
37
40
|
ReferrerPolicy,
|
|
41
|
+
ResponsivePreloadImageOptions,
|
|
38
42
|
ResourceHintOptions,
|
|
39
43
|
RobotsOptions,
|
|
40
44
|
ScriptItem,
|
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.3",
|
|
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,19 +131,21 @@ 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'
|
|
137
|
+
| 'missing-preload-source'
|
|
110
138
|
| 'invalid-preload-as'
|
|
111
139
|
| 'missing-preload-as'
|
|
140
|
+
| 'responsive-image-missing-sizes'
|
|
112
141
|
| 'font-preload-missing-crossorigin'
|
|
113
|
-
| 'modulepreload-
|
|
142
|
+
| 'invalid-modulepreload-as'
|
|
114
143
|
| 'preconnect-missing-href'
|
|
115
144
|
| 'dns-prefetch-missing-href'
|
|
116
145
|
| 'sri-missing-crossorigin'
|
|
117
146
|
| 'missing-sri'
|
|
118
147
|
| 'unsafe-inner-html'
|
|
148
|
+
| 'unsafe-script-text'
|
|
119
149
|
| 'unsafe-event-attribute'
|
|
120
150
|
| 'missing-og-required'
|
|
121
151
|
| 'relative-og-url'
|
|
@@ -145,7 +175,11 @@ export type ResourceHintOptions = Omit<LinkItem, 'rel' | 'href' | 'as'> & {
|
|
|
145
175
|
}
|
|
146
176
|
|
|
147
177
|
export type PreloadOptions = ResourceHintOptions & {
|
|
148
|
-
as:
|
|
178
|
+
as: PreloadAs
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export type ModulePreloadOptions = ResourceHintOptions & {
|
|
182
|
+
as?: ModulePreloadAs
|
|
149
183
|
}
|
|
150
184
|
|
|
151
185
|
export type PreloadFontOptions = ResourceHintOptions & {
|
|
@@ -159,6 +193,11 @@ export type PreloadImageOptions = ResourceHintOptions & {
|
|
|
159
193
|
fetchpriority?: FetchPriority
|
|
160
194
|
}
|
|
161
195
|
|
|
196
|
+
export type ResponsivePreloadImageOptions = PreloadImageOptions & {
|
|
197
|
+
href?: string | URL
|
|
198
|
+
imagesrcset: string
|
|
199
|
+
}
|
|
200
|
+
|
|
162
201
|
export type StylesheetOptions = Omit<LinkItem, 'rel' | 'href'>
|
|
163
202
|
|
|
164
203
|
export type ExternalScriptOptions = Omit<ScriptItem, 'src'>
|
|
@@ -458,7 +497,11 @@ export function getExternalResources(
|
|
|
458
497
|
if (hasRel(link, 'stylesheet') && link.href)
|
|
459
498
|
result.push({ type: 'style', url: toUrlString(link.href) })
|
|
460
499
|
else if (hasRel(link, 'modulepreload') && link.href) {
|
|
461
|
-
|
|
500
|
+
const as = normaliseAttrValue(link.as).toLowerCase() || 'script'
|
|
501
|
+
if (as === 'style')
|
|
502
|
+
result.push({ type: 'style', url: toUrlString(link.href) })
|
|
503
|
+
else if (SCRIPT_LIKE_MODULE_PRELOAD_AS_VALUES.has(as))
|
|
504
|
+
result.push({ type: 'script', url: toUrlString(link.href) })
|
|
462
505
|
} else if (hasRel(link, 'preload') && link.href) {
|
|
463
506
|
if (attrEquals(link.as, 'script'))
|
|
464
507
|
result.push({ type: 'script', url: toUrlString(link.href) })
|
|
@@ -507,14 +550,31 @@ export function preloadFont(
|
|
|
507
550
|
|
|
508
551
|
export function preloadImage(
|
|
509
552
|
href: string | URL,
|
|
553
|
+
options?: PreloadImageOptions
|
|
554
|
+
): LinkItem
|
|
555
|
+
export function preloadImage(options: ResponsivePreloadImageOptions): LinkItem
|
|
556
|
+
export function preloadImage(
|
|
557
|
+
hrefOrOptions: string | URL | ResponsivePreloadImageOptions,
|
|
510
558
|
options: PreloadImageOptions = {}
|
|
511
559
|
): LinkItem {
|
|
512
|
-
|
|
560
|
+
if (typeof hrefOrOptions === 'string' || hrefOrOptions instanceof URL) {
|
|
561
|
+
return {
|
|
562
|
+
...options,
|
|
563
|
+
rel: 'preload',
|
|
564
|
+
href: toUrlString(hrefOrOptions),
|
|
565
|
+
as: 'image'
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const { href, ...rest } = hrefOrOptions
|
|
570
|
+
const link: LinkItem = { ...rest, rel: 'preload', as: 'image' }
|
|
571
|
+
if (href !== undefined) link.href = toUrlString(href)
|
|
572
|
+
return link
|
|
513
573
|
}
|
|
514
574
|
|
|
515
575
|
export function modulepreload(
|
|
516
576
|
href: string | URL,
|
|
517
|
-
options:
|
|
577
|
+
options: ModulePreloadOptions = {}
|
|
518
578
|
): LinkItem {
|
|
519
579
|
return { ...options, rel: 'modulepreload', href: toUrlString(href) }
|
|
520
580
|
}
|
|
@@ -634,7 +694,10 @@ export function validateHeadItems(
|
|
|
634
694
|
|
|
635
695
|
if (
|
|
636
696
|
validationOptions.requireDescription &&
|
|
637
|
-
!items.meta.some(
|
|
697
|
+
!items.meta.some(
|
|
698
|
+
(meta) =>
|
|
699
|
+
attrEquals(meta.name, 'description') && hasNonEmptyAttr(meta, 'content')
|
|
700
|
+
)
|
|
638
701
|
) {
|
|
639
702
|
issues.push({
|
|
640
703
|
code: 'missing-description',
|
|
@@ -663,6 +726,7 @@ function validateMetaItems(
|
|
|
663
726
|
options: HeadValidationOptions
|
|
664
727
|
): void {
|
|
665
728
|
const ogProperties = new Set<string>()
|
|
729
|
+
const populatedOgProperties = new Set<string>()
|
|
666
730
|
|
|
667
731
|
metaItems.forEach((meta, index) => {
|
|
668
732
|
validateUnsafeAttributes(meta, 'meta', `meta[${index}]`, issues, options)
|
|
@@ -670,7 +734,10 @@ function validateMetaItems(
|
|
|
670
734
|
const name = normaliseAttrValue(meta.name).toLowerCase()
|
|
671
735
|
const content = normaliseAttrValue(meta.content)
|
|
672
736
|
|
|
673
|
-
if (property.startsWith('og:'))
|
|
737
|
+
if (property.startsWith('og:')) {
|
|
738
|
+
ogProperties.add(property)
|
|
739
|
+
if (content) populatedOgProperties.add(property)
|
|
740
|
+
}
|
|
674
741
|
|
|
675
742
|
const ogUrlValid =
|
|
676
743
|
property === 'og:url'
|
|
@@ -724,7 +791,8 @@ function validateMetaItems(
|
|
|
724
791
|
|
|
725
792
|
const hasActiveOpenGraph =
|
|
726
793
|
ogProperties.size > 1 ||
|
|
727
|
-
[...ogProperties].some((property) => property !== 'og:type')
|
|
794
|
+
[...ogProperties].some((property) => property !== 'og:type') ||
|
|
795
|
+
(ogProperties.has('og:type') && !populatedOgProperties.has('og:type'))
|
|
728
796
|
|
|
729
797
|
if (hasActiveOpenGraph) {
|
|
730
798
|
const requiredProperties = [
|
|
@@ -734,7 +802,7 @@ function validateMetaItems(
|
|
|
734
802
|
...(options.requireOpenGraphImage ? ['og:image'] : [])
|
|
735
803
|
]
|
|
736
804
|
for (const property of requiredProperties) {
|
|
737
|
-
if (!
|
|
805
|
+
if (!populatedOgProperties.has(property)) {
|
|
738
806
|
issues.push({
|
|
739
807
|
code: 'missing-og-required',
|
|
740
808
|
severity: 'warning',
|
|
@@ -753,8 +821,6 @@ function validateLinkItems(
|
|
|
753
821
|
options: HeadValidationOptions,
|
|
754
822
|
hasNoindex: boolean
|
|
755
823
|
): void {
|
|
756
|
-
let canonicalCount = 0
|
|
757
|
-
|
|
758
824
|
linkItems.forEach((link, index) => {
|
|
759
825
|
validateUnsafeAttributes(link, 'link', `link[${index}]`, issues, options)
|
|
760
826
|
const rels = getRelTokens(link)
|
|
@@ -762,16 +828,33 @@ function validateLinkItems(
|
|
|
762
828
|
const path = `link[${index}]`
|
|
763
829
|
|
|
764
830
|
if (rels.includes('canonical')) {
|
|
765
|
-
|
|
766
|
-
const hrefValid = validateUrlValue(href, 'link', path, issues, options)
|
|
767
|
-
if (href && hrefValid && isRelativeUrl(href)) {
|
|
831
|
+
if (!href) {
|
|
768
832
|
issues.push({
|
|
769
|
-
code: '
|
|
833
|
+
code: 'invalid-url',
|
|
770
834
|
severity: 'warning',
|
|
771
835
|
tagName: 'link',
|
|
772
836
|
path,
|
|
773
|
-
message: 'Canonical links
|
|
837
|
+
message: 'Canonical links need a non-empty absolute HTTP(S) URL.'
|
|
774
838
|
})
|
|
839
|
+
} else {
|
|
840
|
+
const hrefValid = validateUrlValue(href, 'link', path, issues, options)
|
|
841
|
+
if (hrefValid && isRelativeUrl(href)) {
|
|
842
|
+
issues.push({
|
|
843
|
+
code: 'relative-canonical',
|
|
844
|
+
severity: 'warning',
|
|
845
|
+
tagName: 'link',
|
|
846
|
+
path,
|
|
847
|
+
message: 'Canonical links should be absolute URLs.'
|
|
848
|
+
})
|
|
849
|
+
} else if (hrefValid && !isAbsoluteHttpUrl(href)) {
|
|
850
|
+
issues.push({
|
|
851
|
+
code: 'invalid-url',
|
|
852
|
+
severity: 'warning',
|
|
853
|
+
tagName: 'link',
|
|
854
|
+
path,
|
|
855
|
+
message: 'Canonical links must use an absolute HTTP(S) URL.'
|
|
856
|
+
})
|
|
857
|
+
}
|
|
775
858
|
}
|
|
776
859
|
|
|
777
860
|
if (hasNoindex) {
|
|
@@ -808,13 +891,16 @@ function validateLinkItems(
|
|
|
808
891
|
if (rels.includes('preload')) validatePreload(link, issues, path)
|
|
809
892
|
|
|
810
893
|
if (rels.includes('modulepreload') && hasNonEmptyAttr(link, 'as')) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
894
|
+
const as = normaliseAttrValue(link.as).toLowerCase()
|
|
895
|
+
if (!VALID_MODULE_PRELOAD_AS_VALUES.has(as)) {
|
|
896
|
+
issues.push({
|
|
897
|
+
code: 'invalid-modulepreload-as',
|
|
898
|
+
severity: 'warning',
|
|
899
|
+
tagName: 'link',
|
|
900
|
+
path,
|
|
901
|
+
message: `modulepreload as="${as}" is not a standard destination.`
|
|
902
|
+
})
|
|
903
|
+
}
|
|
818
904
|
}
|
|
819
905
|
|
|
820
906
|
validateSubresourceIntegrity(
|
|
@@ -826,16 +912,6 @@ function validateLinkItems(
|
|
|
826
912
|
isSriRelevantLink(link)
|
|
827
913
|
)
|
|
828
914
|
})
|
|
829
|
-
|
|
830
|
-
if (canonicalCount > 1) {
|
|
831
|
-
issues.push({
|
|
832
|
-
code: 'duplicate-canonical',
|
|
833
|
-
severity: 'warning',
|
|
834
|
-
tagName: 'link',
|
|
835
|
-
path: 'link[rel="canonical"]',
|
|
836
|
-
message: 'Multiple canonical links were provided; the last one wins.'
|
|
837
|
-
})
|
|
838
|
-
}
|
|
839
915
|
}
|
|
840
916
|
|
|
841
917
|
function validatePreload(
|
|
@@ -844,6 +920,21 @@ function validatePreload(
|
|
|
844
920
|
path: string
|
|
845
921
|
): void {
|
|
846
922
|
const as = normaliseAttrValue(link.as).toLowerCase()
|
|
923
|
+
const href = normaliseAttrValue(link.href)
|
|
924
|
+
const imagesrcset = normaliseAttrValue(link.imagesrcset)
|
|
925
|
+
const hasResponsiveImageSource = as === 'image' && !!imagesrcset
|
|
926
|
+
|
|
927
|
+
if (!href && !hasResponsiveImageSource) {
|
|
928
|
+
issues.push({
|
|
929
|
+
code: 'missing-preload-source',
|
|
930
|
+
severity: 'warning',
|
|
931
|
+
tagName: 'link',
|
|
932
|
+
path,
|
|
933
|
+
message:
|
|
934
|
+
'Preload links need an href, except responsive image preloads with imagesrcset.'
|
|
935
|
+
})
|
|
936
|
+
}
|
|
937
|
+
|
|
847
938
|
if (!as) {
|
|
848
939
|
issues.push({
|
|
849
940
|
code: 'missing-preload-as',
|
|
@@ -865,6 +956,21 @@ function validatePreload(
|
|
|
865
956
|
})
|
|
866
957
|
}
|
|
867
958
|
|
|
959
|
+
if (
|
|
960
|
+
as === 'image' &&
|
|
961
|
+
hasWidthDescriptorImageSrcset(imagesrcset) &&
|
|
962
|
+
!hasNonEmptyAttr(link, 'imagesizes')
|
|
963
|
+
) {
|
|
964
|
+
issues.push({
|
|
965
|
+
code: 'responsive-image-missing-sizes',
|
|
966
|
+
severity: 'warning',
|
|
967
|
+
tagName: 'link',
|
|
968
|
+
path,
|
|
969
|
+
message:
|
|
970
|
+
'Responsive image preloads with width descriptors need imagesizes.'
|
|
971
|
+
})
|
|
972
|
+
}
|
|
973
|
+
|
|
868
974
|
if (as === 'font' && !hasAttr(link, 'crossorigin')) {
|
|
869
975
|
issues.push({
|
|
870
976
|
code: 'font-preload-missing-crossorigin',
|
|
@@ -883,7 +989,6 @@ function validateScriptItems(
|
|
|
883
989
|
): void {
|
|
884
990
|
scriptItems.forEach((script, index) => {
|
|
885
991
|
const path = `script[${index}]`
|
|
886
|
-
validateUnsafeAttributes(script, 'script', path, issues, options)
|
|
887
992
|
validateSubresourceIntegrity(script, 'script', path, issues, options, true)
|
|
888
993
|
})
|
|
889
994
|
}
|
|
@@ -897,6 +1002,20 @@ function validateContentItems(
|
|
|
897
1002
|
items.forEach((item, index) => {
|
|
898
1003
|
const path = `${tagName}[${index}]`
|
|
899
1004
|
validateUnsafeAttributes(item, tagName, path, issues, options)
|
|
1005
|
+
if (
|
|
1006
|
+
tagName === 'script' &&
|
|
1007
|
+
item.textContent !== undefined &&
|
|
1008
|
+
hasUnsafeScriptTextContent(String(item.textContent))
|
|
1009
|
+
) {
|
|
1010
|
+
issues.push({
|
|
1011
|
+
code: 'unsafe-script-text',
|
|
1012
|
+
severity: 'error',
|
|
1013
|
+
tagName,
|
|
1014
|
+
path,
|
|
1015
|
+
message:
|
|
1016
|
+
'script.textContent contains an HTML parser sequence that can consume the rest of the document. Use an external script instead.'
|
|
1017
|
+
})
|
|
1018
|
+
}
|
|
900
1019
|
if (options.warnOnInnerHTML !== false && item.innerHTML !== undefined) {
|
|
901
1020
|
issues.push({
|
|
902
1021
|
code: 'unsafe-inner-html',
|
|
@@ -1051,6 +1170,13 @@ function isSriRelevantLink(link: LinkItem): boolean {
|
|
|
1051
1170
|
return as === 'script' || as === 'style'
|
|
1052
1171
|
}
|
|
1053
1172
|
|
|
1173
|
+
function hasWidthDescriptorImageSrcset(imagesrcset: string): boolean {
|
|
1174
|
+
return imagesrcset.split(',').some((candidate) => {
|
|
1175
|
+
const descriptor = candidate.trim().split(/\s+/).at(-1) || ''
|
|
1176
|
+
return /^\d+w$/.test(descriptor)
|
|
1177
|
+
})
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1054
1180
|
function hasRobotsDirective(metaItems: MetaItem[], directive: string): boolean {
|
|
1055
1181
|
return metaItems.some(
|
|
1056
1182
|
(meta) =>
|
|
@@ -1281,6 +1407,15 @@ function isRelativeUrl(value: string): boolean {
|
|
|
1281
1407
|
}
|
|
1282
1408
|
}
|
|
1283
1409
|
|
|
1410
|
+
function isAbsoluteHttpUrl(value: string): boolean {
|
|
1411
|
+
try {
|
|
1412
|
+
const url = new URL(value)
|
|
1413
|
+
return url.protocol === 'http:' || url.protocol === 'https:'
|
|
1414
|
+
} catch {
|
|
1415
|
+
return false
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1284
1419
|
function isExternalHttpUrl(value: string, baseUrl?: string | URL): boolean {
|
|
1285
1420
|
let url: URL
|
|
1286
1421
|
try {
|
|
@@ -1300,7 +1435,7 @@ function isExternalHttpUrl(value: string, baseUrl?: string | URL): boolean {
|
|
|
1300
1435
|
}
|
|
1301
1436
|
|
|
1302
1437
|
function escapeJsonLd(json: string): string {
|
|
1303
|
-
return json.replace(
|
|
1438
|
+
return json.replace(/</g, '\\u003c')
|
|
1304
1439
|
}
|
|
1305
1440
|
|
|
1306
1441
|
function deduplicateMetaItems(metaItems: BaseItem[]): BaseItem[] {
|
|
@@ -1404,7 +1539,9 @@ function getLinkDeduplicationKey(link: BaseItem): string | undefined {
|
|
|
1404
1539
|
if ((rels.includes('preconnect') || rels.includes('dns-prefetch')) && href)
|
|
1405
1540
|
return `${rels.join(' ')}:${href}`
|
|
1406
1541
|
|
|
1407
|
-
if (
|
|
1542
|
+
if (rels.includes('preload') || rels.includes('modulepreload')) {
|
|
1543
|
+
if (!href && !(rels.includes('preload') && as === 'image' && imagesrcset))
|
|
1544
|
+
return undefined
|
|
1408
1545
|
return [
|
|
1409
1546
|
rels.join(' '),
|
|
1410
1547
|
href,
|
|
@@ -1415,6 +1552,7 @@ function getLinkDeduplicationKey(link: BaseItem): string | undefined {
|
|
|
1415
1552
|
imagesrcset,
|
|
1416
1553
|
imagesizes
|
|
1417
1554
|
].join(':')
|
|
1555
|
+
}
|
|
1418
1556
|
}
|
|
1419
1557
|
|
|
1420
1558
|
function renderHeadTag(item: BaseItem | ContentItem): string {
|
|
@@ -1462,9 +1600,18 @@ function getTagContent(item: BaseItem | ContentItem): string {
|
|
|
1462
1600
|
}
|
|
1463
1601
|
|
|
1464
1602
|
function escapeRawText(content: string, tagName: 'script' | 'style'): string {
|
|
1603
|
+
if (tagName === 'script' && hasUnsafeScriptTextContent(content)) {
|
|
1604
|
+
throw new Error(
|
|
1605
|
+
'script.textContent contains an HTML parser sequence that can consume the rest of the document. Use an external script or trusted innerHTML instead.'
|
|
1606
|
+
)
|
|
1607
|
+
}
|
|
1465
1608
|
return content.replace(new RegExp(`</${tagName}`, 'gi'), `<\\/${tagName}`)
|
|
1466
1609
|
}
|
|
1467
1610
|
|
|
1611
|
+
function hasUnsafeScriptTextContent(content: string): boolean {
|
|
1612
|
+
return /<!--[\s\S]*<script(?=[\t\n\f />])/i.test(content)
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1468
1615
|
function hasAttr(item: BaseItem | ContentItem, key: string): boolean {
|
|
1469
1616
|
return item[key] !== null && item[key] !== undefined
|
|
1470
1617
|
}
|