javascript-solid-server 0.0.191 → 0.0.192
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/package.json +5 -4
- package/src/idp/export.js +547 -0
- package/src/idp/index.js +42 -1
- package/src/server.js +23 -1
- package/test/idp-export.test.js +429 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "javascript-solid-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.192",
|
|
4
4
|
"description": "A minimal, fast Solid server",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -24,11 +24,11 @@
|
|
|
24
24
|
"benchmark": "node benchmark.js"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@noble/curves": "^1.2.0",
|
|
28
|
-
"@noble/hashes": "^1.3.2",
|
|
29
27
|
"@fastify/middie": "^8.3.3",
|
|
30
28
|
"@fastify/rate-limit": "^9.1.0",
|
|
31
29
|
"@fastify/websocket": "^8.3.1",
|
|
30
|
+
"@noble/curves": "^1.2.0",
|
|
31
|
+
"@noble/hashes": "^1.3.2",
|
|
32
32
|
"@simplewebauthn/server": "^13.2.2",
|
|
33
33
|
"bcryptjs": "^3.0.3",
|
|
34
34
|
"commander": "^14.0.2",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"microfed": "^0.0.14",
|
|
39
39
|
"n3": "^1.26.0",
|
|
40
40
|
"oidc-provider": "^9.6.0",
|
|
41
|
-
"sql.js": "^1.13.0"
|
|
41
|
+
"sql.js": "^1.13.0",
|
|
42
|
+
"tar-stream": "^3.2.0"
|
|
42
43
|
},
|
|
43
44
|
"engines": {
|
|
44
45
|
"node": ">=18.0.0"
|
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET /idp/account/export — self-service pod data download (#353).
|
|
3
|
+
*
|
|
4
|
+
* The export side of the user-rights trio (#351 password change,
|
|
5
|
+
* #352 account delete, this). Authenticated owner downloads a
|
|
6
|
+
* streamed tar.gz of their pod tree + a manifest. End users walk
|
|
7
|
+
* away with what they own without operator help and without
|
|
8
|
+
* shell access.
|
|
9
|
+
*
|
|
10
|
+
* Per the Credible Exit framing (#448), the archive intentionally
|
|
11
|
+
* INCLUDES `/private/privkey.jsonld` when the pod was provisioned
|
|
12
|
+
* with `--provision-keys`. The user's secret IS theirs and must
|
|
13
|
+
* leave with them — refusing would make L4+ identity migration
|
|
14
|
+
* impossible. The endpoint is owner-authenticated; the secret
|
|
15
|
+
* never leaves the WAC perimeter to anyone but the owner.
|
|
16
|
+
*
|
|
17
|
+
* Streaming pipeline: tar.pack → zlib.createGzip → reply. Memory
|
|
18
|
+
* stays constant regardless of pod size; a multi-GB pod doesn't
|
|
19
|
+
* OOM the server.
|
|
20
|
+
*
|
|
21
|
+
* Failure modes:
|
|
22
|
+
* 401 — unauthenticated
|
|
23
|
+
* 403 — caller's WebID has no matching local account record;
|
|
24
|
+
* applies to multi-user (no account for the WebID) and
|
|
25
|
+
* single-user (authenticated WebID is not the seeded owner —
|
|
26
|
+
* e.g. an external Solid-OIDC / LWS-CID identity)
|
|
27
|
+
* 404 — pod directory unexpectedly missing (shouldn't happen for
|
|
28
|
+
* an account with a valid WebID, but caught defensively)
|
|
29
|
+
* 500 — defense-in-depth: podDir resolved to a server-internal
|
|
30
|
+
* name (`.idp` / `.private` / etc.) in non-root-pod mode,
|
|
31
|
+
* meaning account-creation validation has regressed and
|
|
32
|
+
* allowed a reserved username through
|
|
33
|
+
*
|
|
34
|
+
* Cross-account access is structurally impossible: the endpoint
|
|
35
|
+
* takes no target parameter and always scopes to the caller's
|
|
36
|
+
* authenticated WebID. There's no `403 cross-account` failure mode
|
|
37
|
+
* because there's no path to attempt the access in the first place.
|
|
38
|
+
*
|
|
39
|
+
* Out of scope: re-import, cross-server pod migration, periodic
|
|
40
|
+
* scheduled backups, partial / per-resource selection. See #353.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import path from 'path';
|
|
44
|
+
import { promises as fsp, constants as fsConstants } from 'fs';
|
|
45
|
+
import crypto from 'crypto';
|
|
46
|
+
import zlib from 'zlib';
|
|
47
|
+
import tar from 'tar-stream';
|
|
48
|
+
import { getWebIdFromRequestAsync } from '../auth/token.js';
|
|
49
|
+
import { findByWebId } from './accounts.js';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Entries at the data root that are server-internal, not pod data.
|
|
53
|
+
* In single-user *root* pod mode, podDir IS dataRoot — packing it
|
|
54
|
+
* naively would ship server-managed material to the caller. Skip:
|
|
55
|
+
*
|
|
56
|
+
* .idp/ — IDP accounts (passwordHash for every user!) +
|
|
57
|
+
* signing keys (mint tokens for any user) +
|
|
58
|
+
* OIDC adapter state (sessions, refresh tokens)
|
|
59
|
+
* .private/ — pay/Bitcoin keypair + UTXO state (drainable)
|
|
60
|
+
*
|
|
61
|
+
* The pod's *own* /private/ folder (no leading dot) lives at
|
|
62
|
+
* <dataRoot>/private/ in root-pod mode and IS pod data — operator
|
|
63
|
+
* key, etc. — and is INCLUDED.
|
|
64
|
+
*
|
|
65
|
+
* Public namespaces under .well-known/ (webledgers, openid-config,
|
|
66
|
+
* etc.) are reachable by any HTTP client by spec, so they're
|
|
67
|
+
* "pod data" in the sense that they're part of the pod's public
|
|
68
|
+
* surface — included.
|
|
69
|
+
*
|
|
70
|
+
* Named-pod single-user (podDir = <dataRoot>/<name>/) and multi-user
|
|
71
|
+
* (podDir = <dataRoot>/<podName>/) don't hit this code path — the
|
|
72
|
+
* pod tree is already isolated by the path layout.
|
|
73
|
+
*
|
|
74
|
+
* !!! SECURITY-CRITICAL — DO NOT ADD A NEW SERVER-INTERNAL TOP-LEVEL
|
|
75
|
+
* DIRECTORY WITHOUT ALSO ADDING IT HERE AND ADDING A REGRESSION TEST.
|
|
76
|
+
*
|
|
77
|
+
* We use a denylist (not an allowlist of pod-data subdirs) because
|
|
78
|
+
* pod content is open-ended — operators and apps create arbitrary
|
|
79
|
+
* top-level containers, and an allowlist would break Credible Exit
|
|
80
|
+
* by silently dropping legitimate user data. The trade-off: any new
|
|
81
|
+
* server-managed dotfile dir landing at the data root must be added
|
|
82
|
+
* here in the same PR that introduces it. The denylist test in
|
|
83
|
+
* test/idp-export.test.js asserts on the property "no IdP secrets
|
|
84
|
+
* appear in the archive" against on-disk seeded files, so it will
|
|
85
|
+
* regress loudly if a future feature drops a secret-bearing dir at
|
|
86
|
+
* the data root and forgets to update this set.
|
|
87
|
+
*/
|
|
88
|
+
const ROOT_POD_EXCLUDE = new Set(['.idp', '.private']);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Allowlist of account record fields that are safe to include in
|
|
92
|
+
* `account.json`. Defensive: a denylist that strips only
|
|
93
|
+
* `passwordHash` would silently leak any future secret-bearing
|
|
94
|
+
* field added to the account schema (passkey credential records,
|
|
95
|
+
* OIDC client secrets, recovery tokens, etc.).
|
|
96
|
+
*
|
|
97
|
+
* Adding a new field here requires a security review. Passkey
|
|
98
|
+
* credentials are intentionally NOT included — they're device-
|
|
99
|
+
* bound and not portable to a fresh server.
|
|
100
|
+
*
|
|
101
|
+
* !!! TOP-LEVEL ONLY. The allowlist gates only at the first level
|
|
102
|
+
* of the account record. If a future field is itself an
|
|
103
|
+
* object/array (e.g. `oidcClientConfig: { secret: ... }`,
|
|
104
|
+
* `metadata: { recoveryAnswers: [...] }`, etc.), allowlisting the
|
|
105
|
+
* top-level key alone exports the entire nested structure
|
|
106
|
+
* including any secrets. Nested structures need their own scrubbing
|
|
107
|
+
* before being added here, OR they should be projected into a flat
|
|
108
|
+
* shape that excludes the secret-bearing fields.
|
|
109
|
+
*/
|
|
110
|
+
const ACCOUNT_EXPORT_FIELDS = [
|
|
111
|
+
'id', 'webId', 'username', 'email', 'podName', 'createdAt', 'updatedAt',
|
|
112
|
+
'passwordChangedAt', 'lastLoginAt',
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Error vocabulary note: 401 uses the OAuth-Bearer-defined
|
|
117
|
+
* `invalid_token` (RFC 6750), while 403/404 use plain HTTP-semantic
|
|
118
|
+
* tokens (`forbidden`, `not_found`). This mixed convention is
|
|
119
|
+
* deliberate and consistent with the rest of `src/idp/` (see
|
|
120
|
+
* credentials.js: 401→`invalid_token`/`invalid_grant`, 403→
|
|
121
|
+
* `forbidden`, 400→`invalid_request`). Aligning to a single
|
|
122
|
+
* vocabulary across the whole IdP surface is its own refactor.
|
|
123
|
+
*
|
|
124
|
+
* @param {object} request - Fastify request
|
|
125
|
+
* @param {object} reply - Fastify reply
|
|
126
|
+
* @param {object} options
|
|
127
|
+
* @param {boolean} [options.singleUser]
|
|
128
|
+
* @param {string|null} [options.singleUserName] - null for root pod,
|
|
129
|
+
* string for /<name>/ pod
|
|
130
|
+
* @param {string} [options.jssVersion] - written into the manifest
|
|
131
|
+
* for forensic / "what server made this" purposes
|
|
132
|
+
*/
|
|
133
|
+
export async function handleExportAccount(request, reply, options = {}) {
|
|
134
|
+
// 1. Authenticate caller. Same path as DELETE /idp/account etc.;
|
|
135
|
+
// works with bearer tokens, LWS-CID JWTs, etc. — anything
|
|
136
|
+
// getWebIdFromRequestAsync resolves to a WebID.
|
|
137
|
+
const { webId, error: authError } = await getWebIdFromRequestAsync(request);
|
|
138
|
+
if (!webId) {
|
|
139
|
+
return reply.code(401).send({
|
|
140
|
+
error: 'invalid_token',
|
|
141
|
+
error_description: authError || 'Authentication required',
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 2. Resolve the pod tree on disk + the manifest data.
|
|
146
|
+
const dataRoot = process.env.DATA_ROOT || './data';
|
|
147
|
+
let podDir;
|
|
148
|
+
let accountRecord = null;
|
|
149
|
+
let manifest;
|
|
150
|
+
let isRootPod = false;
|
|
151
|
+
|
|
152
|
+
if (options.singleUser) {
|
|
153
|
+
// Single-user: pod is at `/` (root pod) or `/<name>/` based on
|
|
154
|
+
// singleUserName. The seeded IDP account (per
|
|
155
|
+
// seedSingleUserIdpAccount in src/server.js) is the sole owner —
|
|
156
|
+
// an authenticated WebID without a matching local account is
|
|
157
|
+
// some third-party identity (external Solid-OIDC, LWS-CID JWT,
|
|
158
|
+
// etc.), NOT the pod owner, and must not get the operator's
|
|
159
|
+
// /private/privkey.jsonld. The route is only mounted when
|
|
160
|
+
// idpEnabled, so a missing account record means "caller is not
|
|
161
|
+
// the seeded owner" — refuse with the same 403 shape as
|
|
162
|
+
// multi-user.
|
|
163
|
+
//
|
|
164
|
+
// Refuse empty-string / non-string singleUserName explicitly.
|
|
165
|
+
// The previous `!singleUserName` falsy-check silently mapped
|
|
166
|
+
// both '' and null to root-pod, but '' is almost certainly a
|
|
167
|
+
// misconfiguration — an operator who meant root-pod omits the
|
|
168
|
+
// option entirely, and an operator who meant a named pod
|
|
169
|
+
// wouldn't use empty string. WORSE, with `!''`=true the old
|
|
170
|
+
// code took the root-pod branch and applied ROOT_POD_EXCLUDE;
|
|
171
|
+
// a strict null check WITHOUT this refusal would take the
|
|
172
|
+
// named-pod branch with podDir = path.join(dataRoot, '') =
|
|
173
|
+
// dataRoot AND excludeAtRoot = null, silently exporting
|
|
174
|
+
// server-internal `.idp/` and `.private/`. So we explicitly
|
|
175
|
+
// reject the empty-string / non-string case before deciding
|
|
176
|
+
// isRootPod.
|
|
177
|
+
if (
|
|
178
|
+
options.singleUserName !== null &&
|
|
179
|
+
options.singleUserName !== undefined &&
|
|
180
|
+
(typeof options.singleUserName !== 'string' || options.singleUserName.length === 0)
|
|
181
|
+
) {
|
|
182
|
+
request.log.error(
|
|
183
|
+
{ singleUserName: options.singleUserName },
|
|
184
|
+
'pod export refused — singleUserName must be null/undefined or a non-empty string',
|
|
185
|
+
);
|
|
186
|
+
return reply.code(500).send({
|
|
187
|
+
error: 'server_error',
|
|
188
|
+
error_description: 'Invalid singleUserName configuration',
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
isRootPod = options.singleUserName == null;
|
|
192
|
+
podDir = isRootPod
|
|
193
|
+
? dataRoot
|
|
194
|
+
: path.join(dataRoot, options.singleUserName);
|
|
195
|
+
|
|
196
|
+
accountRecord = await findByWebId(webId);
|
|
197
|
+
if (!accountRecord) {
|
|
198
|
+
return reply.code(403).send({
|
|
199
|
+
error: 'forbidden',
|
|
200
|
+
error_description:
|
|
201
|
+
'Authenticated WebID does not match the single-user account',
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
// Defense-in-depth: in named-pod single-user mode, the seeded
|
|
205
|
+
// accountRecord.podName MUST equal options.singleUserName (the
|
|
206
|
+
// CLI option that derives podDir). If seedSingleUserIdpAccount
|
|
207
|
+
// ever drifted, manifest.podName (read from accountRecord) would
|
|
208
|
+
// advertise X while the pod tree is read from <dataRoot>/Y.
|
|
209
|
+
// Refuse with 500 so a seeding regression fails loudly rather
|
|
210
|
+
// than producing a silently mismatched archive.
|
|
211
|
+
//
|
|
212
|
+
// Skipped in root-pod mode where podDir is dataRoot regardless
|
|
213
|
+
// of the seeded podName (which is intentionally 'me' for OIDC
|
|
214
|
+
// identity, not a path component).
|
|
215
|
+
if (!isRootPod && accountRecord.podName !== options.singleUserName) {
|
|
216
|
+
request.log.error(
|
|
217
|
+
{
|
|
218
|
+
seededPodName: accountRecord.podName,
|
|
219
|
+
cliSingleUserName: options.singleUserName,
|
|
220
|
+
},
|
|
221
|
+
'pod export refused — accountRecord.podName disagrees with ' +
|
|
222
|
+
'singleUserName; seedSingleUserIdpAccount regression?',
|
|
223
|
+
);
|
|
224
|
+
return reply.code(500).send({
|
|
225
|
+
error: 'server_error',
|
|
226
|
+
error_description: 'Pod identity inconsistent with on-disk layout',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
// manifest.podName mirrors the IDP account's podName (the OIDC
|
|
230
|
+
// short name) for parity with the multi-user branch — both
|
|
231
|
+
// branches now read podName from accountRecord, so a downstream
|
|
232
|
+
// importer keying on manifest.podName or account.json.podName
|
|
233
|
+
// gets the same answer regardless of server mode. Filesystem
|
|
234
|
+
// layout (root-pod vs /<name>/ pod) is conveyed by `mode` +
|
|
235
|
+
// the seeded podName ('me' for root-pod, singleUserName otherwise).
|
|
236
|
+
manifest = {
|
|
237
|
+
webId: accountRecord.webId,
|
|
238
|
+
username: accountRecord.username,
|
|
239
|
+
email: accountRecord.email,
|
|
240
|
+
podName: accountRecord.podName,
|
|
241
|
+
mode: 'single-user',
|
|
242
|
+
createdAt: accountRecord.createdAt,
|
|
243
|
+
exportedAt: new Date().toISOString(),
|
|
244
|
+
jssVersion: options.jssVersion ?? 'unknown',
|
|
245
|
+
};
|
|
246
|
+
} else {
|
|
247
|
+
// Multi-user: caller's WebID must resolve to an account record
|
|
248
|
+
// on this server. Same 403 shape as DELETE /idp/account uses
|
|
249
|
+
// when an authenticated WebID has no local account.
|
|
250
|
+
accountRecord = await findByWebId(webId);
|
|
251
|
+
if (!accountRecord) {
|
|
252
|
+
return reply.code(403).send({
|
|
253
|
+
error: 'forbidden',
|
|
254
|
+
error_description: 'No account found for authenticated WebID',
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const podName = accountRecord.podName || accountRecord.username;
|
|
258
|
+
podDir = path.join(dataRoot, podName);
|
|
259
|
+
manifest = {
|
|
260
|
+
webId: accountRecord.webId,
|
|
261
|
+
username: accountRecord.username,
|
|
262
|
+
email: accountRecord.email,
|
|
263
|
+
podName,
|
|
264
|
+
mode: 'multi-user',
|
|
265
|
+
createdAt: accountRecord.createdAt,
|
|
266
|
+
exportedAt: new Date().toISOString(),
|
|
267
|
+
jssVersion: options.jssVersion ?? 'unknown',
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Defense-in-depth: in multi-user mode `excludeAtRoot` is null
|
|
272
|
+
// because the pod is at <dataRoot>/<podName>/ (already isolated
|
|
273
|
+
// from server-internal dotfiles by the path layout). But if
|
|
274
|
+
// account-creation validation ever regressed and allowed a
|
|
275
|
+
// username matching `.idp` / `.private`, podDir would resolve to
|
|
276
|
+
// the server-internal directory and the export would happily
|
|
277
|
+
// walk it. Refuse here — account creation rejecting these names
|
|
278
|
+
// is the primary defense; this is the second line.
|
|
279
|
+
if (
|
|
280
|
+
!(options.singleUser && isRootPod) &&
|
|
281
|
+
ROOT_POD_EXCLUDE.has(path.basename(podDir))
|
|
282
|
+
) {
|
|
283
|
+
request.log.error(
|
|
284
|
+
{ podDir, basename: path.basename(podDir) },
|
|
285
|
+
'export refused — podDir resolved to a server-internal name; ' +
|
|
286
|
+
'account-creation validation regression?',
|
|
287
|
+
);
|
|
288
|
+
return reply.code(500).send({
|
|
289
|
+
error: 'server_error',
|
|
290
|
+
error_description: 'Pod resolution conflicts with server-internal layout',
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Pre-flight the pod directory BEFORE flushing response headers.
|
|
295
|
+
// We do both checks (stat for existence/type, readdir for
|
|
296
|
+
// readability) up front so a first-byte EACCES on the top-level
|
|
297
|
+
// readdir doesn't surface inside the streaming pipeline AFTER
|
|
298
|
+
// reply.send(gzip) has already flushed headers — that would leave
|
|
299
|
+
// the client with a 200 + truncated/empty body instead of a clean
|
|
300
|
+
// 5xx JSON error.
|
|
301
|
+
//
|
|
302
|
+
// 404 vs 500 split:
|
|
303
|
+
// ENOENT/ENOTDIR → 404 (auth was fine, just no pod on disk)
|
|
304
|
+
// anything else (EACCES, EIO, etc.) → 500 (server-side problem)
|
|
305
|
+
//
|
|
306
|
+
// Generic error_descriptions on the wire — echoing the resolved
|
|
307
|
+
// podDir back would leak the operator's filesystem layout. Path
|
|
308
|
+
// stays in the server log via request.log.{warn,error} for
|
|
309
|
+
// operator debugging.
|
|
310
|
+
try {
|
|
311
|
+
const st = await fsp.stat(podDir);
|
|
312
|
+
if (!st.isDirectory()) {
|
|
313
|
+
const err = new Error('not a directory');
|
|
314
|
+
err.code = 'ENOTDIR';
|
|
315
|
+
throw err;
|
|
316
|
+
}
|
|
317
|
+
// Pre-flight readability with `fsp.access(R_OK | X_OK)` rather
|
|
318
|
+
// than a discarded readdir — same effect (catches EACCES on the
|
|
319
|
+
// pod directory) without scanning the whole top-level entry
|
|
320
|
+
// list twice. R_OK gates listing, X_OK gates traversal into
|
|
321
|
+
// subdirectories, both required by the recursive walk.
|
|
322
|
+
await fsp.access(podDir, fsConstants.R_OK | fsConstants.X_OK);
|
|
323
|
+
} catch (err) {
|
|
324
|
+
if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {
|
|
325
|
+
request.log.warn({ podDir }, 'pod export: podDir missing or not a directory');
|
|
326
|
+
return reply.code(404).send({
|
|
327
|
+
error: 'not_found',
|
|
328
|
+
error_description: 'Pod data not found',
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
request.log.error({ err, podDir }, 'pod export: pre-flight failed');
|
|
332
|
+
return reply.code(500).send({
|
|
333
|
+
error: 'server_error',
|
|
334
|
+
error_description: 'Pod data is unreadable',
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// 3. Set headers and start streaming.
|
|
339
|
+
const slug = sanitizeSlug(webId);
|
|
340
|
+
const isoDate = manifest.exportedAt.replace(/[:.]/g, '-');
|
|
341
|
+
// Short random suffix avoids collisions when a client batches
|
|
342
|
+
// exports — ISO timestamps have only millisecond resolution and
|
|
343
|
+
// two consecutive calls in the same ms would otherwise produce
|
|
344
|
+
// identical filenames that overwrite each other on the client.
|
|
345
|
+
const rand = crypto.randomBytes(3).toString('hex'); // 6 hex chars
|
|
346
|
+
const filename = `jss-export-${slug}-${isoDate}-${rand}.tar.gz`;
|
|
347
|
+
|
|
348
|
+
// sanitizeSlug() restricts the slug to [A-Za-z0-9._-] and isoDate
|
|
349
|
+
// is ISO-8601 with `:`/`.` replaced — both are strict-ASCII safe
|
|
350
|
+
// for the legacy `filename=` form. Also emit `filename*=UTF-8''…`
|
|
351
|
+
// (RFC 5987) so any future non-ASCII slip-through degrades to
|
|
352
|
+
// valid UTF-8 percent-encoding rather than a malformed header.
|
|
353
|
+
const cdValue =
|
|
354
|
+
`attachment; filename="${filename}"; ` +
|
|
355
|
+
`filename*=UTF-8''${encodeURIComponent(filename)}`;
|
|
356
|
+
reply
|
|
357
|
+
.type('application/x-tar+gzip')
|
|
358
|
+
.header('Content-Disposition', cdValue)
|
|
359
|
+
.header('Cache-Control', 'no-store');
|
|
360
|
+
|
|
361
|
+
const pack = tar.pack();
|
|
362
|
+
const gzip = zlib.createGzip();
|
|
363
|
+
pack.pipe(gzip);
|
|
364
|
+
|
|
365
|
+
// Surface stream-level errors. Once response headers are out an
|
|
366
|
+
// EACCES / mid-pack failure can otherwise present as a silently
|
|
367
|
+
// truncated download — the client gets 200 + partial gzip + no
|
|
368
|
+
// error signal. We log on the server side and destroy the
|
|
369
|
+
// pipeline so the client at least sees an aborted transfer rather
|
|
370
|
+
// than a corrupt but seemingly-complete archive.
|
|
371
|
+
// Idempotent: invoked from pack.error, gzip.error, AND the
|
|
372
|
+
// .catch(onStreamError) chained on packExport(...) below.
|
|
373
|
+
// Destroying a stream re-emits 'error', which would re-enter this
|
|
374
|
+
// handler and produce duplicate log lines for one underlying
|
|
375
|
+
// failure. The destroyed-flag short-circuits all subsequent calls
|
|
376
|
+
// so a single failure logs once.
|
|
377
|
+
const onStreamError = (err) => {
|
|
378
|
+
if (gzip.destroyed || pack.destroyed) return;
|
|
379
|
+
request.log.error({ err }, 'pod export stream error');
|
|
380
|
+
pack.destroy(err);
|
|
381
|
+
gzip.destroy(err);
|
|
382
|
+
};
|
|
383
|
+
pack.on('error', onStreamError);
|
|
384
|
+
gzip.on('error', onStreamError);
|
|
385
|
+
|
|
386
|
+
// Client disconnect handler. Without this, walkAndPack keeps
|
|
387
|
+
// reading every file in the pod, opening file descriptors, and
|
|
388
|
+
// pushing into a gzip whose downstream socket is gone. For a
|
|
389
|
+
// multi-GB pod that's wasted IO + fds until the walk finishes.
|
|
390
|
+
// Destroying both ends short-circuits the walk via stream error
|
|
391
|
+
// propagation; the request.log captures the abort for diagnostics.
|
|
392
|
+
//
|
|
393
|
+
// `responseFinished` closes a race window. We can't gate on
|
|
394
|
+
// `packExport`'s resolution: that fires when `pack.finalize()`
|
|
395
|
+
// returns, which is "we're done WRITING into the pipeline" — not
|
|
396
|
+
// "the client has received the bytes". For a multi-GB gzipped
|
|
397
|
+
// response over a slow link, the gap between those two events
|
|
398
|
+
// can be substantial, and a real client disconnect during the
|
|
399
|
+
// final flush would be silently swallowed (close handler
|
|
400
|
+
// short-circuits on a flag set too early). Gate instead on
|
|
401
|
+
// `reply.raw.on('finish')` — Node emits 'finish' when the last
|
|
402
|
+
// byte has been flushed to the OS socket buffer, AFTER which a
|
|
403
|
+
// 'close' event means "client disconnected after we were done"
|
|
404
|
+
// and is correctly ignored. Before 'finish', a 'close' is a
|
|
405
|
+
// genuine mid-stream disconnect and gets logged + cleans up.
|
|
406
|
+
let responseFinished = false;
|
|
407
|
+
reply.raw.on('finish', () => { responseFinished = true; });
|
|
408
|
+
reply.raw.on('close', () => {
|
|
409
|
+
if (responseFinished || reply.raw.writableEnded) return;
|
|
410
|
+
request.log.warn('pod export client disconnected mid-stream');
|
|
411
|
+
pack.destroy(new Error('client disconnected'));
|
|
412
|
+
gzip.destroy(new Error('client disconnected'));
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// Pump in the background; entries are added asynchronously below.
|
|
416
|
+
// The root-pod denylist is wired in so single-user-root-pod mode
|
|
417
|
+
// doesn't ship .idp/ (accounts + signing keys + OIDC state).
|
|
418
|
+
// `.catch(onStreamError)` attaches a rejection handler so there's
|
|
419
|
+
// no unhandled-rejection risk; the pipeline runs detached.
|
|
420
|
+
packExport({
|
|
421
|
+
pack,
|
|
422
|
+
podDir,
|
|
423
|
+
manifest,
|
|
424
|
+
accountRecord,
|
|
425
|
+
excludeAtRoot: (options.singleUser && isRootPod) ? ROOT_POD_EXCLUDE : null,
|
|
426
|
+
}).catch(onStreamError);
|
|
427
|
+
|
|
428
|
+
return reply.send(gzip);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async function packExport({ pack, podDir, manifest, accountRecord, excludeAtRoot }) {
|
|
432
|
+
// Manifest first so consumers can read the shape before deciding
|
|
433
|
+
// whether to keep streaming the (potentially large) pod tree.
|
|
434
|
+
await addEntry(pack, 'jss-export/manifest.json',
|
|
435
|
+
Buffer.from(JSON.stringify(manifest, null, 2), 'utf8'));
|
|
436
|
+
|
|
437
|
+
// Account record — allowlisted fields only. Both branches in
|
|
438
|
+
// handleExportAccount now refuse with 403 when accountRecord is
|
|
439
|
+
// null, so by the time we get here the record is always defined
|
|
440
|
+
// and account.json is always emitted.
|
|
441
|
+
const safeAccount = {};
|
|
442
|
+
for (const key of ACCOUNT_EXPORT_FIELDS) {
|
|
443
|
+
if (accountRecord[key] !== undefined) safeAccount[key] = accountRecord[key];
|
|
444
|
+
}
|
|
445
|
+
await addEntry(pack, 'jss-export/account.json',
|
|
446
|
+
Buffer.from(JSON.stringify(safeAccount, null, 2), 'utf8'));
|
|
447
|
+
|
|
448
|
+
// Pod tree. The first-level filter (`excludeAtRoot`) is what
|
|
449
|
+
// prevents single-user-root-pod mode from leaking .idp/.
|
|
450
|
+
await walkAndPack(pack, podDir, 'jss-export/pod', excludeAtRoot);
|
|
451
|
+
|
|
452
|
+
pack.finalize();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Recursively pack `dir` into `pack` under the tar prefix `tarBase`.
|
|
457
|
+
*
|
|
458
|
+
* @param {Set<string>|null} excludeAtRoot - first-level names to skip
|
|
459
|
+
* (applied only at the top of the walk; deeper entries pass freely).
|
|
460
|
+
* Set to `ROOT_POD_EXCLUDE` in single-user-root-pod mode where
|
|
461
|
+
* podDir is the data root and server-internal dotfiles live next
|
|
462
|
+
* to pod data.
|
|
463
|
+
*/
|
|
464
|
+
async function walkAndPack(pack, dir, tarBase, excludeAtRoot = null) {
|
|
465
|
+
const entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
466
|
+
for (const dirent of entries) {
|
|
467
|
+
if (excludeAtRoot && excludeAtRoot.has(dirent.name)) {
|
|
468
|
+
// Top-level server-internal dir — never appears in any export.
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
const fullPath = path.join(dir, dirent.name);
|
|
472
|
+
const tarPath = `${tarBase}/${dirent.name}`;
|
|
473
|
+
if (dirent.isDirectory()) {
|
|
474
|
+
// Emit an explicit directory entry so empty LDP containers
|
|
475
|
+
// (a container the operator provisioned but hasn't populated)
|
|
476
|
+
// survive a round-trip — preserves the pod's LDP shape on
|
|
477
|
+
// restore. tar requires the trailing slash on directory names.
|
|
478
|
+
await addDirEntry(pack, `${tarPath}/`);
|
|
479
|
+
// No `excludeAtRoot` on recursion — the denylist is first-level only.
|
|
480
|
+
await walkAndPack(pack, fullPath, tarPath, null);
|
|
481
|
+
} else if (dirent.isFile()) {
|
|
482
|
+
// Stream the file into the entry from an *open fd* — opens
|
|
483
|
+
// and stats off the same fd, so a concurrent truncate/grow
|
|
484
|
+
// can't desync `size` from the bytes actually piped. Without
|
|
485
|
+
// this, a stat-then-open dance would TOCTOU on a live pod
|
|
486
|
+
// and produce a corrupt tar that fails extraction.
|
|
487
|
+
const fh = await fsp.open(fullPath, 'r');
|
|
488
|
+
try {
|
|
489
|
+
const st = await fh.stat();
|
|
490
|
+
await new Promise((resolve, reject) => {
|
|
491
|
+
const entry = pack.entry(
|
|
492
|
+
{ name: tarPath, size: st.size, mode: st.mode & 0o777 },
|
|
493
|
+
(err) => err ? reject(err) : resolve(),
|
|
494
|
+
);
|
|
495
|
+
const rs = fh.createReadStream({ autoClose: false });
|
|
496
|
+
rs.on('error', reject);
|
|
497
|
+
entry.on('error', reject);
|
|
498
|
+
rs.pipe(entry);
|
|
499
|
+
});
|
|
500
|
+
} finally {
|
|
501
|
+
await fh.close();
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// Symlinks, sockets, FIFOs, etc. are intentionally skipped —
|
|
505
|
+
// `dirent.isFile()` is false for those, even when the symlink
|
|
506
|
+
// points at a regular file. Out of scope for "downloadable pod
|
|
507
|
+
// data" and would complicate restore semantics.
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Add a tar directory entry. `tar-stream` distinguishes by name
|
|
513
|
+
* suffix (`/`) and explicit `type: 'directory'`.
|
|
514
|
+
*/
|
|
515
|
+
function addDirEntry(pack, name) {
|
|
516
|
+
return new Promise((resolve, reject) => {
|
|
517
|
+
pack.entry({ name, type: 'directory', size: 0 }, (err) => {
|
|
518
|
+
if (err) reject(err);
|
|
519
|
+
else resolve();
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Add a single in-memory entry to the tar pack. Promisified wrapper
|
|
526
|
+
* around `pack.entry({...}, callback)` so the caller can await.
|
|
527
|
+
*/
|
|
528
|
+
function addEntry(pack, name, content) {
|
|
529
|
+
return new Promise((resolve, reject) => {
|
|
530
|
+
pack.entry({ name, size: content.length }, content, (err) => {
|
|
531
|
+
if (err) reject(err);
|
|
532
|
+
else resolve();
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Squash a WebID into something safe for a download filename.
|
|
539
|
+
* Just enough to be useful as a hint; doesn't need to round-trip.
|
|
540
|
+
*/
|
|
541
|
+
function sanitizeSlug(webId) {
|
|
542
|
+
return webId
|
|
543
|
+
.replace(/^https?:\/\//, '')
|
|
544
|
+
.replace(/[#?].*$/, '')
|
|
545
|
+
.replace(/[^A-Za-z0-9._-]+/g, '_')
|
|
546
|
+
.slice(0, 80) || 'pod';
|
|
547
|
+
}
|
package/src/idp/index.js
CHANGED
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
handleAccountDeleteForm,
|
|
28
28
|
setNoCacheClickjackHeaders,
|
|
29
29
|
} from './credentials.js';
|
|
30
|
+
import { handleExportAccount } from './export.js';
|
|
30
31
|
import * as passkey from './passkey.js';
|
|
31
32
|
import { addTrustedIssuer } from '../auth/solid-oidc.js';
|
|
32
33
|
import { landingPage, accountDeletePage } from './views.js';
|
|
@@ -36,9 +37,21 @@ import { landingPage, accountDeletePage } from './views.js';
|
|
|
36
37
|
* @param {FastifyInstance} fastify
|
|
37
38
|
* @param {object} options
|
|
38
39
|
* @param {string} options.issuer - The issuer URL
|
|
40
|
+
* @param {boolean} [options.inviteOnly=false] - If true, /idp/register
|
|
41
|
+
* requires a valid invite code; public registration is disabled.
|
|
42
|
+
* @param {boolean} [options.singleUser=false] - Single-user mode.
|
|
43
|
+
* Disables /idp/register and /idp/account DELETE; gates the
|
|
44
|
+
* single-user branch in /idp/account/export.
|
|
45
|
+
* @param {string|null} [options.singleUserName=null] - Single-user
|
|
46
|
+
* pod name. null → root pod (podDir = dataRoot); string → pod
|
|
47
|
+
* lives at <dataRoot>/<name>/. Threaded into /idp/account/export
|
|
48
|
+
* so the handler can resolve podDir + apply ROOT_POD_EXCLUDE.
|
|
49
|
+
* @param {string} [options.jssVersion] - Server version, written
|
|
50
|
+
* into the export manifest for forensic / "what server made this"
|
|
51
|
+
* purposes. Defaults to 'unknown' inside the export handler.
|
|
39
52
|
*/
|
|
40
53
|
export async function idpPlugin(fastify, options) {
|
|
41
|
-
const { issuer, inviteOnly = false, singleUser = false } = options;
|
|
54
|
+
const { issuer, inviteOnly = false, singleUser = false, singleUserName = null, jssVersion } = options;
|
|
42
55
|
|
|
43
56
|
if (!issuer) {
|
|
44
57
|
throw new Error('IdP requires issuer URL');
|
|
@@ -297,6 +310,34 @@ export async function idpPlugin(fastify, options) {
|
|
|
297
310
|
return handleDeleteAccount(request, reply, { singleUser });
|
|
298
311
|
});
|
|
299
312
|
|
|
313
|
+
// GET account export — authenticated owner downloads their pod tree as
|
|
314
|
+
// a streamed tar.gz (#353). MVP slice of the Credible Exit ladder
|
|
315
|
+
// (#448). Lighter rate-limit than the destructive endpoints — this is
|
|
316
|
+
// a read, but a heavy one (entire pod), so cap at 3/min to deter
|
|
317
|
+
// abuse without blocking a legitimate operator pulling a backup.
|
|
318
|
+
//
|
|
319
|
+
// Keyed by IP, consistent with the other /idp/ endpoints. We can't
|
|
320
|
+
// honestly key by WebID here: the global auth hook in src/server.js
|
|
321
|
+
// skips /idp/* (so request.webId is unset at this phase) and the
|
|
322
|
+
// rate-limit keyGenerator is sync, so we can't await token
|
|
323
|
+
// verification inline. Per-user keying is a follow-up that needs
|
|
324
|
+
// a preParsing hook resolving auth before the limiter runs.
|
|
325
|
+
fastify.get('/idp/account/export', {
|
|
326
|
+
config: {
|
|
327
|
+
rateLimit: {
|
|
328
|
+
max: 3,
|
|
329
|
+
timeWindow: '1 minute',
|
|
330
|
+
keyGenerator: (request) => request.ip
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}, async (request, reply) => {
|
|
334
|
+
return handleExportAccount(request, reply, {
|
|
335
|
+
singleUser,
|
|
336
|
+
singleUserName,
|
|
337
|
+
jssVersion,
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
300
341
|
// GET account-delete form (#392) - human-friendly UI for #352. Public
|
|
301
342
|
// unauthenticated page; auth happens at form submission via password.
|
|
302
343
|
// Single-user mode returns 403 to stay consistent with /idp/register's
|
package/src/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Fastify from 'fastify';
|
|
2
2
|
import rateLimit from '@fastify/rate-limit';
|
|
3
3
|
import { readFile } from 'fs/promises';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
4
5
|
import { STATUS_CODES } from 'node:http';
|
|
5
6
|
import { join, dirname } from 'path';
|
|
6
7
|
import { fileURLToPath } from 'url';
|
|
@@ -321,7 +322,28 @@ export function createServer(options = {}) {
|
|
|
321
322
|
|
|
322
323
|
// Register Identity Provider plugin if enabled
|
|
323
324
|
if (idpEnabled) {
|
|
324
|
-
|
|
325
|
+
// singleUserName + jssVersion are threaded through for the
|
|
326
|
+
// pod-data export endpoint (#353), which uses singleUserName to
|
|
327
|
+
// resolve the pod's on-disk path and writes the version into
|
|
328
|
+
// the export manifest for forensic / "what server made this"
|
|
329
|
+
// purposes. Reading the package.json lazily here keeps the
|
|
330
|
+
// export endpoint independent of any seedServerRoot work.
|
|
331
|
+
let jssVersion = 'unknown';
|
|
332
|
+
try {
|
|
333
|
+
// Sync read because createServer isn't async and we need the
|
|
334
|
+
// version to thread into idpPlugin registration below. The file
|
|
335
|
+
// is tiny + on local disk. There is a second async read of
|
|
336
|
+
// package.json in the onReady hook for seedServerRoot — both
|
|
337
|
+
// are read-once at startup so drift is bounded to "package.json
|
|
338
|
+
// changed between two ~ms-apart reads", which doesn't happen
|
|
339
|
+
// in practice. Hoisting both into a memoized module-level
|
|
340
|
+
// helper is a worthwhile follow-up but out of scope for #353.
|
|
341
|
+
const pkgRaw = readFileSync(join(__dirname, '..', 'package.json'), 'utf8');
|
|
342
|
+
jssVersion = JSON.parse(pkgRaw).version;
|
|
343
|
+
} catch { /* keep 'unknown' */ }
|
|
344
|
+
fastify.register(idpPlugin, {
|
|
345
|
+
issuer: idpIssuer, inviteOnly, singleUser, singleUserName, jssVersion,
|
|
346
|
+
});
|
|
325
347
|
}
|
|
326
348
|
|
|
327
349
|
// Register Nostr relay if enabled
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET /idp/account/export — pod data export endpoint (#353).
|
|
3
|
+
*
|
|
4
|
+
* MVP slice of the Credible Exit ladder (#448). The end user takes
|
|
5
|
+
* their pod data with them, no operator help required.
|
|
6
|
+
*
|
|
7
|
+
* Coverage:
|
|
8
|
+
* - 401 unauthenticated
|
|
9
|
+
* - 403 cross-account (multi-user: caller authed as B can't pull A)
|
|
10
|
+
* - 200 owner export → valid tar.gz containing manifest + account
|
|
11
|
+
* + pod tree
|
|
12
|
+
* - account.json never carries passwordHash
|
|
13
|
+
* - manifest shape (webId, podName, mode, exportedAt, jssVersion)
|
|
14
|
+
* - Single-user mode + --provision-keys: archive contains
|
|
15
|
+
* /private/privkey.jsonld (per Credible Exit framing — the
|
|
16
|
+
* user's secret IS theirs and must leave with them)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, it, before, after } from 'node:test';
|
|
20
|
+
import assert from 'node:assert';
|
|
21
|
+
import fs from 'fs-extra';
|
|
22
|
+
import path from 'path';
|
|
23
|
+
import zlib from 'zlib';
|
|
24
|
+
import tar from 'tar-stream';
|
|
25
|
+
import { Readable } from 'stream';
|
|
26
|
+
import { createServer } from '../src/server.js';
|
|
27
|
+
import { createToken } from '../src/auth/token.js';
|
|
28
|
+
|
|
29
|
+
// startServer/stopServer manage process.env.DATA_ROOT via a snapshot
|
|
30
|
+
// stored on the returned server. createServer mutates DATA_ROOT
|
|
31
|
+
// (src/server.js:175); without restore, subsequent unrelated tests
|
|
32
|
+
// reading DATA_ROOT in this run see a stale path pointing at a
|
|
33
|
+
// now-removed directory.
|
|
34
|
+
async function startServer(dataDir, options = {}) {
|
|
35
|
+
const prevDataRoot = process.env.DATA_ROOT;
|
|
36
|
+
await fs.remove(dataDir);
|
|
37
|
+
await fs.ensureDir(dataDir);
|
|
38
|
+
const server = createServer({
|
|
39
|
+
logger: false,
|
|
40
|
+
forceCloseConnections: true,
|
|
41
|
+
root: dataDir,
|
|
42
|
+
idp: true,
|
|
43
|
+
idpIssuer: 'http://127.0.0.1/',
|
|
44
|
+
...options,
|
|
45
|
+
});
|
|
46
|
+
await server.listen({ port: 0, host: '127.0.0.1' });
|
|
47
|
+
const baseUrl = `http://127.0.0.1:${server.server.address().port}`;
|
|
48
|
+
server.__prevDataRoot = prevDataRoot;
|
|
49
|
+
return { server, baseUrl };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function stopServer(server, dataDir) {
|
|
53
|
+
await server.close();
|
|
54
|
+
await fs.remove(dataDir);
|
|
55
|
+
if (server.__prevDataRoot === undefined) delete process.env.DATA_ROOT;
|
|
56
|
+
else process.env.DATA_ROOT = server.__prevDataRoot;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Read a tar.gz archive from a Buffer or Response into a map of
|
|
61
|
+
* `{ filename: Buffer-content }`. Tests can then assert on filenames
|
|
62
|
+
* + content shapes without touching disk.
|
|
63
|
+
*/
|
|
64
|
+
async function unpackTarGz(buf) {
|
|
65
|
+
const out = {};
|
|
66
|
+
const extract = tar.extract();
|
|
67
|
+
await new Promise((resolve, reject) => {
|
|
68
|
+
extract.on('entry', (header, stream, next) => {
|
|
69
|
+
const chunks = [];
|
|
70
|
+
stream.on('data', (c) => chunks.push(c));
|
|
71
|
+
stream.on('end', () => {
|
|
72
|
+
out[header.name] = Buffer.concat(chunks);
|
|
73
|
+
next();
|
|
74
|
+
});
|
|
75
|
+
stream.on('error', reject);
|
|
76
|
+
stream.resume();
|
|
77
|
+
});
|
|
78
|
+
extract.on('finish', resolve);
|
|
79
|
+
extract.on('error', reject);
|
|
80
|
+
Readable.from(buf).pipe(zlib.createGunzip()).pipe(extract);
|
|
81
|
+
});
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
describe('GET /idp/account/export — multi-user', () => {
|
|
86
|
+
const DATA_DIR = './test-data-export-mu';
|
|
87
|
+
// Alice plants a uniquely-named marker resource in her pod. Bob's
|
|
88
|
+
// export must not contain anything matching this name OR contents.
|
|
89
|
+
// Anchors the cross-account test against the actual files-on-disk
|
|
90
|
+
// shape rather than the archive's prefix layout (which never embeds
|
|
91
|
+
// a username segment, so a plain `/alice/` regex would tautologically
|
|
92
|
+
// pass even if Bob's archive somehow contained Alice's bytes).
|
|
93
|
+
const ALICE_CANARY_NAME = 'alice-canary-do-not-leak.txt';
|
|
94
|
+
const ALICE_CANARY_BODY = 'ALICE_SECRET_CANARY_a7f3e9d1c4b2';
|
|
95
|
+
let server, baseUrl, aliceToken, bobToken;
|
|
96
|
+
|
|
97
|
+
before(async () => {
|
|
98
|
+
({ server, baseUrl } = await startServer(DATA_DIR));
|
|
99
|
+
// Create two pods so the cross-account property is real.
|
|
100
|
+
// Hard-fail on pod creation so a /.pods shape regression surfaces
|
|
101
|
+
// as "pod creation failed" rather than as cryptic 401s in every
|
|
102
|
+
// downstream test. Same pattern as the single-user before hooks.
|
|
103
|
+
const aliceRes = await fetch(`${baseUrl}/.pods`, {
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: { 'Content-Type': 'application/json' },
|
|
106
|
+
body: JSON.stringify({
|
|
107
|
+
name: 'alice', email: 'alice@example.com', password: 'pw-alice-123'
|
|
108
|
+
})
|
|
109
|
+
});
|
|
110
|
+
assert.ok(aliceRes.ok, `alice pod creation failed: ${aliceRes.status}`);
|
|
111
|
+
aliceToken = (await aliceRes.json()).token;
|
|
112
|
+
assert.ok(aliceToken, 'alice pod creation must return a token');
|
|
113
|
+
const bobRes = await fetch(`${baseUrl}/.pods`, {
|
|
114
|
+
method: 'POST',
|
|
115
|
+
headers: { 'Content-Type': 'application/json' },
|
|
116
|
+
body: JSON.stringify({
|
|
117
|
+
name: 'bob', email: 'bob@example.com', password: 'pw-bob-456'
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
assert.ok(bobRes.ok, `bob pod creation failed: ${bobRes.status}`);
|
|
121
|
+
bobToken = (await bobRes.json()).token;
|
|
122
|
+
assert.ok(bobToken, 'bob pod creation must return a token');
|
|
123
|
+
|
|
124
|
+
// Plant the alice-only canary file directly on disk under
|
|
125
|
+
// <DATA_ROOT>/alice/. PUT through the LDP layer would also work
|
|
126
|
+
// but adds an auth round-trip we don't need for this assertion.
|
|
127
|
+
await fs.outputFile(
|
|
128
|
+
path.join(DATA_DIR, 'alice', ALICE_CANARY_NAME),
|
|
129
|
+
ALICE_CANARY_BODY,
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
after(async () => {
|
|
134
|
+
await stopServer(server, DATA_DIR);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('returns 401 unauthenticated', async () => {
|
|
138
|
+
const res = await fetch(`${baseUrl}/idp/account/export`);
|
|
139
|
+
assert.strictEqual(res.status, 401);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('returns 200 with a tar.gz for the authenticated owner', async () => {
|
|
143
|
+
const res = await fetch(`${baseUrl}/idp/account/export`, {
|
|
144
|
+
headers: { Authorization: `Bearer ${aliceToken}` }
|
|
145
|
+
});
|
|
146
|
+
assert.strictEqual(res.status, 200);
|
|
147
|
+
assert.strictEqual(res.headers.get('content-type'), 'application/x-tar+gzip');
|
|
148
|
+
assert.match(res.headers.get('content-disposition') || '', /^attachment; filename="jss-export-/);
|
|
149
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
150
|
+
const files = await unpackTarGz(buf);
|
|
151
|
+
|
|
152
|
+
// Manifest first.
|
|
153
|
+
assert.ok(files['jss-export/manifest.json'], 'manifest must be present');
|
|
154
|
+
const manifest = JSON.parse(files['jss-export/manifest.json'].toString('utf8'));
|
|
155
|
+
assert.strictEqual(manifest.username, 'alice');
|
|
156
|
+
assert.strictEqual(manifest.podName, 'alice');
|
|
157
|
+
assert.strictEqual(manifest.mode, 'multi-user');
|
|
158
|
+
assert.match(manifest.exportedAt, /^\d{4}-\d{2}-\d{2}T/);
|
|
159
|
+
assert.match(manifest.webId, /alice\/profile\/card\.jsonld#me$/);
|
|
160
|
+
|
|
161
|
+
// Account record present, sans passwordHash.
|
|
162
|
+
assert.ok(files['jss-export/account.json'], 'account.json must be present');
|
|
163
|
+
const account = JSON.parse(files['jss-export/account.json'].toString('utf8'));
|
|
164
|
+
assert.strictEqual(account.username, 'alice');
|
|
165
|
+
assert.strictEqual(account.email, 'alice@example.com');
|
|
166
|
+
assert.strictEqual(account.passwordHash, undefined,
|
|
167
|
+
'account.json must NEVER include the password hash');
|
|
168
|
+
|
|
169
|
+
// Pod tree contents — at minimum the seeded files.
|
|
170
|
+
const podKeys = Object.keys(files).filter(k => k.startsWith('jss-export/pod/'));
|
|
171
|
+
assert.ok(podKeys.length > 0, 'pod tree must be packed');
|
|
172
|
+
assert.ok(podKeys.some(k => k.endsWith('profile/card.jsonld')),
|
|
173
|
+
'WebID profile must be in the export');
|
|
174
|
+
assert.ok(podKeys.some(k => k.endsWith('.acl')),
|
|
175
|
+
'ACL files must be in the export');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("scopes the export to the authenticated caller — no cross-account exposure", async () => {
|
|
179
|
+
// The endpoint takes no target parameter; the WebID is taken
|
|
180
|
+
// from the auth context. Cross-account access is structurally
|
|
181
|
+
// impossible to attempt (so there's no 403 case). This test
|
|
182
|
+
// pins the *property* by confirming bob's authenticated call
|
|
183
|
+
// returns bob's data and never anything from alice's pod.
|
|
184
|
+
const res = await fetch(`${baseUrl}/idp/account/export`, {
|
|
185
|
+
headers: { Authorization: `Bearer ${bobToken}` }
|
|
186
|
+
});
|
|
187
|
+
assert.strictEqual(res.status, 200);
|
|
188
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
189
|
+
const files = await unpackTarGz(buf);
|
|
190
|
+
|
|
191
|
+
// Manifest identifies bob.
|
|
192
|
+
const manifest = JSON.parse(files['jss-export/manifest.json'].toString('utf8'));
|
|
193
|
+
assert.strictEqual(manifest.username, 'bob');
|
|
194
|
+
assert.strictEqual(manifest.podName, 'bob');
|
|
195
|
+
|
|
196
|
+
// Account record (the actual server-side record, not the manifest)
|
|
197
|
+
// identifies bob — guards against a bug where the wrong account
|
|
198
|
+
// is looked up but the manifest is built from the request webId.
|
|
199
|
+
assert.ok(files['jss-export/account.json'],
|
|
200
|
+
'account.json must be present in bob\'s export');
|
|
201
|
+
const account = JSON.parse(files['jss-export/account.json'].toString('utf8'));
|
|
202
|
+
assert.strictEqual(account.username, 'bob',
|
|
203
|
+
'account.json.username must be bob, not alice');
|
|
204
|
+
assert.strictEqual(account.email, 'bob@example.com');
|
|
205
|
+
|
|
206
|
+
// The alice-only canary file must NOT appear in bob's archive,
|
|
207
|
+
// by entry name or by entry contents. This is the substantive
|
|
208
|
+
// cross-account assertion — the previous `/alice/` regex check
|
|
209
|
+
// was tautological because pod-tree entries are namespaced by
|
|
210
|
+
// archive prefix (`jss-export/pod/...`), not by username segment.
|
|
211
|
+
const allKeys = Object.keys(files);
|
|
212
|
+
for (const k of allKeys) {
|
|
213
|
+
assert.ok(
|
|
214
|
+
!k.endsWith(ALICE_CANARY_NAME),
|
|
215
|
+
`bob's export must not contain alice's canary file: ${k}`,
|
|
216
|
+
);
|
|
217
|
+
// Body check: even if the entry name was reshaped, the canary
|
|
218
|
+
// body bytes must never appear in any of bob's archive entries.
|
|
219
|
+
assert.ok(
|
|
220
|
+
!files[k].includes(ALICE_CANARY_BODY),
|
|
221
|
+
`bob's export entry ${k} contains alice's canary body bytes`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('GET /idp/account/export — single-user ROOT pod (denylist check)', () => {
|
|
228
|
+
// Critical: in single-user root-pod mode (the default since #348),
|
|
229
|
+
// podDir IS dataRoot. Without an explicit denylist, the export would
|
|
230
|
+
// ship server-internal directories that live next to pod data:
|
|
231
|
+
//
|
|
232
|
+
// .idp/ — every account record (incl. passwordHash) and the
|
|
233
|
+
// IdP signing keys that mint tokens for any user
|
|
234
|
+
// .private/ — pay handler's Bitcoin keypair + UTXO state
|
|
235
|
+
// (recipient could drain pay balance / spend UTXOs)
|
|
236
|
+
//
|
|
237
|
+
// The handler must refuse to include either at the top level.
|
|
238
|
+
const DATA_DIR = './test-data-export-root-pod';
|
|
239
|
+
let server, baseUrl, ownerToken;
|
|
240
|
+
|
|
241
|
+
before(async () => {
|
|
242
|
+
({ server, baseUrl } = await startServer(DATA_DIR, {
|
|
243
|
+
singleUser: true,
|
|
244
|
+
// No singleUserName → root pod (#348 default)
|
|
245
|
+
singleUserName: null,
|
|
246
|
+
singleUserPassword: 'pw-root-321',
|
|
247
|
+
provisionKeys: true,
|
|
248
|
+
}));
|
|
249
|
+
const credRes = await fetch(`${baseUrl}/idp/credentials`, {
|
|
250
|
+
method: 'POST',
|
|
251
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
252
|
+
body: new URLSearchParams({ username: 'me', password: 'pw-root-321' }).toString(),
|
|
253
|
+
});
|
|
254
|
+
if (credRes.status === 200) {
|
|
255
|
+
const body = await credRes.json();
|
|
256
|
+
ownerToken = body.access_token || body.token;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
after(async () => {
|
|
261
|
+
await stopServer(server, DATA_DIR);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('does NOT pack server-internal dirs (.idp/, .private/) at root', async () => {
|
|
265
|
+
// Hard-fail rather than t.skip — a credentials regression must
|
|
266
|
+
// not silently disable this denylist test, which is the only
|
|
267
|
+
// assertion guarding against the catastrophic root-pod leak.
|
|
268
|
+
assert.ok(ownerToken,
|
|
269
|
+
'pre-condition: IDP credentials handshake must return a token; ' +
|
|
270
|
+
'a regression here would silently skip the denylist assertion');
|
|
271
|
+
|
|
272
|
+
// Sanity: confirm the server actually wrote these dirs to disk so
|
|
273
|
+
// the denylist assertion below is exercising real entries. We
|
|
274
|
+
// synthesize .private/ ourselves (pay handler only writes it on
|
|
275
|
+
// first /pay use) so the test doesn't depend on side-channel
|
|
276
|
+
// activity to be meaningful. We also pin the IdP secret-bearing
|
|
277
|
+
// file specifically — the property under test is "no IdP secrets
|
|
278
|
+
// appear in the export", not just "no entries under one specific
|
|
279
|
+
// dotted prefix". A future refactor moving secrets out of
|
|
280
|
+
// `.idp/accounts/*.json` would fail this pre-condition loudly
|
|
281
|
+
// and force the denylist + assertion to be re-pinned to wherever
|
|
282
|
+
// the secrets moved.
|
|
283
|
+
await fs.outputFile(
|
|
284
|
+
path.join(DATA_DIR, '.private', 'keypair.json'),
|
|
285
|
+
JSON.stringify({ canary: 'must-not-leak' }),
|
|
286
|
+
);
|
|
287
|
+
const idpAccounts = await fs.readdir(path.join(DATA_DIR, '.idp', 'accounts'))
|
|
288
|
+
.catch(() => []);
|
|
289
|
+
const accountFiles = idpAccounts.filter(f => f.endsWith('.json'));
|
|
290
|
+
assert.ok(accountFiles.length > 0,
|
|
291
|
+
'pre-condition: .idp/accounts/*.json must exist (the actual ' +
|
|
292
|
+
'secret-bearing material that the denylist must keep out of the export)');
|
|
293
|
+
assert.ok(
|
|
294
|
+
await fs.pathExists(path.join(DATA_DIR, '.private', 'keypair.json')),
|
|
295
|
+
'pre-condition: .private/keypair.json must exist'
|
|
296
|
+
);
|
|
297
|
+
const res = await fetch(`${baseUrl}/idp/account/export`, {
|
|
298
|
+
headers: { Authorization: `Bearer ${ownerToken}` }
|
|
299
|
+
});
|
|
300
|
+
assert.strictEqual(res.status, 200);
|
|
301
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
302
|
+
const files = await unpackTarGz(buf);
|
|
303
|
+
|
|
304
|
+
// Critical: NOTHING under jss-export/pod/.idp/ or jss-export/pod/.private/.
|
|
305
|
+
const leakedEntries = Object.keys(files).filter(k =>
|
|
306
|
+
k.startsWith('jss-export/pod/.idp/') || k === 'jss-export/pod/.idp' ||
|
|
307
|
+
k.startsWith('jss-export/pod/.private/') || k === 'jss-export/pod/.private'
|
|
308
|
+
);
|
|
309
|
+
assert.strictEqual(leakedEntries.length, 0,
|
|
310
|
+
`Server-internal dirs must not be packed in root-pod export. ` +
|
|
311
|
+
`Found: ${leakedEntries.join(', ')}`);
|
|
312
|
+
|
|
313
|
+
// Sanity: actual pod content IS in the archive.
|
|
314
|
+
const podKeys = Object.keys(files).filter(k => k.startsWith('jss-export/pod/'));
|
|
315
|
+
assert.ok(podKeys.some(k => k.endsWith('profile/card.jsonld')),
|
|
316
|
+
'pod content must still be exported');
|
|
317
|
+
assert.ok(podKeys.some(k => k.endsWith('private/privkey.jsonld')),
|
|
318
|
+
'pod /private/ must still be exported (this is pod data, not server-internal)');
|
|
319
|
+
|
|
320
|
+
// Manifest shape in root-pod mode. Pins the parity property:
|
|
321
|
+
// manifest.podName MUST equal account.json.podName so a downstream
|
|
322
|
+
// importer keying on either field gets the same answer. Without
|
|
323
|
+
// this, root-pod previously emitted manifest.podName=null while
|
|
324
|
+
// accountRecord.podName='me' — silent disagreement in the same
|
|
325
|
+
// archive.
|
|
326
|
+
assert.ok(files['jss-export/manifest.json'], 'manifest.json must be present');
|
|
327
|
+
assert.ok(files['jss-export/account.json'], 'account.json must be present');
|
|
328
|
+
const manifest = JSON.parse(files['jss-export/manifest.json'].toString('utf8'));
|
|
329
|
+
const account = JSON.parse(files['jss-export/account.json'].toString('utf8'));
|
|
330
|
+
assert.strictEqual(manifest.mode, 'single-user');
|
|
331
|
+
assert.strictEqual(manifest.username, 'me',
|
|
332
|
+
'root-pod manifest carries the seeded username');
|
|
333
|
+
assert.strictEqual(manifest.podName, account.podName,
|
|
334
|
+
'manifest.podName must match account.json.podName — single source of truth');
|
|
335
|
+
assert.strictEqual(account.podName, 'me',
|
|
336
|
+
'seeded root-pod account.podName is "me" (the OIDC short name)');
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe('GET /idp/account/export — single-user with --provision-keys', () => {
|
|
341
|
+
const DATA_DIR = './test-data-export-su-keys';
|
|
342
|
+
let server, baseUrl, ownerToken;
|
|
343
|
+
|
|
344
|
+
before(async () => {
|
|
345
|
+
({ server, baseUrl } = await startServer(DATA_DIR, {
|
|
346
|
+
singleUser: true,
|
|
347
|
+
singleUserName: 'me',
|
|
348
|
+
singleUserPassword: 'pw-me-789',
|
|
349
|
+
provisionKeys: true,
|
|
350
|
+
}));
|
|
351
|
+
// Auth via the seeded IDP account password.
|
|
352
|
+
const credRes = await fetch(`${baseUrl}/idp/credentials`, {
|
|
353
|
+
method: 'POST',
|
|
354
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
355
|
+
body: new URLSearchParams({ username: 'me', password: 'pw-me-789' }).toString(),
|
|
356
|
+
});
|
|
357
|
+
if (credRes.status === 200) {
|
|
358
|
+
const body = await credRes.json();
|
|
359
|
+
ownerToken = body.access_token || body.token;
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
after(async () => {
|
|
364
|
+
await stopServer(server, DATA_DIR);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('exports the pod tree including the on-disk owner secret', async () => {
|
|
368
|
+
// Hard-fail rather than t.skip — a credentials regression must
|
|
369
|
+
// not silently turn this Credible Exit assertion into a no-op.
|
|
370
|
+
assert.ok(ownerToken,
|
|
371
|
+
'pre-condition: IDP credentials must return a token; a silent ' +
|
|
372
|
+
'skip here would mask a regression in single-user authentication');
|
|
373
|
+
const res = await fetch(`${baseUrl}/idp/account/export`, {
|
|
374
|
+
headers: { Authorization: `Bearer ${ownerToken}` }
|
|
375
|
+
});
|
|
376
|
+
assert.strictEqual(res.status, 200);
|
|
377
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
378
|
+
const files = await unpackTarGz(buf);
|
|
379
|
+
|
|
380
|
+
const manifest = JSON.parse(files['jss-export/manifest.json'].toString('utf8'));
|
|
381
|
+
assert.strictEqual(manifest.mode, 'single-user',
|
|
382
|
+
'manifest.mode reflects the server mode, not the existence of an account record');
|
|
383
|
+
assert.strictEqual(manifest.podName, 'me',
|
|
384
|
+
'singleUserName="me" → pod is at <DATA_ROOT>/me/ → podName is "me"');
|
|
385
|
+
// Single-user manifest now includes account-derived fields when an
|
|
386
|
+
// account record exists, matching the multi-user manifest shape so
|
|
387
|
+
// a downstream importer doesn't see different shapes per server mode.
|
|
388
|
+
assert.strictEqual(manifest.username, 'me');
|
|
389
|
+
assert.match(manifest.webId, /me\/profile\/card\.jsonld#me$/);
|
|
390
|
+
assert.ok(manifest.createdAt, 'manifest.createdAt must be populated from the seeded account');
|
|
391
|
+
|
|
392
|
+
// The on-disk secret must be in the archive — Credible Exit
|
|
393
|
+
// requires the user can leave with their identity, not just
|
|
394
|
+
// their bytes. Refusing to include the secret would make L4+
|
|
395
|
+
// identity migration impossible.
|
|
396
|
+
const podKeys = Object.keys(files).filter(k => k.startsWith('jss-export/pod/'));
|
|
397
|
+
assert.ok(
|
|
398
|
+
podKeys.some(k => k.endsWith('private/privkey.jsonld')),
|
|
399
|
+
`/private/privkey.jsonld must be in the archive (Credible Exit). Pod entries: ${podKeys.join(', ')}`
|
|
400
|
+
);
|
|
401
|
+
// And the WebID profile carrying the public side.
|
|
402
|
+
assert.ok(podKeys.some(k => k.endsWith('profile/card.jsonld')));
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it('rejects an authenticated third-party WebID with 403', async () => {
|
|
406
|
+
// Single-user mode previously trusted ANY successfully-authenticated
|
|
407
|
+
// WebID and shipped the entire pod (incl. /private/privkey.jsonld)
|
|
408
|
+
// to the caller. If the server accepts external Solid-OIDC issuers,
|
|
409
|
+
// LWS-CID JWTs, or any non-local WebID, that meant a third party's
|
|
410
|
+
// bearer token was sufficient to download the operator's secret.
|
|
411
|
+
//
|
|
412
|
+
// The handler now refuses with 403 when the authenticated WebID
|
|
413
|
+
// does NOT match the seeded single-user account — same shape as
|
|
414
|
+
// the multi-user "no local account record" 403.
|
|
415
|
+
//
|
|
416
|
+
// We mint the third-party token with createToken (same HMAC
|
|
417
|
+
// SECRET the server uses) so verifyToken accepts the signature
|
|
418
|
+
// and getWebIdFromRequestAsync resolves to the foreign WebID.
|
|
419
|
+
const intruderWebId = 'http://attacker.example.com/profile/card.jsonld#me';
|
|
420
|
+
const intruderToken = createToken(intruderWebId, 3600);
|
|
421
|
+
const res = await fetch(`${baseUrl}/idp/account/export`, {
|
|
422
|
+
headers: { Authorization: `Bearer ${intruderToken}` }
|
|
423
|
+
});
|
|
424
|
+
assert.strictEqual(res.status, 403,
|
|
425
|
+
'authenticated-but-not-owner WebID must NOT receive the pod export');
|
|
426
|
+
const body = await res.json();
|
|
427
|
+
assert.strictEqual(body.error, 'forbidden');
|
|
428
|
+
});
|
|
429
|
+
});
|