@mearl/preset 3.0.2 → 3.0.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/dist/installation.d.ts +0 -1
- package/dist/installation.js +60 -17
- package/package.json +1 -1
package/dist/installation.d.ts
CHANGED
package/dist/installation.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
function record(value, allowed) {
|
|
2
|
-
if (!value ||
|
|
2
|
+
if (!value ||
|
|
3
|
+
typeof value !== 'object' ||
|
|
4
|
+
Array.isArray(value) ||
|
|
5
|
+
Object.keys(value).some(key => !allowed.includes(key))) {
|
|
3
6
|
throw new Error('Invalid installation configuration');
|
|
4
7
|
}
|
|
5
8
|
return value;
|
|
@@ -19,15 +22,17 @@ export function parseInstallationConfig(value, publicOrigin) {
|
|
|
19
22
|
throw new Error('Invalid installation schema');
|
|
20
23
|
const result = { schemaVersion: 1 };
|
|
21
24
|
if (input.guide !== undefined) {
|
|
22
|
-
const guide = record(input.guide, ['url'
|
|
25
|
+
const guide = record(input.guide, ['url']);
|
|
23
26
|
const url = httpsUrl(guide.url);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
result.guide = { url: url.href, registry: registry.href };
|
|
27
|
+
if (url.origin === publicOrigin)
|
|
28
|
+
throw new Error('Invalid guide URL');
|
|
29
|
+
result.guide = { url: url.href };
|
|
28
30
|
}
|
|
29
31
|
if (input.presets !== undefined) {
|
|
30
|
-
if (!result.guide ||
|
|
32
|
+
if (!result.guide ||
|
|
33
|
+
!input.presets ||
|
|
34
|
+
typeof input.presets !== 'object' ||
|
|
35
|
+
Array.isArray(input.presets))
|
|
31
36
|
throw new Error('A preset catalog requires its guide');
|
|
32
37
|
const entries = Object.entries(input.presets);
|
|
33
38
|
if (entries.length > 32)
|
|
@@ -36,12 +41,20 @@ export function parseInstallationConfig(value, publicOrigin) {
|
|
|
36
41
|
if (!/^[a-z][a-z0-9-]{0,79}$/.test(id))
|
|
37
42
|
throw new Error('Invalid installation preset ID');
|
|
38
43
|
const entry = record(value, ['label', 'source', 'origins']);
|
|
39
|
-
if (typeof entry.label !== 'string' ||
|
|
44
|
+
if (typeof entry.label !== 'string' ||
|
|
45
|
+
!entry.label.trim() ||
|
|
46
|
+
entry.label.length > 120 ||
|
|
47
|
+
/[\u0000-\u001f\u007f]/.test(entry.label))
|
|
40
48
|
throw new Error('Invalid preset label');
|
|
41
49
|
const source = httpsUrl(entry.source).href;
|
|
42
50
|
if (!Array.isArray(entry.origins) || entry.origins.length > 64)
|
|
43
51
|
throw new Error('Expected preset permission origins');
|
|
44
|
-
const origins = [
|
|
52
|
+
const origins = [
|
|
53
|
+
...new Set([
|
|
54
|
+
`${new URL(source).origin}/*`,
|
|
55
|
+
...entry.origins.map(validateInstallationOrigin),
|
|
56
|
+
]),
|
|
57
|
+
];
|
|
45
58
|
if (origins.length > 64)
|
|
46
59
|
throw new Error('Installation permission origins exceed 64 scopes');
|
|
47
60
|
return [id, { label: entry.label, source, origins }];
|
|
@@ -51,13 +64,23 @@ export function parseInstallationConfig(value, publicOrigin) {
|
|
|
51
64
|
}
|
|
52
65
|
/** Explicit host scopes retain their selected schemes; download sources remain HTTPS. */
|
|
53
66
|
export function validateInstallationOrigin(value) {
|
|
54
|
-
if (typeof value !== 'string' ||
|
|
67
|
+
if (typeof value !== 'string' ||
|
|
68
|
+
value.length > 4096 ||
|
|
69
|
+
!/^(https?|\*):\/\//.test(value) ||
|
|
70
|
+
!value.endsWith('/*'))
|
|
55
71
|
throw new Error('Invalid installation permission origin');
|
|
56
72
|
const scheme = value.slice(0, value.indexOf(':'));
|
|
57
73
|
const wildcard = /^(?:https?|\*):\/\/\*\./.test(value);
|
|
58
74
|
const normalized = value.replace(/^\*:/, 'https:').replace(/^(https?:\/\/)\*\./, '$1');
|
|
59
75
|
const url = new URL(normalized);
|
|
60
|
-
if (url.username ||
|
|
76
|
+
if (url.username ||
|
|
77
|
+
url.password ||
|
|
78
|
+
url.hash ||
|
|
79
|
+
url.hostname.includes('*') ||
|
|
80
|
+
url.pathname !== '/*' ||
|
|
81
|
+
url.search ||
|
|
82
|
+
!url.hostname ||
|
|
83
|
+
(wildcard && !url.hostname.includes('.')))
|
|
61
84
|
throw new Error('Invalid installation permission origin');
|
|
62
85
|
return `${scheme}://${wildcard ? '*.' : ''}${url.host}/*`;
|
|
63
86
|
}
|
|
@@ -65,11 +88,15 @@ export function validateInstallationOrigin(value) {
|
|
|
65
88
|
export function matchesInstallationOrigin(scope, target) {
|
|
66
89
|
const value = validateInstallationOrigin(scope);
|
|
67
90
|
const scheme = value.slice(0, value.indexOf(':'));
|
|
68
|
-
if (scheme === '*'
|
|
91
|
+
if (scheme === '*'
|
|
92
|
+
? !['http:', 'https:'].includes(target.protocol)
|
|
93
|
+
: `${scheme}:` !== target.protocol)
|
|
69
94
|
return false;
|
|
70
95
|
const wildcard = /^(?:https?|\*):\/\/\*\./.test(value);
|
|
71
96
|
const origin = new URL(value.replace(/^\*:/, 'https:').replace(/^(https?:\/\/)\*\./, '$1'));
|
|
72
|
-
return (!origin.port || origin.port === target.port) &&
|
|
97
|
+
return ((!origin.port || origin.port === target.port) &&
|
|
98
|
+
(target.hostname === origin.hostname ||
|
|
99
|
+
(wildcard && target.hostname.endsWith(`.${origin.hostname}`))));
|
|
73
100
|
}
|
|
74
101
|
export const INSTALLATION_CONFIG_MAX_BYTES = 65_536;
|
|
75
102
|
export async function readInstallationJson(response, maxBytes) {
|
|
@@ -104,7 +131,12 @@ export async function readInstallationJson(response, maxBytes) {
|
|
|
104
131
|
return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
105
132
|
}
|
|
106
133
|
export async function loadInstallationConfig(url, publicOrigin, signal) {
|
|
107
|
-
const response = await fetch(url, {
|
|
134
|
+
const response = await fetch(url, {
|
|
135
|
+
cache: 'no-store',
|
|
136
|
+
credentials: 'omit',
|
|
137
|
+
redirect: 'error',
|
|
138
|
+
signal,
|
|
139
|
+
});
|
|
108
140
|
if (!response.ok || !response.headers.get('content-type')?.includes('application/json'))
|
|
109
141
|
throw new Error('Installation configuration unavailable');
|
|
110
142
|
return parseInstallationConfig(await readInstallationJson(response, INSTALLATION_CONFIG_MAX_BYTES), publicOrigin);
|
|
@@ -112,18 +144,29 @@ export async function loadInstallationConfig(url, publicOrigin, signal) {
|
|
|
112
144
|
export const PRESET_UPDATE_INTERVAL_MS = 12 * 60 * 60_000;
|
|
113
145
|
export const PRESET_UPDATE_RETRY_MS = 60 * 60_000;
|
|
114
146
|
export function trustedInstallationPreset(config, id, source) {
|
|
115
|
-
return id &&
|
|
147
|
+
return id &&
|
|
148
|
+
source &&
|
|
149
|
+
Object.hasOwn(config.presets ?? {}, id) &&
|
|
150
|
+
config.presets[id].source === source
|
|
151
|
+
? config.presets[id]
|
|
152
|
+
: undefined;
|
|
116
153
|
}
|
|
117
154
|
/** An exact scope can be covered by a broader catalog scope, never the reverse. */
|
|
118
155
|
export function installationOriginCovered(origin, scopes) {
|
|
119
156
|
const target = validateInstallationOrigin(origin);
|
|
120
157
|
const wildcard = /^(?:https?|\*):\/\/\*\./.test(target);
|
|
121
|
-
const schemes = target.startsWith('*:')
|
|
158
|
+
const schemes = target.startsWith('*:')
|
|
159
|
+
? ['http:', 'https:']
|
|
160
|
+
: [target.slice(0, target.indexOf(':') + 1)];
|
|
122
161
|
return scopes.some(scope => {
|
|
123
162
|
const candidate = validateInstallationOrigin(scope);
|
|
124
163
|
if (wildcard && !/^(?:https?|\*):\/\/\*\./.test(candidate))
|
|
125
164
|
return false;
|
|
126
165
|
const base = target.replace(/^\*:/, 'https:').replace(/^(https?:\/\/)\*\./, '$1');
|
|
127
|
-
return schemes.every(scheme => {
|
|
166
|
+
return schemes.every(scheme => {
|
|
167
|
+
const url = new URL(base);
|
|
168
|
+
url.protocol = scheme;
|
|
169
|
+
return matchesInstallationOrigin(candidate, url);
|
|
170
|
+
});
|
|
128
171
|
});
|
|
129
172
|
}
|