javascript-solid-server 0.0.210 → 0.0.212
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/.claude/settings.local.json +7 -51
- package/docs/configuration.md +1 -1
- package/package.json +1 -1
- package/src/auth/nostr-keys.js +49 -22
- package/src/auth/nostr.js +13 -21
- package/src/handlers/container.js +40 -1
- package/src/server.js +5 -2
- package/src/wac/checker.js +28 -4
- package/test/auth.test.js +54 -0
- package/test/nostr-key-parity.test.js +96 -0
- package/test/server-root.test.js +50 -0
- package/test/wac.test.js +75 -0
|
@@ -1,58 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"permissions": {
|
|
3
3
|
"allow": [
|
|
4
|
+
"Bash(node bin/jss.js start:*)",
|
|
5
|
+
"Bash(node:*)",
|
|
6
|
+
"Bash(source ~/.nvm/nvm.sh)",
|
|
7
|
+
"Bash(nvm use:*)",
|
|
4
8
|
"Bash(gh issue *)",
|
|
5
|
-
"Bash(
|
|
6
|
-
"Bash(
|
|
7
|
-
"Bash(
|
|
8
|
-
"Bash(git add *)",
|
|
9
|
-
"Bash(git commit -q -m 'docs: add Account Management page with API endpoints \\(#23\\) *)",
|
|
10
|
-
"Bash(git push *)",
|
|
11
|
-
"Bash(gh pr create --title 'docs: Account Management page \\(change password, backup/export, delete\\) — closes #23' --body ' *)",
|
|
12
|
-
"Bash(gh pr *)",
|
|
13
|
-
"Bash(git checkout *)",
|
|
14
|
-
"Bash(git pull *)",
|
|
15
|
-
"Bash(gh api *)",
|
|
16
|
-
"Bash(git fetch *)",
|
|
17
|
-
"Bash(grep *)",
|
|
18
|
-
"Bash(node --check src/cli/install.js)",
|
|
19
|
-
"Bash(git commit -q -m 'fix\\(install\\): drop spawnSync\\('\\\\''rm'\\\\''\\) shell-out for fs.rmSync — works on Windows *)",
|
|
20
|
-
"Bash(node --test --test-concurrency=1 test/turtle.test.js)",
|
|
21
|
-
"Bash(npm test *)",
|
|
22
|
-
"Bash(git commit -q -m 'fix\\(rdf\\): preserve blank-node SUBJECTS through Turtle ↔ JSON-LD \\(#536\\) *)",
|
|
23
|
-
"Bash(npm view *)",
|
|
24
|
-
"Bash(git branch *)",
|
|
25
|
-
"Bash(git commit -q -m 'bump version to 0.0.205 *)",
|
|
26
|
-
"Bash(gh repo *)",
|
|
27
|
-
"Bash(curl -sL https://raw.githubusercontent.com/JavaScriptSolidServer/solid-oidc/gh-pages/example.html)",
|
|
28
|
-
"Bash(curl -sL https://raw.githubusercontent.com/JavaScriptSolidServer/solid-oidc/gh-pages/solid-oidc.js)",
|
|
29
|
-
"Bash(mkdir -p /tmp/jss-sanity-23)",
|
|
30
|
-
"Read(//tmp/**)",
|
|
31
|
-
"Bash(DATA_ROOT=. node /home/melvin/remote/github.com/JavaScriptSolidServer/JavaScriptSolidServer/bin/jss.js start --idp --conneg --port 4901 --quiet)",
|
|
32
|
-
"Bash(curl -si -c /tmp/jss-sanity-cookies.txt -b /tmp/jss-sanity-cookies.txt \"http://localhost:4901/idp/interaction/Qp-9Nn6O9Rv79dlCsI9YWGlXNZZswTi4UUJ2PjXt4x3\")",
|
|
33
|
-
"Bash(curl -s -c /tmp/jss-sanity-cookies.txt -b /tmp/jss-sanity-cookies.txt \"http://localhost:4901/idp/interaction/Qp-9Nn6O9Rv79dlCsI9YWGlXNZZswTi4UUJ2PjXt4x3\")",
|
|
34
|
-
"Bash(kill bx5n5fpi1 *)",
|
|
35
|
-
"Bash(pkill -f \"jss.js start --idp --port 4901\")",
|
|
36
|
-
"Bash(rm -f /tmp/jss-sanity-cookies.txt)",
|
|
37
|
-
"Bash(rm -rf /tmp/jss-sanity-23)",
|
|
38
|
-
"Bash(gh label *)",
|
|
39
|
-
"Bash(git commit -q -m 'fix\\(idp\\): handleSchnorrComplete recovers from interactionFinished throw — no more 504 hang \\(#412\\) *)",
|
|
40
|
-
"Bash(sed -n '163,170p' src/idp/interactions.js)",
|
|
41
|
-
"Bash(sed -n '283,295p' src/idp/interactions.js)",
|
|
42
|
-
"Bash(sed -n '600,610p' src/idp/interactions.js)",
|
|
43
|
-
"Bash(sed -n '638,650p' src/idp/interactions.js)",
|
|
44
|
-
"Bash(wait)",
|
|
45
|
-
"Bash(node --test --test-concurrency=1 test/schnorr-complete-defensive.test.js)",
|
|
46
|
-
"Bash(git commit -q -m 'review fixes \\(#539\\): stop leaking err.message + add regression tests *)",
|
|
47
|
-
"Bash(git commit -q -m 'review pass-3 \\(#539\\): log payload preserves stack + mock matches Node behaviour *)",
|
|
48
|
-
"Bash(git commit -q -m 'refactor\\(idp\\): sweep hijack-then-throw defensive pattern across all 5 interaction handlers \\(#526\\) *)",
|
|
49
|
-
"Bash(npm audit *)",
|
|
50
|
-
"Bash(node --check src/index.js)",
|
|
51
|
-
"Bash(node --check src/server.js)",
|
|
52
|
-
"Bash(node --test --test-concurrency=1 test/body-limit.test.js)",
|
|
53
|
-
"Bash(git commit -q -m 'feat: configurable bodyLimit via createServer / --body-limit / JSS_BODY_LIMIT \\(#474\\) *)",
|
|
54
|
-
"Bash(git commit -q -m 'review fixes \\(#543\\): unused imports + DATA_ROOT pollution + clearer comment *)",
|
|
55
|
-
"Bash(node --test --test-concurrency=1 test/ssrf.test.js)"
|
|
9
|
+
"Bash(GIT_CONFIG_NOSYSTEM=1 gh issue *)",
|
|
10
|
+
"Bash(GIT_CONFIG_NOSYSTEM=1 gh auth status)",
|
|
11
|
+
"Bash(git -C /home/melvin/remote/github.com/JavaScriptSolidServer/JavaScriptSolidServer remote -v)"
|
|
56
12
|
]
|
|
57
13
|
}
|
|
58
14
|
}
|
package/docs/configuration.md
CHANGED
|
@@ -160,7 +160,7 @@ Server: pub http://localhost:4443/alice/public/data.json (on change)
|
|
|
160
160
|
| `--ap-display-name <name>` | ActivityPub display name | (username) |
|
|
161
161
|
| `--ap-summary <text>` | ActivityPub bio/summary | - |
|
|
162
162
|
| `--ap-nostr-pubkey <hex>` | Nostr pubkey for identity linking | - |
|
|
163
|
-
| `--public` | Allow unauthenticated access (skip WAC) | false |
|
|
163
|
+
| `--public` | Allow unauthenticated access (skip WAC); never writes into the served directory (no landing-page seed) | false |
|
|
164
164
|
| `--read-only` | Disable PUT/DELETE/PATCH methods | false |
|
|
165
165
|
| `--live-reload` | Auto-refresh browser on file changes | false |
|
|
166
166
|
| `--pay` | Enable HTTP 402 paid access for /pay/* | false |
|
package/package.json
CHANGED
package/src/auth/nostr-keys.js
CHANGED
|
@@ -12,21 +12,52 @@ import { secp256k1 } from '@noble/curves/secp256k1';
|
|
|
12
12
|
/** Multicodec varint for secp256k1-pub: 0xe7 0x01 → "e701" hex. */
|
|
13
13
|
const MULTICODEC_SECP256K1_PUB_HEX = 'e701';
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The two valid y-coordinates (64-char hex) for a secp256k1 x: the
|
|
17
|
+
* even-parity point and its odd-parity reflection. Returns `null` if
|
|
18
|
+
* `xHex` isn't a valid curve x.
|
|
19
|
+
*
|
|
20
|
+
* Both parities are in-spec for did:nostr: the spec
|
|
21
|
+
* (https://nostrcg.github.io/did-nostr/) states "Nostr applications
|
|
22
|
+
* may generate keys with either 0x02 or 0x03 prefixes" — 0x02 for an
|
|
23
|
+
* even y, 0x03 for an odd y. So a verification method that encodes the
|
|
24
|
+
* same x-only Nostr identity can legitimately carry either parity, and
|
|
25
|
+
* key matching accepts either while still requiring `(x, y)` to be a
|
|
26
|
+
* real on-curve point. See issue #571.
|
|
27
|
+
*/
|
|
28
|
+
export function nostrJwkYParities(xHex) {
|
|
29
|
+
if (typeof xHex !== 'string') return null;
|
|
30
|
+
const x = xHex.toLowerCase();
|
|
31
|
+
if (!/^[0-9a-f]{64}$/.test(x)) return null;
|
|
32
|
+
try {
|
|
33
|
+
const even = secp256k1.ProjectivePoint.fromHex('02' + x).toAffine().y;
|
|
34
|
+
const odd = secp256k1.ProjectivePoint.fromHex('03' + x).toAffine().y;
|
|
35
|
+
return [
|
|
36
|
+
even.toString(16).padStart(64, '0'),
|
|
37
|
+
odd.toString(16).padStart(64, '0'),
|
|
38
|
+
];
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
15
44
|
/**
|
|
16
45
|
* Validate a secp256k1 JWK as a Nostr key and return its x-only
|
|
17
46
|
* pubkey hex. Returns `null` if the JWK isn't a Nostr-shaped key
|
|
18
|
-
* or its `y`
|
|
19
|
-
*
|
|
47
|
+
* or its `y` isn't an on-curve point (either parity) for the
|
|
48
|
+
* declared `x`.
|
|
20
49
|
*
|
|
21
|
-
* Why y matters: every secp256k1 x has TWO valid points (
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
50
|
+
* Why y matters: every secp256k1 x has TWO valid points (even and
|
|
51
|
+
* odd y). Nostr identities are x-only, so the x coordinate IS the
|
|
52
|
+
* identity — but a profile that declares a JWK with the right x and a
|
|
53
|
+
* *fabricated* (off-curve) y is malformed and must be rejected, else
|
|
54
|
+
* an attacker could plant arbitrary key material at someone else's
|
|
55
|
+
* WebID and have the indexer publish it. We therefore require y to be
|
|
56
|
+
* one of the two genuine on-curve y's, accepting either parity per the
|
|
57
|
+
* did:nostr spec (see `nostrJwkYParities` / issue #571).
|
|
27
58
|
*
|
|
28
|
-
* The verifier in src/auth/nostr.js (jwkMatchesNostrPubkey)
|
|
29
|
-
* the same
|
|
59
|
+
* The verifier in src/auth/nostr.js (jwkMatchesNostrPubkey) applies
|
|
60
|
+
* the same rule. Keeping the indexer in sync prevents the
|
|
30
61
|
* "indexed but verifier rejects" inconsistency that would surface
|
|
31
62
|
* as a 401 on a key the well-known endpoint had advertised.
|
|
32
63
|
*/
|
|
@@ -41,18 +72,14 @@ export function pubkeyFromValidatedJwk(jwk) {
|
|
|
41
72
|
.toString('hex').toLowerCase();
|
|
42
73
|
} catch { return null; }
|
|
43
74
|
if (!/^[0-9a-f]{64}$/.test(xHex)) return null;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// Compressed SEC1 encoding for the EVEN-y point at this x.
|
|
47
|
-
const point = secp256k1.ProjectivePoint.fromHex('02' + xHex);
|
|
48
|
-
canonicalY = point.toAffine().y.toString(16).padStart(64, '0');
|
|
49
|
-
} catch { return null; }
|
|
75
|
+
const validY = nostrJwkYParities(xHex);
|
|
76
|
+
if (!validY) return null;
|
|
50
77
|
let jwkYHex;
|
|
51
78
|
try {
|
|
52
79
|
jwkYHex = Buffer.from(jwk.y.replace(/-/g, '+').replace(/_/g, '/'), 'base64')
|
|
53
80
|
.toString('hex').toLowerCase();
|
|
54
81
|
} catch { return null; }
|
|
55
|
-
if (jwkYHex
|
|
82
|
+
if (!validY.includes(jwkYHex)) return null;
|
|
56
83
|
return xHex;
|
|
57
84
|
}
|
|
58
85
|
|
|
@@ -99,11 +126,11 @@ export function extractNostrPubkeysFromProfile(profile) {
|
|
|
99
126
|
const xonly = decodeFFormSecp256k1(vm.publicKeyMultibase);
|
|
100
127
|
if (xonly) out.push({ pubkey: xonly, vm });
|
|
101
128
|
} else if (vm.publicKeyJwk && typeof vm.publicKeyJwk === 'object') {
|
|
102
|
-
// Require y to
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
// advertised as authentic.
|
|
129
|
+
// Require y to be a genuine on-curve point for x (either
|
|
130
|
+
// parity, per the did:nostr spec) — the same check the NIP-98
|
|
131
|
+
// verifier applies. Without this, the indexer could publish a
|
|
132
|
+
// JWK that the verifier will then reject, surfacing as a 401 on
|
|
133
|
+
// a key the well-known endpoint had advertised as authentic.
|
|
107
134
|
const xonly = pubkeyFromValidatedJwk(vm.publicKeyJwk);
|
|
108
135
|
if (xonly) out.push({ pubkey: xonly, vm });
|
|
109
136
|
}
|
package/src/auth/nostr.js
CHANGED
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
import { verifyEvent, getEventHash } from '../nostr/event.js';
|
|
29
|
-
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
30
29
|
import crypto from 'crypto';
|
|
31
30
|
import { resolveDidNostrToWebId } from './did-nostr.js';
|
|
32
31
|
// resolveDidNostrLocally is loaded lazily (inside the idpEnabled
|
|
@@ -35,8 +34,8 @@ import { resolveDidNostrToWebId } from './did-nostr.js';
|
|
|
35
34
|
// importing the NIP-98 verifier.
|
|
36
35
|
import { fetchCidDocument } from './cid-doc-fetch.js';
|
|
37
36
|
import { normalizeControllers } from './lws-cid.js'; // shared JSON-LD controller helper
|
|
38
|
-
import { decodeFFormSecp256k1, extractNostrPubkeysFromProfile } from './nostr-keys.js';
|
|
39
|
-
export { extractNostrPubkeysFromProfile };
|
|
37
|
+
import { decodeFFormSecp256k1, extractNostrPubkeysFromProfile, nostrJwkYParities } from './nostr-keys.js';
|
|
38
|
+
export { extractNostrPubkeysFromProfile }; // re-exported for back-compat
|
|
40
39
|
|
|
41
40
|
// NIP-98 event kind (references RFC 7235)
|
|
42
41
|
const HTTP_AUTH_KIND = 27235;
|
|
@@ -659,29 +658,22 @@ function hexToBase64url(hex) {
|
|
|
659
658
|
*
|
|
660
659
|
* EC keys are (x, y) pairs — two distinct valid points share the same
|
|
661
660
|
* x with opposite y parities. Matching on x alone would let an
|
|
662
|
-
* attacker craft a JWK with the target x and a
|
|
663
|
-
* then accept as the user's Nostr key. So we also
|
|
664
|
-
*
|
|
665
|
-
*
|
|
661
|
+
* attacker craft a JWK with the target x and a fabricated, off-curve
|
|
662
|
+
* y, which we'd then accept as the user's Nostr key. So we also
|
|
663
|
+
* require the JWK's y to be a genuine on-curve y for the target x —
|
|
664
|
+
* accepting either parity, since the did:nostr spec allows both 0x02
|
|
665
|
+
* (even) and 0x03 (odd) encodings of the same x-only identity (see
|
|
666
|
+
* `nostrJwkYParities` / issue #571).
|
|
666
667
|
*
|
|
667
668
|
* Returns false if the JWK's coordinates aren't on-curve, can't be
|
|
668
|
-
* decoded, or don't match
|
|
669
|
+
* decoded, or don't match an on-curve point for `targetHex`.
|
|
669
670
|
*/
|
|
670
671
|
function jwkMatchesNostrPubkey(jwk, targetHex, targetB64u) {
|
|
671
672
|
if (typeof jwk.x !== 'string' || typeof jwk.y !== 'string') return false;
|
|
672
673
|
if (jwk.x !== targetB64u) return false;
|
|
673
|
-
//
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
try {
|
|
677
|
-
// Compressed SEC1 point, even-y prefix (0x02) || x.
|
|
678
|
-
const compressed = '02' + targetHex;
|
|
679
|
-
const point = secp256k1.ProjectivePoint.fromHex(compressed);
|
|
680
|
-
const affine = point.toAffine();
|
|
681
|
-
canonicalY = affine.y.toString(16).padStart(64, '0');
|
|
682
|
-
} catch {
|
|
683
|
-
return false;
|
|
684
|
-
}
|
|
674
|
+
// The two genuine on-curve y's (even + odd parity) for the target x.
|
|
675
|
+
const validY = nostrJwkYParities(targetHex);
|
|
676
|
+
if (!validY) return false;
|
|
685
677
|
let jwkYHex;
|
|
686
678
|
try {
|
|
687
679
|
jwkYHex = Buffer.from(jwk.y.replace(/-/g, '+').replace(/_/g, '/'), 'base64')
|
|
@@ -689,7 +681,7 @@ function jwkMatchesNostrPubkey(jwk, targetHex, targetB64u) {
|
|
|
689
681
|
} catch {
|
|
690
682
|
return false;
|
|
691
683
|
}
|
|
692
|
-
return jwkYHex
|
|
684
|
+
return validY.includes(jwkYHex);
|
|
693
685
|
}
|
|
694
686
|
|
|
695
687
|
function isInProofPurpose(profile, predicate, vmId, baseUrl) {
|
|
@@ -3,7 +3,9 @@ import { initializeQuota, checkQuota, updateQuotaUsage } from '../storage/quota.
|
|
|
3
3
|
import { getAllHeaders } from '../ldp/headers.js';
|
|
4
4
|
import { isContainer, getEffectiveUrlPath, getPodName } from '../utils/url.js';
|
|
5
5
|
import { generateProfile, generatePreferences, generateTypeIndex, serialize } from '../webid/profile.js';
|
|
6
|
-
import { generateOwnerAcl, generatePrivateAcl, generateInboxAcl, generatePublicFolderAcl, serializeAcl, relativizeOwnerWebId } from '../wac/parser.js';
|
|
6
|
+
import { generateOwnerAcl, generatePrivateAcl, generateInboxAcl, generatePublicFolderAcl, serializeAcl, relativizeOwnerWebId, AccessMode } from '../wac/parser.js';
|
|
7
|
+
import { checkAccess } from '../wac/checker.js';
|
|
8
|
+
import { buildResourceUrl } from '../auth/middleware.js';
|
|
7
9
|
import { provisionOwnerKey, assertProvisionKeysCompatible } from '../keys/provision.js';
|
|
8
10
|
import { createToken } from '../auth/token.js';
|
|
9
11
|
import { canAcceptInput, toJsonLd, RDF_TYPES } from '../rdf/conneg.js';
|
|
@@ -88,6 +90,43 @@ export async function handlePost(request, reply) {
|
|
|
88
90
|
const newStoragePath = storagePath + filename + (isCreatingContainer ? '/' : '');
|
|
89
91
|
const resourceUrl = `${request.protocol}://${request.hostname}${newUrlPath}`;
|
|
90
92
|
|
|
93
|
+
// Security: a Slug that resolves to an `.acl` sidecar governs ANOTHER
|
|
94
|
+
// resource's permissions — the WAC checker searches for `*.acl`, so an
|
|
95
|
+
// `.acl` written here becomes the authorization policy for its sibling.
|
|
96
|
+
// The authorize() preHandler only checked Append/Write on the *container*
|
|
97
|
+
// (the request path), and its dedicated `.acl` Control guard
|
|
98
|
+
// (authorizeAclAccess) never fires here because the request path is the
|
|
99
|
+
// container, not the resolved sidecar. Without this an agent with mere
|
|
100
|
+
// Append rights on a container could POST `Slug: victim.acl` and self-grant
|
|
101
|
+
// Control on a sibling resource — privilege escalation. `.meta` is not
|
|
102
|
+
// consulted for WAC, but it is a protected Solid sidecar dotfile, so we gate
|
|
103
|
+
// it the same way (defense in depth) rather than let it be minted by Append.
|
|
104
|
+
// Mirror authorizeAclAccess: require acl:Control on the protected resource
|
|
105
|
+
// before minting a sidecar via POST. Build the resource URL with the same
|
|
106
|
+
// buildResourceUrl() the auth middleware uses so this Control decision is
|
|
107
|
+
// evaluated against the identical origin (host+port, subdomain-normalized).
|
|
108
|
+
// noDebit: this is a secondary WAC check on a request the authorize() hook
|
|
109
|
+
// already evaluated (and possibly billed) — pass noDebit so a payment-gated
|
|
110
|
+
// Control grant can't be charged here (no double debit, no silent charge).
|
|
111
|
+
if (!isCreatingContainer && /\.(acl|meta)$/.test(filename)) {
|
|
112
|
+
const protectedUrlPath = newUrlPath.replace(/\.(acl|meta)$/, '');
|
|
113
|
+
const protectedStoragePath = newStoragePath.replace(/\.(acl|meta)$/, '');
|
|
114
|
+
const { allowed } = await checkAccess({
|
|
115
|
+
resourceUrl: buildResourceUrl(request, protectedUrlPath),
|
|
116
|
+
resourcePath: protectedStoragePath,
|
|
117
|
+
isContainer: protectedUrlPath.endsWith('/'),
|
|
118
|
+
agentWebId: request.webId,
|
|
119
|
+
requiredMode: AccessMode.CONTROL,
|
|
120
|
+
noDebit: true
|
|
121
|
+
});
|
|
122
|
+
if (!allowed) {
|
|
123
|
+
return reply.code(403).send({
|
|
124
|
+
error: 'Forbidden',
|
|
125
|
+
message: 'Creating an ACL/meta sidecar via POST requires Control on the protected resource'
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
91
130
|
let success;
|
|
92
131
|
if (isCreatingContainer) {
|
|
93
132
|
success = await storage.createContainer(newStoragePath);
|
package/src/server.js
CHANGED
|
@@ -887,8 +887,11 @@ export function createServer(options = {}) {
|
|
|
887
887
|
// Server-root landing page: seed /index.html and a public-read /.acl
|
|
888
888
|
// on first start (skip-if-exists, so operator-provided files are
|
|
889
889
|
// preserved). See #433 / #276. Skipped in read-only deployments so
|
|
890
|
-
// startup never mutates DATA_ROOT
|
|
891
|
-
|
|
890
|
+
// startup never mutates DATA_ROOT, and in --public mode: WAC is
|
|
891
|
+
// bypassed there so the seeded .acl files would never be consulted,
|
|
892
|
+
// and public mode's serve-a-directory use case (servejss) must not
|
|
893
|
+
// write into the served tree.
|
|
894
|
+
if (!options.readOnly && !options.public) {
|
|
892
895
|
fastify.addHook('onReady', async () => {
|
|
893
896
|
// A missing or unreadable package.json (some production bundles
|
|
894
897
|
// omit it) shouldn't block seeding; fall back to "unknown".
|
package/src/wac/checker.js
CHANGED
|
@@ -16,14 +16,32 @@ import { readLedger, getBalance, debit } from '../webledger.js';
|
|
|
16
16
|
* @param {boolean} options.isContainer - Whether resource is a container
|
|
17
17
|
* @param {string|null} options.agentWebId - WebID of the agent (null for unauthenticated)
|
|
18
18
|
* @param {string} options.requiredMode - Required access mode (from AccessMode)
|
|
19
|
-
* @
|
|
19
|
+
* @param {boolean} [options.noDebit=false] - When true, evaluate a
|
|
20
|
+
* PaymentCondition without charging the ledger. A positive-cost paid grant
|
|
21
|
+
* is treated as not-satisfied (returns paymentRequired) rather than debited.
|
|
22
|
+
* Used by secondary/guard checks (e.g. the POST sidecar Control gate in
|
|
23
|
+
* handlePost) so a single request cannot debit twice or charge silently;
|
|
24
|
+
* the authoritative debit stays in the primary authorize() hook.
|
|
25
|
+
* @returns {Promise<{
|
|
26
|
+
* allowed: boolean,
|
|
27
|
+
* wacAllow: string,
|
|
28
|
+
* paymentRequired?: object|null,
|
|
29
|
+
* paid?: number,
|
|
30
|
+
* balance?: number,
|
|
31
|
+
* currency?: string
|
|
32
|
+
* }>}
|
|
33
|
+
* `paymentRequired` carries the unmet PaymentCondition (present when a paid
|
|
34
|
+
* grant is denied, including every `noDebit` denial). `paid`/`balance`/
|
|
35
|
+
* `currency` are set only when a debit actually occurred. The no-ACL deny
|
|
36
|
+
* path returns just `{allowed, wacAllow}`.
|
|
20
37
|
*/
|
|
21
38
|
export async function checkAccess({
|
|
22
39
|
resourceUrl,
|
|
23
40
|
resourcePath,
|
|
24
41
|
isContainer,
|
|
25
42
|
agentWebId,
|
|
26
|
-
requiredMode
|
|
43
|
+
requiredMode,
|
|
44
|
+
noDebit = false
|
|
27
45
|
}) {
|
|
28
46
|
// Find applicable ACL
|
|
29
47
|
const aclResult = await findApplicableAcl(resourceUrl, resourcePath, isContainer);
|
|
@@ -43,7 +61,8 @@ export async function checkAccess({
|
|
|
43
61
|
resourceUrl, // Use actual resource URL, not the ACL container URL
|
|
44
62
|
agentWebId,
|
|
45
63
|
requiredMode,
|
|
46
|
-
isDefault
|
|
64
|
+
isDefault,
|
|
65
|
+
noDebit
|
|
47
66
|
);
|
|
48
67
|
|
|
49
68
|
// Calculate WAC-Allow header
|
|
@@ -129,7 +148,7 @@ function getParentPath(path) {
|
|
|
129
148
|
// Supported condition types
|
|
130
149
|
const SUPPORTED_CONDITIONS = ['PaymentCondition', 'https://webacl.org/ns#PaymentCondition'];
|
|
131
150
|
|
|
132
|
-
async function checkAuthorizations(authorizations, targetUrl, agentWebId, requiredMode, isDefault) {
|
|
151
|
+
async function checkAuthorizations(authorizations, targetUrl, agentWebId, requiredMode, isDefault, noDebit = false) {
|
|
133
152
|
for (const auth of authorizations) {
|
|
134
153
|
// For default ACLs, check if auth has default rules and matches target
|
|
135
154
|
// For direct ACLs, check if accessTo matches target
|
|
@@ -183,6 +202,11 @@ async function checkAuthorizations(authorizations, targetUrl, agentWebId, requir
|
|
|
183
202
|
// Paid access: check balance and deduct
|
|
184
203
|
const balance = getBalance(ledger, agentWebId, currency);
|
|
185
204
|
if (cost > 0 && balance >= cost) {
|
|
205
|
+
// Guard checks must not charge: a paid grant is left unsatisfied
|
|
206
|
+
// here so billing happens once, in the primary authorize() path.
|
|
207
|
+
if (noDebit) {
|
|
208
|
+
return { allowed: false, paymentRequired: paymentCondition };
|
|
209
|
+
}
|
|
186
210
|
const result = debit(ledger, agentWebId, cost, currency);
|
|
187
211
|
const { writeLedger } = await import('../webledger.js');
|
|
188
212
|
await writeLedger(ledger);
|
package/test/auth.test.js
CHANGED
|
@@ -212,6 +212,60 @@ describe('Authentication', () => {
|
|
|
212
212
|
const res3 = await request('/authuser1/authenticated-only/test.txt', { auth: 'authuser2' });
|
|
213
213
|
assertStatus(res3, 200);
|
|
214
214
|
});
|
|
215
|
+
|
|
216
|
+
it('should deny POST-created .acl/.meta sidecars without Control on the protected resource', async () => {
|
|
217
|
+
// Regression for the POST .acl sidecar injection: an agent holding only
|
|
218
|
+
// acl:Append on a container (here, the public-append inbox) must not be
|
|
219
|
+
// able to plant an .acl sidecar, which the WAC checker would then treat
|
|
220
|
+
// as the authorization policy for the sibling resource. .meta is not a
|
|
221
|
+
// WAC input, but is gated the same way as a protected Solid sidecar.
|
|
222
|
+
await createTestPod('sidecarvictim');
|
|
223
|
+
|
|
224
|
+
const aclBody = JSON.stringify({
|
|
225
|
+
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
|
|
226
|
+
'@graph': []
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Append-only (unauthenticated public append) agent tries to plant victim.acl
|
|
230
|
+
const attackAcl = await request('/sidecarvictim/inbox/', {
|
|
231
|
+
method: 'POST',
|
|
232
|
+
headers: { 'Content-Type': 'application/json', 'Slug': 'victim.acl' },
|
|
233
|
+
body: aclBody
|
|
234
|
+
});
|
|
235
|
+
assertStatus(attackAcl, 403);
|
|
236
|
+
|
|
237
|
+
// The same trick with a .meta sidecar must also be blocked
|
|
238
|
+
const attackMeta = await request('/sidecarvictim/inbox/', {
|
|
239
|
+
method: 'POST',
|
|
240
|
+
headers: { 'Content-Type': 'application/json', 'Slug': 'victim.meta' },
|
|
241
|
+
body: aclBody
|
|
242
|
+
});
|
|
243
|
+
assertStatus(attackMeta, 403);
|
|
244
|
+
|
|
245
|
+
// A normal (non-sidecar) POST to the public inbox still works
|
|
246
|
+
const legit = await request('/sidecarvictim/inbox/', {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
headers: { 'Content-Type': 'application/json', 'Slug': 'note' },
|
|
249
|
+
body: JSON.stringify({ type: 'note' })
|
|
250
|
+
});
|
|
251
|
+
assertStatus(legit, 201);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('should allow the owner (Control) to POST an .acl sidecar', async () => {
|
|
255
|
+
// Owners hold acl:Control, so the sidecar guard must not block them.
|
|
256
|
+
await createTestPod('sidecarowner');
|
|
257
|
+
|
|
258
|
+
const res = await request('/sidecarowner/', {
|
|
259
|
+
method: 'POST',
|
|
260
|
+
headers: { 'Content-Type': 'application/json', 'Slug': 'owned.acl' },
|
|
261
|
+
body: JSON.stringify({
|
|
262
|
+
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
|
|
263
|
+
'@graph': []
|
|
264
|
+
}),
|
|
265
|
+
auth: 'sidecarowner'
|
|
266
|
+
});
|
|
267
|
+
assertStatus(res, 201);
|
|
268
|
+
});
|
|
215
269
|
});
|
|
216
270
|
|
|
217
271
|
describe('WAC-Allow Header', () => {
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parity consistency across the two Nostr verification-method decoders
|
|
3
|
+
* (#571).
|
|
4
|
+
*
|
|
5
|
+
* The did:nostr spec (https://nostrcg.github.io/did-nostr/) allows BOTH
|
|
6
|
+
* parity prefixes for the same x-only identity: "Nostr applications may
|
|
7
|
+
* generate keys with either 0x02 or 0x03 prefixes." So:
|
|
8
|
+
*
|
|
9
|
+
* - the f-form Multikey decoder accepts 02 and 03 (parity discarded,
|
|
10
|
+
* x is the identity), and
|
|
11
|
+
* - the JWK path accepts an even-Y *or* odd-Y on-curve point,
|
|
12
|
+
*
|
|
13
|
+
* but both still reject a fabricated/off-curve y. These tests pin that
|
|
14
|
+
* the two paths agree.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { describe, it } from 'node:test';
|
|
18
|
+
import assert from 'node:assert';
|
|
19
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
20
|
+
import {
|
|
21
|
+
decodeFFormSecp256k1,
|
|
22
|
+
pubkeyFromValidatedJwk,
|
|
23
|
+
nostrJwkYParities,
|
|
24
|
+
extractNostrPubkeysFromProfile,
|
|
25
|
+
} from '../src/auth/nostr-keys.js';
|
|
26
|
+
|
|
27
|
+
const b64u = (hex) => Buffer.from(hex, 'hex').toString('base64')
|
|
28
|
+
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
29
|
+
|
|
30
|
+
/** A real x-only Nostr pubkey (x with a valid even-Y point). */
|
|
31
|
+
const X = '124c0fa99407182ece5a24fad9b7f6674902fc422843d3128d38a0afbee0fdd2';
|
|
32
|
+
|
|
33
|
+
function jwkFor(xHex, parity) {
|
|
34
|
+
const point = secp256k1.ProjectivePoint.fromHex(parity + xHex).toAffine();
|
|
35
|
+
return {
|
|
36
|
+
kty: 'EC', crv: 'secp256k1', alg: 'ES256K',
|
|
37
|
+
x: b64u(point.x.toString(16).padStart(64, '0')),
|
|
38
|
+
y: b64u(point.y.toString(16).padStart(64, '0')),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const fform = (xHex, parity) => 'f' + 'e701' + parity + xHex.toLowerCase();
|
|
43
|
+
|
|
44
|
+
describe('Nostr key parity consistency (#571)', () => {
|
|
45
|
+
it('f-form Multikey decoder accepts both 02 and 03, returning the same x', () => {
|
|
46
|
+
assert.strictEqual(decodeFFormSecp256k1(fform(X, '02')), X);
|
|
47
|
+
assert.strictEqual(decodeFFormSecp256k1(fform(X, '03')), X);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('f-form decoder rejects a non-parity prefix byte', () => {
|
|
51
|
+
assert.strictEqual(decodeFFormSecp256k1(fform(X, '04')), null);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('JWK path accepts an even-Y point and returns x', () => {
|
|
55
|
+
assert.strictEqual(pubkeyFromValidatedJwk(jwkFor(X, '02')), X);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('JWK path now also accepts an odd-Y point and returns the same x', () => {
|
|
59
|
+
assert.strictEqual(pubkeyFromValidatedJwk(jwkFor(X, '03')), X);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('JWK path still rejects a fabricated off-curve y', () => {
|
|
63
|
+
const bad = jwkFor(X, '02');
|
|
64
|
+
// Flip the low bit of y → no longer the genuine even-Y coordinate
|
|
65
|
+
// (and not the odd-Y one either), so it must be rejected.
|
|
66
|
+
const yHex = Buffer.from(bad.y.replace(/-/g, '+').replace(/_/g, '/'), 'base64')
|
|
67
|
+
.toString('hex');
|
|
68
|
+
const flipped = (BigInt('0x' + yHex) ^ 1n).toString(16).padStart(64, '0');
|
|
69
|
+
bad.y = b64u(flipped);
|
|
70
|
+
assert.strictEqual(pubkeyFromValidatedJwk(bad), null);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('nostrJwkYParities returns both genuine y values and null for a bad x', () => {
|
|
74
|
+
const ys = nostrJwkYParities(X);
|
|
75
|
+
assert.ok(Array.isArray(ys) && ys.length === 2);
|
|
76
|
+
assert.notStrictEqual(ys[0], ys[1]);
|
|
77
|
+
assert.strictEqual(nostrJwkYParities('zz'), null);
|
|
78
|
+
assert.strictEqual(nostrJwkYParities(42), null);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('nostrJwkYParities normalizes uppercase hex (matches the lowercase result)', () => {
|
|
82
|
+
assert.deepStrictEqual(nostrJwkYParities(X.toUpperCase()), nostrJwkYParities(X));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('profile extraction maps a 03 Multikey and a 03 JWK to the same identity', () => {
|
|
86
|
+
const profile = {
|
|
87
|
+
verificationMethod: [
|
|
88
|
+
{ id: '#mk', type: 'Multikey', publicKeyMultibase: fform(X, '03') },
|
|
89
|
+
{ id: '#jwk', type: 'JsonWebKey', publicKeyJwk: jwkFor(X, '03') },
|
|
90
|
+
],
|
|
91
|
+
};
|
|
92
|
+
const found = extractNostrPubkeysFromProfile(profile);
|
|
93
|
+
assert.strictEqual(found.length, 2);
|
|
94
|
+
assert.ok(found.every((e) => e.pubkey === X));
|
|
95
|
+
});
|
|
96
|
+
});
|
package/test/server-root.test.js
CHANGED
|
@@ -103,6 +103,56 @@ describe('Server-root landing — operator override', () => {
|
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
105
|
|
|
106
|
+
describe('Server-root landing — public mode skips seeding', () => {
|
|
107
|
+
let server;
|
|
108
|
+
let baseUrl;
|
|
109
|
+
let savedDataRoot;
|
|
110
|
+
const DATA_DIR = './test-data-server-root-public';
|
|
111
|
+
|
|
112
|
+
before(async () => {
|
|
113
|
+
savedDataRoot = process.env.DATA_ROOT;
|
|
114
|
+
|
|
115
|
+
await fs.remove(DATA_DIR);
|
|
116
|
+
await fs.ensureDir(DATA_DIR);
|
|
117
|
+
await fs.writeFile(`${DATA_DIR}/hello.txt`, 'hello');
|
|
118
|
+
|
|
119
|
+
server = createServer({
|
|
120
|
+
logger: false,
|
|
121
|
+
root: DATA_DIR,
|
|
122
|
+
public: true,
|
|
123
|
+
forceCloseConnections: true,
|
|
124
|
+
});
|
|
125
|
+
await server.listen({ port: 0, host: '127.0.0.1' });
|
|
126
|
+
baseUrl = `http://127.0.0.1:${server.server.address().port}`;
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
after(async () => {
|
|
130
|
+
await server.close();
|
|
131
|
+
await fs.remove(DATA_DIR);
|
|
132
|
+
if (savedDataRoot === undefined) delete process.env.DATA_ROOT;
|
|
133
|
+
else process.env.DATA_ROOT = savedDataRoot;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('does not seed /index.html, /.acl, or /index.html.acl into the served tree', async () => {
|
|
137
|
+
// WAC is bypassed in public mode, so the seeded ACLs would never be
|
|
138
|
+
// consulted — and serve-a-directory consumers (servejss) must not
|
|
139
|
+
// have startup write into the directory they serve.
|
|
140
|
+
assert.strictEqual(await fs.pathExists(`${DATA_DIR}/index.html`), false);
|
|
141
|
+
assert.strictEqual(await fs.pathExists(`${DATA_DIR}/.acl`), false);
|
|
142
|
+
assert.strictEqual(await fs.pathExists(`${DATA_DIR}/index.html.acl`), false);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('GET / serves the container listing, and files remain readable', async () => {
|
|
146
|
+
const res = await fetch(`${baseUrl}/`);
|
|
147
|
+
assert.strictEqual(res.status, 200);
|
|
148
|
+
assert.match(res.headers.get('content-type'), /application\/ld\+json/);
|
|
149
|
+
|
|
150
|
+
const file = await fetch(`${baseUrl}/hello.txt`);
|
|
151
|
+
assert.strictEqual(file.status, 200);
|
|
152
|
+
assert.strictEqual(await file.text(), 'hello');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
106
156
|
describe('renderServerRoot', () => {
|
|
107
157
|
// The seeded HTML is fully static — no template substitution, no
|
|
108
158
|
// values vary by request. This is deliberate: anything dynamic
|
package/test/wac.test.js
CHANGED
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
relativizeOwnerWebId
|
|
26
26
|
} from '../src/wac/parser.js';
|
|
27
27
|
import { checkAccess, getRequiredMode } from '../src/wac/checker.js';
|
|
28
|
+
import * as storage from '../src/storage/filesystem.js';
|
|
29
|
+
import { createLedger, setBalance, getBalance, LEDGER_PATH } from '../src/webledger.js';
|
|
28
30
|
|
|
29
31
|
describe('WAC Parser', () => {
|
|
30
32
|
describe('parseAcl', () => {
|
|
@@ -713,3 +715,76 @@ describe('WAC Conditions', () => {
|
|
|
713
715
|
});
|
|
714
716
|
});
|
|
715
717
|
});
|
|
718
|
+
|
|
719
|
+
describe('WAC PaymentCondition noDebit (secondary/guard checks must not charge)', () => {
|
|
720
|
+
let baseUrl;
|
|
721
|
+
const AGENT = 'https://payer.example/profile/card#me';
|
|
722
|
+
const COST = 5;
|
|
723
|
+
const RESOURCE_PATH = '/paygate/resource';
|
|
724
|
+
|
|
725
|
+
before(async () => {
|
|
726
|
+
const result = await startTestServer();
|
|
727
|
+
baseUrl = result.baseUrl;
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
after(async () => {
|
|
731
|
+
await stopTestServer();
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
// Seed a ledger balance for AGENT and an ACL granting AGENT Control on the
|
|
735
|
+
// protected resource, gated behind a positive-cost PaymentCondition.
|
|
736
|
+
async function seed(balance) {
|
|
737
|
+
const ledger = createLedger();
|
|
738
|
+
setBalance(ledger, AGENT, balance, 'sat');
|
|
739
|
+
await storage.write(LEDGER_PATH, Buffer.from(JSON.stringify(ledger)));
|
|
740
|
+
|
|
741
|
+
const resourceUrl = `${baseUrl}${RESOURCE_PATH}`;
|
|
742
|
+
const acl = {
|
|
743
|
+
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
|
|
744
|
+
'@graph': [{
|
|
745
|
+
'@id': '#paid',
|
|
746
|
+
'@type': 'acl:Authorization',
|
|
747
|
+
'acl:agent': { '@id': AGENT },
|
|
748
|
+
'acl:accessTo': { '@id': resourceUrl },
|
|
749
|
+
'acl:mode': [{ '@id': 'acl:Control' }],
|
|
750
|
+
'acl:condition': { '@type': 'PaymentCondition', amount: String(COST), currency: 'sat' }
|
|
751
|
+
}]
|
|
752
|
+
};
|
|
753
|
+
await storage.write(`${RESOURCE_PATH}.acl`, Buffer.from(JSON.stringify(acl)));
|
|
754
|
+
return resourceUrl;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async function balanceNow() {
|
|
758
|
+
const raw = await storage.read(LEDGER_PATH);
|
|
759
|
+
return getBalance(JSON.parse(raw.toString()), AGENT, 'sat');
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
it('debits the ledger on a normal (primary) Control check', async () => {
|
|
763
|
+
const resourceUrl = await seed(100);
|
|
764
|
+
const res = await checkAccess({
|
|
765
|
+
resourceUrl,
|
|
766
|
+
resourcePath: RESOURCE_PATH,
|
|
767
|
+
isContainer: false,
|
|
768
|
+
agentWebId: AGENT,
|
|
769
|
+
requiredMode: AccessMode.CONTROL
|
|
770
|
+
});
|
|
771
|
+
assert.strictEqual(res.allowed, true);
|
|
772
|
+
assert.strictEqual(res.paid, COST);
|
|
773
|
+
assert.strictEqual(await balanceNow(), 100 - COST, 'primary check should debit');
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
it('does NOT debit when noDebit is set (guard/secondary check)', async () => {
|
|
777
|
+
const resourceUrl = await seed(100);
|
|
778
|
+
const res = await checkAccess({
|
|
779
|
+
resourceUrl,
|
|
780
|
+
resourcePath: RESOURCE_PATH,
|
|
781
|
+
isContainer: false,
|
|
782
|
+
agentWebId: AGENT,
|
|
783
|
+
requiredMode: AccessMode.CONTROL,
|
|
784
|
+
noDebit: true
|
|
785
|
+
});
|
|
786
|
+
assert.strictEqual(res.allowed, false, 'paid grant is not satisfied without charging');
|
|
787
|
+
assert.ok(res.paymentRequired, 'should surface paymentRequired instead of debiting');
|
|
788
|
+
assert.strictEqual(await balanceNow(), 100, 'balance must be unchanged');
|
|
789
|
+
});
|
|
790
|
+
});
|