javascript-solid-server 0.0.225 → 0.0.227
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/SECURITY.md +8 -0
- package/bin/jss.js +14 -0
- package/docs/authentication.md +6 -0
- package/docs/configuration.md +1 -0
- package/docs/mashlib.md +21 -0
- package/docs/security.md +4 -0
- package/package.json +1 -1
- package/src/auth/cid-doc-fetch.js +44 -51
- package/src/auth/did-nostr.js +35 -42
- package/src/auth/middleware.js +1 -1
- package/src/auth/nostr.js +70 -7
- package/src/auth/webid-tls.js +35 -21
- package/src/config.js +4 -0
- package/src/handlers/resource.js +2 -2
- package/src/mashlib/index.js +146 -4
- package/src/mashlib/pane-loader.js +95 -0
- package/src/server.js +22 -0
- package/src/utils/bounded-body.js +89 -0
- package/test/auth-fetch-body-timeout.test.js +253 -0
- package/test/nip98-replay.test.js +145 -0
- package/test/panes.test.js +282 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Panes loaded at start-up (#652).
|
|
3
|
+
*
|
|
4
|
+
* `--panes` gives the server a list of pane module URLs. The data browser
|
|
5
|
+
* page loads and registers them before it starts SolidOS. These tests pin:
|
|
6
|
+
* - the list is cleaned (http(s) URLs and absolute paths only), and the
|
|
7
|
+
* dropped entries are reported
|
|
8
|
+
* - with no panes the generated page is unchanged
|
|
9
|
+
* - with panes the page loads them, then starts SolidOS (CDN and local)
|
|
10
|
+
* - module mode is not touched
|
|
11
|
+
* - a URL cannot break out of the inline script
|
|
12
|
+
* - the start-up function: always starts once, a dead or slow pane host
|
|
13
|
+
* holds back only its own pane, configured order is kept
|
|
14
|
+
* - the loader is served only when panes are configured, its path
|
|
15
|
+
* cannot be written to, and it loads nothing named in its own URL
|
|
16
|
+
* - the 401 page is a data browser page too, and gets the panes
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, it, before, after, beforeEach, afterEach } from 'node:test';
|
|
20
|
+
import assert from 'node:assert';
|
|
21
|
+
import fs from 'node:fs/promises';
|
|
22
|
+
import {
|
|
23
|
+
startTestServer,
|
|
24
|
+
stopTestServer,
|
|
25
|
+
request,
|
|
26
|
+
createTestPod,
|
|
27
|
+
assertStatus
|
|
28
|
+
} from './helpers.js';
|
|
29
|
+
import {
|
|
30
|
+
generateDatabrowserHtml,
|
|
31
|
+
generateModuleDatabrowserHtml,
|
|
32
|
+
jssStartDataBrowser,
|
|
33
|
+
normalizePanes,
|
|
34
|
+
sortPanes,
|
|
35
|
+
PANE_LOADER_PATH
|
|
36
|
+
} from '../src/mashlib/index.js';
|
|
37
|
+
|
|
38
|
+
const PANE = 'https://panes.example/calendar-pane.js';
|
|
39
|
+
const HTML = { Accept: 'text/html', 'Sec-Fetch-Dest': 'document' };
|
|
40
|
+
|
|
41
|
+
describe('panes at start-up — the list (unit, #652)', () => {
|
|
42
|
+
it('accepts an array or a comma-separated string', () => {
|
|
43
|
+
assert.deepStrictEqual(normalizePanes([PANE, '/public/panes/a.js']), [PANE, '/public/panes/a.js']);
|
|
44
|
+
assert.deepStrictEqual(normalizePanes(` ${PANE} , /public/panes/a.js ,`), [PANE, '/public/panes/a.js']);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('is empty for nothing', () => {
|
|
48
|
+
for (const v of [undefined, null, false, '', [], ' , ']) {
|
|
49
|
+
assert.deepStrictEqual(normalizePanes(v), []);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('drops anything that is not an http(s) URL or an absolute path', () => {
|
|
54
|
+
const bad = ['javascript:alert(1)', 'data:text/javascript,1', '../a.js', 'a.js', '//evil.example/a.js', 'ftp://x/a.js', 'https://x/a b.js', 42, null];
|
|
55
|
+
assert.deepStrictEqual(normalizePanes(bad), []);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('removes duplicates and keeps order', () => {
|
|
59
|
+
assert.deepStrictEqual(normalizePanes(['/b.js', PANE, '/b.js']), ['/b.js', PANE]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('reports what it dropped', () => {
|
|
63
|
+
assert.deepStrictEqual(sortPanes(`${PANE}, htps://typo/a.js ,, foo.js`), {
|
|
64
|
+
urls: [PANE],
|
|
65
|
+
dropped: ['htps://typo/a.js', 'foo.js']
|
|
66
|
+
});
|
|
67
|
+
assert.deepStrictEqual(sortPanes(undefined), { urls: [], dropped: [] });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('keeps a URL with a comma when given as an array', () => {
|
|
71
|
+
const url = 'https://cdn.example/combine/a.js,b.js';
|
|
72
|
+
assert.deepStrictEqual(normalizePanes([url]), [url]);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('panes at start-up — the page (unit, #652)', () => {
|
|
77
|
+
const modes = [
|
|
78
|
+
['CDN', (opts) => generateDatabrowserHtml('https://pod.example/', '2.0.0', opts)],
|
|
79
|
+
['local', (opts) => generateDatabrowserHtml('https://pod.example/', null, opts)]
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
for (const [name, page] of modes) {
|
|
83
|
+
it(`${name} mode: no panes leaves the page unchanged`, () => {
|
|
84
|
+
const plain = page({});
|
|
85
|
+
assert.strictEqual(page({ panes: [] }), plain);
|
|
86
|
+
assert.strictEqual(page({ panes: null }), plain);
|
|
87
|
+
assert.strictEqual(page({ panes: 'not a url' }), plain);
|
|
88
|
+
assert.ok(!plain.includes(PANE_LOADER_PATH));
|
|
89
|
+
assert.ok(!plain.includes('jssStartDataBrowser'));
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it(`${name} mode: panes are loaded through the loader, then SolidOS starts`, () => {
|
|
93
|
+
const html = page({ panes: [PANE] });
|
|
94
|
+
assert.ok(html.includes('function jssStartDataBrowser('), 'defines the start-up function');
|
|
95
|
+
assert.match(html, new RegExp(`jssStartDataBrowser\\(\\["${PANE.replace(/[.]/g, '\\.')}"\\], "/\\.well-known/jss/pane-loader\\.js\\?v=[^"]+", \\d+\\)`), 'calls it with the list, the versioned loader and a time limit');
|
|
96
|
+
// The only call to runDataBrowser is the one inside the function.
|
|
97
|
+
assert.strictEqual(html.split('panes.runDataBrowser()').length - 1, 1);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
it('module mode is not touched', () => {
|
|
102
|
+
const plain = generateModuleDatabrowserHtml('https://cdn.example/mashlib.js', 'https://pod.example/', {});
|
|
103
|
+
const withPanes = generateModuleDatabrowserHtml('https://cdn.example/mashlib.js', 'https://pod.example/', { panes: [PANE] });
|
|
104
|
+
assert.strictEqual(withPanes, plain);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('a pane URL cannot close the inline script', () => {
|
|
108
|
+
const nasty = 'https://panes.example/a.js?x=</script><script>alert(1)</script>';
|
|
109
|
+
for (const [, page] of modes) {
|
|
110
|
+
const html = page({ panes: [nasty] });
|
|
111
|
+
assert.ok(!html.includes('<script>alert(1)'), 'no injected script tag');
|
|
112
|
+
assert.ok(html.includes('\\u003c/script>'), 'the < is escaped inside the script');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('panes at start-up — the start-up function (unit, #652)', () => {
|
|
118
|
+
let starts, registered, log, saved;
|
|
119
|
+
|
|
120
|
+
// A fake loader: loadPane registers a pane named after its URL, or
|
|
121
|
+
// reports a failure for a URL the fake "network" rejects.
|
|
122
|
+
const fakeLoader = (failing = []) => ({
|
|
123
|
+
loadPane: async (url) => {
|
|
124
|
+
if (failing.includes(url)) return { url, ok: false, reason: 'import failed' };
|
|
125
|
+
registered.push(url);
|
|
126
|
+
return { url, ok: true, name: url };
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
// A fake import(): the loader URL gives the loader; each pane URL
|
|
130
|
+
// settles when the test says so.
|
|
131
|
+
const network = (loader, behaviour = {}) => (url) => {
|
|
132
|
+
if (url === 'LOADER') return loader ? Promise.resolve(loader) : Promise.reject(new Error('no loader'));
|
|
133
|
+
const b = behaviour[url];
|
|
134
|
+
if (b === 'hang') return new Promise(() => {});
|
|
135
|
+
if (b === 'fail') return Promise.reject(new Error('404'));
|
|
136
|
+
if (typeof b === 'number') return new Promise((resolve) => setTimeout(() => resolve({}), b));
|
|
137
|
+
return Promise.resolve({});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
beforeEach(() => {
|
|
141
|
+
starts = 0; registered = []; log = [];
|
|
142
|
+
saved = { panes: globalThis.panes, info: console.info, warn: console.warn };
|
|
143
|
+
globalThis.panes = { runDataBrowser: () => { starts++; log.push('start'); } };
|
|
144
|
+
console.info = (m) => log.push('info ' + m);
|
|
145
|
+
console.warn = (m) => log.push('warn ' + m);
|
|
146
|
+
});
|
|
147
|
+
afterEach(() => {
|
|
148
|
+
globalThis.panes = saved.panes; console.info = saved.info; console.warn = saved.warn;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('registers every pane, in the configured order, then starts once', async () => {
|
|
152
|
+
// b arrives before a, but a is listed first.
|
|
153
|
+
await jssStartDataBrowser(['a', 'b'], 'LOADER', 1000, network(fakeLoader(), { a: 30, b: 0 }));
|
|
154
|
+
assert.deepStrictEqual(registered, ['a', 'b']);
|
|
155
|
+
assert.strictEqual(starts, 1);
|
|
156
|
+
assert.strictEqual(log[log.length - 1], 'start', 'SolidOS starts after the panes');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('skips a pane that fails and still starts', async () => {
|
|
160
|
+
await jssStartDataBrowser(['bad', 'good'], 'LOADER', 1000, network(fakeLoader(['bad']), { bad: 'fail' }));
|
|
161
|
+
assert.deepStrictEqual(registered, ['good']);
|
|
162
|
+
assert.strictEqual(starts, 1);
|
|
163
|
+
assert.ok(log.some((l) => l.startsWith('warn [jss] pane skipped: bad')));
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('starts without panes when the loader cannot be imported', async () => {
|
|
167
|
+
await jssStartDataBrowser(['a'], 'LOADER', 1000, network(null));
|
|
168
|
+
assert.deepStrictEqual(registered, []);
|
|
169
|
+
assert.strictEqual(starts, 1);
|
|
170
|
+
assert.ok(log.some((l) => l.startsWith('warn [jss] panes not loaded')));
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('a host that never answers holds back only its own pane', async () => {
|
|
174
|
+
const t0 = Date.now();
|
|
175
|
+
await jssStartDataBrowser(['dead', 'good'], 'LOADER', 60, network(fakeLoader(), { dead: 'hang' }));
|
|
176
|
+
assert.ok(Date.now() - t0 < 1000, 'did not wait for the dead host');
|
|
177
|
+
assert.deepStrictEqual(registered, ['good'], 'the pane listed after the dead one is registered');
|
|
178
|
+
assert.strictEqual(starts, 1);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('a pane slower than the limit is registered when it arrives, without a second start', async () => {
|
|
182
|
+
await jssStartDataBrowser(['slow', 'good'], 'LOADER', 40, network(fakeLoader(), { slow: 120 }));
|
|
183
|
+
assert.deepStrictEqual(registered, ['good']);
|
|
184
|
+
assert.strictEqual(starts, 1);
|
|
185
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
186
|
+
assert.deepStrictEqual(registered, ['good', 'slow']);
|
|
187
|
+
assert.strictEqual(starts, 1);
|
|
188
|
+
assert.ok(log.some((l) => l.includes('"slow"') && l.includes('(after start-up)')));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('starts at the time limit even if the loader itself hangs', async () => {
|
|
192
|
+
const hangingLoader = (url) => new Promise(() => {});
|
|
193
|
+
jssStartDataBrowser(['a'], 'LOADER', 30, hangingLoader);
|
|
194
|
+
await new Promise((resolve) => setTimeout(resolve, 80));
|
|
195
|
+
assert.strictEqual(starts, 1);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe('panes at start-up — the shipped loader (unit, #652)', () => {
|
|
200
|
+
it('loads nothing named in its own URL', async () => {
|
|
201
|
+
const source = await fs.readFile(new URL('../src/mashlib/pane-loader.js', import.meta.url), 'utf8');
|
|
202
|
+
assert.ok(!/searchParams|import\.meta/.test(source), 'no ?pane= handling in the shipped copy');
|
|
203
|
+
assert.match(source, /export async function loadPane /);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('panes at start-up — live server with panes (#652)', () => {
|
|
208
|
+
before(async () => {
|
|
209
|
+
await startTestServer({ mashlibCdn: true, panes: `${PANE}, /public/panes/local-pane.js` });
|
|
210
|
+
await createTestPod('panetest');
|
|
211
|
+
});
|
|
212
|
+
after(async () => { await stopTestServer(); });
|
|
213
|
+
|
|
214
|
+
it('serves the loader as JavaScript, with or without the version query', async () => {
|
|
215
|
+
for (const url of [PANE_LOADER_PATH, PANE_LOADER_PATH + '?v=1.2.3']) {
|
|
216
|
+
const res = await request(url);
|
|
217
|
+
assertStatus(res, 200);
|
|
218
|
+
assert.match(res.headers.get('content-type'), /^text\/javascript/);
|
|
219
|
+
assert.match(await res.text(), /export async function loadPanes/);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('answers HEAD for the loader with the same length as GET', async () => {
|
|
224
|
+
const get = await request(PANE_LOADER_PATH);
|
|
225
|
+
await get.text();
|
|
226
|
+
const head = await request(PANE_LOADER_PATH, { method: 'HEAD' });
|
|
227
|
+
assertStatus(head, 200);
|
|
228
|
+
assert.match(head.headers.get('content-type'), /^text\/javascript/);
|
|
229
|
+
assert.strictEqual(head.headers.get('content-length'), get.headers.get('content-length'));
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('refuses writes to the loader path', async () => {
|
|
233
|
+
for (const method of ['PUT', 'DELETE', 'PATCH', 'POST']) {
|
|
234
|
+
const res = await request(PANE_LOADER_PATH, {
|
|
235
|
+
method,
|
|
236
|
+
headers: { 'Content-Type': 'text/javascript' },
|
|
237
|
+
body: method === 'DELETE' ? undefined : 'export default 1',
|
|
238
|
+
auth: 'panetest'
|
|
239
|
+
});
|
|
240
|
+
assertStatus(res, 405, `${method} must be refused`);
|
|
241
|
+
}
|
|
242
|
+
const res = await request(PANE_LOADER_PATH);
|
|
243
|
+
assert.match(await res.text(), /export async function loadPanes/, 'still the packaged loader');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('the data browser page loads the configured panes', async () => {
|
|
247
|
+
const res = await request('/panetest/public/', { headers: HTML });
|
|
248
|
+
assertStatus(res, 200);
|
|
249
|
+
const html = await res.text();
|
|
250
|
+
assert.ok(html.includes('function jssStartDataBrowser('));
|
|
251
|
+
assert.ok(html.includes(JSON.stringify([PANE, '/public/panes/local-pane.js'])));
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('the 401 page for a private resource loads them too', async () => {
|
|
255
|
+
const res = await request('/panetest/private/', { headers: HTML });
|
|
256
|
+
assertStatus(res, 401);
|
|
257
|
+
const html = await res.text();
|
|
258
|
+
assert.ok(html.includes('function jssStartDataBrowser('), 'the unauthorised page is a data browser page');
|
|
259
|
+
assert.ok(html.includes(JSON.stringify([PANE, '/public/panes/local-pane.js'])));
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe('panes at start-up — live server without panes (#652)', () => {
|
|
264
|
+
before(async () => {
|
|
265
|
+
await startTestServer({ mashlibCdn: true });
|
|
266
|
+
await createTestPod('nopanes');
|
|
267
|
+
});
|
|
268
|
+
after(async () => { await stopTestServer(); });
|
|
269
|
+
|
|
270
|
+
it('does not serve the loader', async () => {
|
|
271
|
+
const res = await request(PANE_LOADER_PATH);
|
|
272
|
+
assertStatus(res, 404);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('the data browser page starts SolidOS directly', async () => {
|
|
276
|
+
const res = await request('/nopanes/public/', { headers: HTML });
|
|
277
|
+
assertStatus(res, 200);
|
|
278
|
+
const html = await res.text();
|
|
279
|
+
assert.ok(!html.includes('jssStartDataBrowser'));
|
|
280
|
+
assert.ok(html.includes('panes.runDataBrowser()'));
|
|
281
|
+
});
|
|
282
|
+
});
|