Skip to content

Commit 7a2f0a9

Browse files
Address copilot pass 11 on #2
- doctor.js: bail with a clean fail-check when JSON.parse returns null, a primitive, or an array. Previously these would crash downstream on profile['@id'] / profile.controller access and surface as a generic "Diagnostics crashed". - lib/multikey.js: validate that webId is a non-empty string at the buildNostrVerificationMethod entry point. Also harden stripHash to not throw on non-string input — defensive since the helper is exported for standalone use.
1 parent 5029ff5 commit 7a2f0a9

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

doctor.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ async function runAll(webIdUrl) {
153153
});
154154
return result;
155155
}
156+
// JSON.parse accepts null, primitives, and arrays — none of which are
157+
// a usable JSON-LD profile document. Bail out before downstream code
158+
// tries to read `@id`/`controller` and throws.
159+
if (profile === null || typeof profile !== 'object' || Array.isArray(profile)) {
160+
checks.push({
161+
status: 'fail',
162+
label: 'Profile parses as JSON',
163+
detail: `Top-level value is ${profile === null ? 'null' : Array.isArray(profile) ? 'an array' : typeof profile}; expected a JSON object.`,
164+
});
165+
return result;
166+
}
156167
checks.push({ status: 'pass', label: 'Profile parses as JSON' });
157168

158169
// Profile was fetched and parsed — safe to root a snippet against this URL.

lib/multikey.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ export function buildNostrVerificationMethod({
8989
fragment = 'nostr-key-1',
9090
parity = 0x02,
9191
}) {
92+
if (typeof webId !== 'string' || webId.length === 0) {
93+
throw new Error('webId must be a non-empty string');
94+
}
9295
const mb = nostrPubkeyToMultikey(xOnlyHex, { parity });
9396
// The VM id is a fragment URI rooted at the WebID's document. We strip
9497
// any existing fragment so paths like ".../card.jsonld#me" yield
@@ -108,6 +111,7 @@ function normalizeHex(s) {
108111
}
109112

110113
function stripHash(u) {
114+
if (typeof u !== 'string') return u;
111115
try {
112116
const url = new URL(u);
113117
url.hash = '';

0 commit comments

Comments
 (0)