javascript-solid-server 0.0.187 → 0.0.189

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.
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Unit tests for owner-key provisioning (Phase 1 of #437).
3
+ */
4
+
5
+ import { describe, it } from 'node:test';
6
+ import assert from 'node:assert';
7
+ import { schnorr } from '@noble/curves/secp256k1';
8
+ import {
9
+ generateOwnerKeypair,
10
+ publicKeyMultibase,
11
+ secretKeyMultibase,
12
+ buildOwnerKeyDocument,
13
+ provisionOwnerKey,
14
+ assertProvisionKeysCompatible
15
+ } from '../src/keys/provision.js';
16
+ import { decodeFFormSecp256k1 } from '../src/auth/nostr-keys.js';
17
+
18
+ describe('generateOwnerKeypair', () => {
19
+ it('produces a 32-byte x-only pubkey and a 32-byte secret', () => {
20
+ const { publicHex, secretHex } = generateOwnerKeypair();
21
+ assert.match(publicHex, /^[0-9a-f]{64}$/);
22
+ assert.match(secretHex, /^[0-9a-f]{64}$/);
23
+ });
24
+
25
+ it('produces a working Schnorr keypair (sign + verify)', () => {
26
+ const { publicHex, secretHex } = generateOwnerKeypair();
27
+ const message = new TextEncoder().encode('jss provision-keys roundtrip');
28
+ const sig = schnorr.sign(message, hexToBytes(secretHex));
29
+ assert.strictEqual(
30
+ schnorr.verify(sig, message, hexToBytes(publicHex)),
31
+ true,
32
+ 'signature must verify under the generated pubkey'
33
+ );
34
+ });
35
+
36
+ it('produces a different keypair on every call', () => {
37
+ const a = generateOwnerKeypair();
38
+ const b = generateOwnerKeypair();
39
+ assert.notStrictEqual(a.publicHex, b.publicHex);
40
+ assert.notStrictEqual(a.secretHex, b.secretHex);
41
+ });
42
+ });
43
+
44
+ describe('publicKeyMultibase / secretKeyMultibase', () => {
45
+ // Fixed test vectors so the encoding format is pinned (any change to
46
+ // multicodec/multibase would shift these values and fail the assertion).
47
+ const publicHex = '87a1c6f0e9b3d2456789abcdef0123456789abcdef0123456789abcdef012345';
48
+ const secretHex = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
49
+
50
+ it('encodes a public key as f + e701 + 02 + 64-hex-pubkey', () => {
51
+ const mb = publicKeyMultibase(publicHex);
52
+ // f (multibase) + e701 (multicodec varint for secp256k1-pub) +
53
+ // 02 (BIP-340 even-y parity) + 64-char pubkey hex
54
+ assert.strictEqual(mb, 'fe70102' + publicHex);
55
+ });
56
+
57
+ it('encodes a secret key as f + 8126 + 64-hex-secret', () => {
58
+ const mb = secretKeyMultibase(secretHex);
59
+ // f (multibase) + 8126 (multicodec varint for secp256k1-priv) +
60
+ // 64-char secret hex (no parity byte — secret IS the scalar)
61
+ assert.strictEqual(mb, 'f8126' + secretHex);
62
+ });
63
+
64
+ it('round-trips through src/auth/nostr-keys decoder for the public side', () => {
65
+ // The Multikey value we write must be readable by jss's existing
66
+ // f-form decoder (used by the did:nostr verifier). This pins the
67
+ // shape so a future change to either side fails loud.
68
+ const mb = publicKeyMultibase(publicHex);
69
+ const decoded = decodeFFormSecp256k1(mb);
70
+ assert.strictEqual(decoded, publicHex);
71
+ });
72
+
73
+ it('rejects non-hex / wrong-length inputs', () => {
74
+ assert.throws(() => publicKeyMultibase('not-hex'), /64-char lower-hex/);
75
+ assert.throws(() => publicKeyMultibase(publicHex.slice(0, 63)), /64-char/);
76
+ assert.throws(() => secretKeyMultibase(''), /64-char/);
77
+ assert.throws(() => secretKeyMultibase(publicHex.toUpperCase()), /64-char lower-hex/);
78
+ });
79
+ });
80
+
81
+ describe('buildOwnerKeyDocument', () => {
82
+ const args = {
83
+ controllerWebId: 'https://alice.example/profile/card.jsonld#me',
84
+ publicHex: '87a1c6f0e9b3d2456789abcdef0123456789abcdef0123456789abcdef012345',
85
+ secretHex: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
86
+ };
87
+
88
+ it('emits a W3C CID v1.0 Multikey JSON-LD document', () => {
89
+ const doc = buildOwnerKeyDocument(args);
90
+ assert.strictEqual(doc['@context'], 'https://www.w3.org/ns/cid/v1');
91
+ assert.strictEqual(doc.type, 'Multikey');
92
+ });
93
+
94
+ it('uses the WebID as controller in Phase 1 (not did:nostr)', () => {
95
+ // Phase 1 keeps the document self-consistent. did:nostr controllers
96
+ // require the Phase 2 resolver to be useful; until then a CID
97
+ // consumer would dereference an unresolvable URI.
98
+ const doc = buildOwnerKeyDocument(args);
99
+ assert.strictEqual(doc.controller, args.controllerWebId);
100
+ assert.doesNotMatch(doc.controller, /^did:nostr:/);
101
+ });
102
+
103
+ it('embeds both the public and secret Multikey values', () => {
104
+ const doc = buildOwnerKeyDocument(args);
105
+ assert.strictEqual(doc.publicKeyMultibase, publicKeyMultibase(args.publicHex));
106
+ assert.strictEqual(doc.secretKeyMultibase, secretKeyMultibase(args.secretHex));
107
+ });
108
+
109
+ it('does NOT include a `nostr` extension block (npub etc.)', () => {
110
+ // Resolution #2 of #437: bech32 npub is derivable from the public
111
+ // multibase value in any Nostr-aware tool. Including it would
112
+ // double the secret-on-disk surface for log/error/dump leaks
113
+ // (the symmetric `nsec` field was originally proposed alongside).
114
+ const doc = buildOwnerKeyDocument(args);
115
+ assert.strictEqual(doc.nostr, undefined);
116
+ });
117
+
118
+ it('rejects a missing or empty controller WebID', () => {
119
+ assert.throws(() => buildOwnerKeyDocument({ ...args, controllerWebId: '' }), /controllerWebId required/);
120
+ assert.throws(() => buildOwnerKeyDocument({ ...args, controllerWebId: undefined }), /controllerWebId required/);
121
+ });
122
+ });
123
+
124
+ describe('provisionOwnerKey', () => {
125
+ it('returns the document together with raw key material for CLI display', () => {
126
+ const out = provisionOwnerKey({ controllerWebId: 'https://alice.example/profile/card.jsonld#me' });
127
+ assert.match(out.publicHex, /^[0-9a-f]{64}$/);
128
+ assert.match(out.secretHex, /^[0-9a-f]{64}$/);
129
+ assert.strictEqual(out.publicMultibase, out.document.publicKeyMultibase);
130
+ assert.strictEqual(out.document.type, 'Multikey');
131
+ assert.strictEqual(out.document.controller, 'https://alice.example/profile/card.jsonld#me');
132
+ });
133
+ });
134
+
135
+ describe('assertProvisionKeysCompatible', () => {
136
+ // Refuse the provisionKeys + --public combination — public mode
137
+ // bypasses WAC, which would expose the freshly-written secret on
138
+ // /private/privkey.jsonld to anyone over HTTP.
139
+ it('throws when both provisionKeys and isPublic are true', () => {
140
+ assert.throws(
141
+ () => assertProvisionKeysCompatible({ provisionKeys: true, isPublic: true }),
142
+ /cannot be combined with --public/
143
+ );
144
+ });
145
+
146
+ it('does NOT throw for provisionKeys alone (the supported case)', () => {
147
+ assert.doesNotThrow(() =>
148
+ assertProvisionKeysCompatible({ provisionKeys: true, isPublic: false })
149
+ );
150
+ });
151
+
152
+ it('does NOT throw for --public alone (no secrets being written)', () => {
153
+ assert.doesNotThrow(() =>
154
+ assertProvisionKeysCompatible({ provisionKeys: false, isPublic: true })
155
+ );
156
+ });
157
+
158
+ it('does NOT throw when neither flag is set', () => {
159
+ assert.doesNotThrow(() =>
160
+ assertProvisionKeysCompatible({ provisionKeys: false, isPublic: false })
161
+ );
162
+ });
163
+ });
164
+
165
+ function hexToBytes(hex) {
166
+ if (hex.length % 2 !== 0) throw new Error('hex length must be even');
167
+ const out = new Uint8Array(hex.length / 2);
168
+ for (let i = 0; i < out.length; i++) {
169
+ out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
170
+ }
171
+ return out;
172
+ }
@@ -1,12 +1,18 @@
1
1
  /**
2
- * Server-root landing page seed (#276).
2
+ * Server-root landing page seed (#276 / #433 / #435).
3
+ *
4
+ * Phase 3 (#433) seeded a mode-specific landing page that went stale on
5
+ * mode change. Phase 3 refinement (#435) replaced the mode-specific copy
6
+ * with a single mode-agnostic page that adapts at load time via a HEAD
7
+ * probe against /idp/register, so the same seeded HTML keeps working
8
+ * across modes without regenerating the file.
3
9
  */
4
10
 
5
11
  import { describe, it, before, after } from 'node:test';
6
12
  import assert from 'node:assert';
7
13
  import fs from 'fs-extra';
8
14
  import { createServer } from '../src/server.js';
9
- import { renderServerRoot } from '../src/ui/server-root.js';
15
+ import { renderServerRoot, decideRevealForRegisterStatus } from '../src/ui/server-root.js';
10
16
  import { startTestServer, stopTestServer, request, assertStatus } from './helpers.js';
11
17
 
12
18
  describe('Server-root landing page', () => {
@@ -22,8 +28,8 @@ describe('Server-root landing page', () => {
22
28
  const res = await request('/', { headers: { Accept: 'text/html' } });
23
29
  assertStatus(res, 200);
24
30
  const body = await res.text();
25
- assert.match(body, /<title>JSS<\/title>/);
26
- assert.match(body, /A personal data server/);
31
+ assert.match(body, /<title>JSS Solid pod<\/title>/);
32
+ assert.match(body, /Your JSS Solid pod is running/);
27
33
  });
28
34
 
29
35
  it('landing page is publicly readable (no auth required)', async () => {
@@ -97,96 +103,147 @@ describe('Server-root landing — operator override', () => {
97
103
  });
98
104
  });
99
105
 
100
- describe('renderServerRoot — mode-specific output', () => {
101
- it('multi-user + IDP shows Create a pod + Sign in', () => {
102
- const html = renderServerRoot({ version: '1.0.0', singleUser: false, idp: true });
103
- assert.match(html, /Create a pod/);
104
- assert.match(html, /href="\/idp\/register"/);
105
- assert.match(html, /href="\/idp"/);
106
+ describe('renderServerRoot', () => {
107
+ // The seeded HTML is fully static — no template substitution, no
108
+ // values vary by request. This is deliberate: anything dynamic
109
+ // (mode, features, version) goes stale on the next mode change or
110
+ // upgrade because the seed is skip-if-exists. Static = honest.
111
+ it('renders byte-identical HTML regardless of the ctx passed', () => {
112
+ const a = renderServerRoot({ version: '1.0.0', singleUser: true, enabled: { idp: true } });
113
+ const b = renderServerRoot({ version: '99.0.0', singleUser: false, enabled: {} });
114
+ const c = renderServerRoot();
115
+ assert.strictEqual(a, b);
116
+ assert.strictEqual(b, c);
117
+ assert.match(a, /<h1>Welcome<\/h1>/);
118
+ assert.match(a, /Your JSS Solid pod is running/);
119
+ assert.match(a, /open standard for personal data/);
120
+ });
121
+
122
+ it('does not bake mode, feature, or version pills into the seeded HTML', () => {
123
+ // All three would go stale across mode changes / upgrades because
124
+ // the seed is skip-if-exists. The CLI banner already lists them
125
+ // at startup. Excluded from the seed entirely.
126
+ const html = renderServerRoot({ version: '1.2.3', singleUser: true, enabled: { idp: true, nostr: true } });
127
+ assert.doesNotMatch(html, /<code>single-user<\/code>/);
128
+ assert.doesNotMatch(html, /<span>idp<\/span>/);
129
+ assert.doesNotMatch(html, /<span>nostr<\/span>/);
130
+ assert.doesNotMatch(html, /<code>1\.2\.3<\/code>/);
131
+ // No info box at all — there's nothing left to put in it.
132
+ assert.doesNotMatch(html, /class="info"/);
133
+ });
134
+
135
+ it('always emits the Get started button pointing at the docs First Run page', () => {
136
+ const html = renderServerRoot({ version: '1.0.0' });
137
+ // First Run is the friendly "you just installed it, now what?"
138
+ // walkthrough — better destination for a first-time installer than
139
+ // the encyclopedic Introduction page. See #440.
140
+ assert.match(html, /href="https:\/\/jss\.live\/docs\/getting-started\/first-run"/);
141
+ assert.match(html, /Get started/);
142
+ });
143
+
144
+ it('emits Sign up + Sign in buttons hidden for the HEAD probe to reveal, with page-relative hrefs', () => {
145
+ const html = renderServerRoot({ version: '1.0.0' });
146
+ // Page-relative (./idp/...) so a reverse-proxy mount under a path
147
+ // prefix sends visitors into the correct prefix instead of the
148
+ // origin root. Same rationale as the ./ ACL targets from #428.
149
+ assert.match(html, /<a href="\.\/idp\/register"[^>]*data-cond="register"[^>]*hidden/);
150
+ assert.match(html, /<a href="\.\/idp"[^>]*data-cond="login"[^>]*hidden/);
151
+ assert.doesNotMatch(html, /<a href="\/idp\/register"/,
152
+ 'Sign up href must be page-relative for path-prefix portability');
153
+ assert.doesNotMatch(html, /<a href="\/idp"/,
154
+ 'Sign in href must be page-relative for path-prefix portability');
155
+ assert.match(html, /Sign up/);
106
156
  assert.match(html, /Sign in/);
107
157
  });
108
158
 
109
- it('single-user + IDP shows Sign in only (no Create a pod)', () => {
110
- const html = renderServerRoot({ version: '1.0.0', singleUser: true, idp: true, singleUserName: 'alice' });
111
- assert.doesNotMatch(html, /Create a pod/);
112
- assert.match(html, /Sign in/);
113
- });
114
-
115
- it('multi-user without IDP shows only the Docs link', () => {
116
- const html = renderServerRoot({ version: '1.0.0', singleUser: false, idp: false });
117
- assert.doesNotMatch(html, /Create a pod/);
118
- assert.doesNotMatch(html, /Sign in/);
119
- assert.match(html, /Docs/);
120
- });
121
-
122
- it('single-user subtitle includes the pod name when provided', () => {
123
- const html = renderServerRoot({ version: '1.0.0', singleUser: true, idp: false, singleUserName: 'alice' });
124
- assert.match(html, /Personal pod for alice/);
159
+ it('overrides the .btn display rule for the [hidden] attribute so the buttons actually start hidden', () => {
160
+ // Without an explicit !important [hidden] rule, the .btn class's
161
+ // display:inline-block beats the UA stylesheet's [hidden]{display:none}
162
+ // and the Sign up / Sign in anchors flash visible before the HEAD probe
163
+ // finishes. The CSS rule is the load-bearing piece; assert it's there.
164
+ const html = renderServerRoot({ version: '1.0.0' });
165
+ assert.match(html, /\[hidden\]\s*\{[^}]*display:\s*none\s*!important/);
166
+ });
167
+
168
+ it('includes the HEAD-adaptive script targeting ./idp/register (page-relative)', () => {
169
+ const html = renderServerRoot({ version: '1.0.0' });
170
+ // Same path-prefix portability concern as the anchor hrefs.
171
+ assert.match(html, /fetch\(['"]\.\/idp\/register['"]/);
172
+ assert.doesNotMatch(html, /fetch\(['"]\/idp\/register['"]/,
173
+ 'HEAD probe URL must be page-relative for path-prefix portability');
174
+ assert.match(html, /method:\s*['"]HEAD['"]/);
175
+ assert.match(html, /res\.status === 200/);
176
+ assert.match(html, /res\.status === 403/);
177
+ });
178
+
179
+ it('includes the live-URL script that resolves the document base, not just origin', () => {
180
+ const html = renderServerRoot({ version: '1.0.0' });
181
+ assert.match(html, /id="server-url"/);
182
+ // Must use new URL('./', window.location.href) so a reverse-proxy
183
+ // mount at a path prefix is preserved. window.location.origin alone
184
+ // would drop the prefix and display the proxy origin instead.
185
+ assert.match(html, /new URL\(['"]\.\/['"],\s*window\.location\.href\)/);
186
+ assert.doesNotMatch(html, /textContent\s*=\s*window\.location\.origin/,
187
+ 'live-URL must not display origin-only — it would drop the path prefix on reverse-proxy mounts');
188
+ });
189
+
190
+ it('uses a page-relative fallback href on the live-URL anchor', () => {
191
+ // Before the script runs (or if scripts are blocked / blocked by CSP),
192
+ // the anchor is still clickable. A href="/" fallback would escape
193
+ // any reverse-proxy path prefix; use href="./" so the link stays
194
+ // inside the mount.
195
+ const html = renderServerRoot({ version: '1.0.0' });
196
+ assert.match(html, /<a id="server-url" href="\.\/"/);
197
+ assert.doesNotMatch(html, /<a id="server-url" href="\/"/,
198
+ 'fallback href must be page-relative for path-prefix portability');
199
+ });
200
+
201
+ it('points the footer at the GitHub repo and the customise hint', () => {
202
+ const html = renderServerRoot({ version: '1.0.0' });
203
+ assert.match(html, /href="https:\/\/github\.com\/JavaScriptSolidServer\/JavaScriptSolidServer"/);
204
+ assert.match(html, /Customise this page/);
205
+ assert.match(html, /<code>\/index\.html<\/code>/);
125
206
  });
207
+ });
126
208
 
127
- it('lists enabled features', () => {
128
- const html = renderServerRoot({
129
- version: '1.0.0',
130
- singleUser: false,
131
- idp: true,
132
- enabled: { idp: true, nostr: true, webrtc: true, terminal: true }
133
- });
134
- assert.match(html, /<span>idp<\/span>/);
135
- assert.match(html, /<span>nostr<\/span>/);
136
- assert.match(html, /<span>webrtc<\/span>/);
137
- assert.match(html, /<span>terminal<\/span>/);
138
- });
139
-
140
- it('interpolates version', () => {
141
- const html = renderServerRoot({ version: '9.9.9' });
142
- assert.match(html, /<code>9\.9\.9<\/code>/);
143
- });
144
-
145
- it('escapes version to prevent injection', () => {
146
- const html = renderServerRoot({ version: '<script>alert(1)</script>' });
147
- assert.doesNotMatch(html, /<script>alert\(1\)<\/script>/);
148
- assert.match(html, /&lt;script&gt;/);
149
- });
150
-
151
- // Regression for token re-scanning (#433 review thread): if the
152
- // renderer ran a chain of sequential .replace() calls, a value
153
- // containing a literal `{{actions}}` would land inside the subtitle
154
- // and then get expanded by the later `.replace(/{{actions}}/g, ...)`,
155
- // letting any pod owner inject other template fragments via their
156
- // singleUserName. The single-pass substitution prevents that.
157
- it('does not re-scan substituted values for further template tokens', () => {
158
- const html = renderServerRoot({
159
- version: '1.0.0',
160
- singleUser: true,
161
- idp: false,
162
- // The HTML escape only touches & < > " — { } pass through, so the
163
- // token would land in the output verbatim if the substitution were
164
- // multi-pass.
165
- singleUserName: 'evil{{actions}}name'
166
- });
167
- assert.match(html, /Personal pod for evil\{\{actions\}\}name/,
168
- 'singleUserName containing a template token should appear as plain text, not be re-templated');
169
- // Sanity: the real {{actions}} slot is still resolved (Docs link is always present).
170
- assert.match(html, /href="https:\/\/javascriptsolidserver\.github\.io\/docs/);
171
- });
172
-
173
- // Regression for the `$&` substitution gotcha (#433): a string used as
174
- // the second argument of String.prototype.replace interprets `$&`,
175
- // `$1`, etc. as substitution patterns. Interpolated values can contain
176
- // `$` (notably a singleUserName), so the renderer uses the function
177
- // form of replace instead. Asserting the literal `$&` survives the
178
- // round-trip would mean it survived as plain text.
179
- it('preserves $-patterns in singleUserName instead of treating them as replacement specials', () => {
180
- const html = renderServerRoot({
181
- version: '1.0.0',
182
- singleUser: true,
183
- idp: false,
184
- singleUserName: 'foo$&bar'
185
- });
186
- // The HTML escape converts `&` to `&amp;`; the rest must stay verbatim,
187
- // not be replaced by the matched template token.
188
- assert.match(html, /Personal pod for foo\$&amp;bar/,
189
- 'singleUserName containing "$&" should land as-is, not trigger String.replace substitution');
190
- assert.doesNotMatch(html, /\{\{subtitle\}\}/, 'subtitle token should be fully consumed');
209
+ // Pure-function unit tests for the HEAD response → button-reveal matrix.
210
+ // The inline script in server-root.html implements the same matrix by
211
+ // hand; a regex check on the script text (above) catches outright drops
212
+ // of the literals, but only this helper test pins down the *behaviour*
213
+ // of the matrix without needing a DOM.
214
+ describe('decideRevealForRegisterStatus', () => {
215
+ it('reveals both Sign up and Sign in for HTTP 200 (registration open)', () => {
216
+ assert.deepStrictEqual(
217
+ decideRevealForRegisterStatus(200),
218
+ { register: true, login: true }
219
+ );
220
+ });
221
+
222
+ it('reveals only Sign in for HTTP 403 (single-user — registration disabled)', () => {
223
+ assert.deepStrictEqual(
224
+ decideRevealForRegisterStatus(403),
225
+ { register: false, login: true }
226
+ );
227
+ });
228
+
229
+ it('reveals neither for HTTP 404 (no IDP)', () => {
230
+ assert.deepStrictEqual(
231
+ decideRevealForRegisterStatus(404),
232
+ { register: false, login: false }
233
+ );
234
+ });
235
+
236
+ it('reveals neither for any other status (e.g. 500)', () => {
237
+ assert.deepStrictEqual(
238
+ decideRevealForRegisterStatus(500),
239
+ { register: false, login: false }
240
+ );
241
+ });
242
+
243
+ it('reveals neither when status is undefined (network error)', () => {
244
+ assert.deepStrictEqual(
245
+ decideRevealForRegisterStatus(undefined),
246
+ { register: false, login: false }
247
+ );
191
248
  });
192
249
  });