mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
Fix NIP-07 adapter race condition with lazy encryption capability injection
- Modified `js/nostr/adapters/nip07Adapter.js` to use dynamic getters for `capabilities` (nip04/nip44) and unconditionally expose encryption methods. - Updated `js/nostr/client.js` to use `Object.getOwnPropertyDescriptor` when handling `signer.capabilities`. - Added `tests/nostr/nip07Adapter-race-condition.test.mjs` to verify the fix. - Fixed test environment issues in `tests/nostr/adapters.test.mjs` and `tests/nostr-login-permissions.test.mjs`. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
51924d1619
commit
d38585f376
5 files changed
+121
-7
No files matched your search
@@ -212,7 +212,7 @@ export async function createNip07Adapter(initialExtension) {
|
||||
Object.defineProperty(signer, "capabilities", {
|
||||
get: () => {
|
||||
const ext = getExtension();
|
||||
const hasNip04 = ext?.nip04 && typeof ext.nip04 === "object";
|
||||
const hasNip04 = !!(ext?.nip04 && typeof ext.nip04 === "object");
|
||||
const hasNip44 = !!resolveNip44Module(ext);
|
||||
return {
|
||||
sign: typeof ext?.signEvent === "function",
|
||||
|
||||
+25
-3
@@ -358,7 +358,13 @@ function setActiveSigner(signer) {
|
||||
|
||||
hydrateExtensionSignerCapabilities(signer);
|
||||
attachNipMethodAliases(signer);
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
|
||||
const capsDescriptor = Object.getOwnPropertyDescriptor(signer, "capabilities");
|
||||
const isGetter = capsDescriptor && typeof capsDescriptor.get === "function";
|
||||
|
||||
if (!isGetter) {
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
}
|
||||
|
||||
const pubkey =
|
||||
typeof signer.pubkey === "string" && signer.pubkey.trim()
|
||||
@@ -376,7 +382,15 @@ function getActiveSigner() {
|
||||
hydrateExtensionSignerCapabilities(signer);
|
||||
attachNipMethodAliases(signer);
|
||||
if (signer && typeof signer === "object") {
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
const capsDescriptor = Object.getOwnPropertyDescriptor(
|
||||
signer,
|
||||
"capabilities",
|
||||
);
|
||||
const isGetter = capsDescriptor && typeof capsDescriptor.get === "function";
|
||||
|
||||
if (!isGetter) {
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
}
|
||||
}
|
||||
return signer;
|
||||
}
|
||||
@@ -394,7 +408,15 @@ function resolveActiveSigner(pubkey) {
|
||||
hydrateExtensionSignerCapabilities(signer);
|
||||
attachNipMethodAliases(signer);
|
||||
if (signer && typeof signer === "object") {
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
const capsDescriptor = Object.getOwnPropertyDescriptor(
|
||||
signer,
|
||||
"capabilities",
|
||||
);
|
||||
const isGetter = capsDescriptor && typeof capsDescriptor.get === "function";
|
||||
|
||||
if (!isGetter) {
|
||||
signer.capabilities = resolveSignerCapabilities(signer);
|
||||
}
|
||||
}
|
||||
return signer;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,16 @@ function setupLoginEnvironment({ enableImpl, getPublicKey = HEX_PUBKEY } = {}) {
|
||||
|
||||
windowRef.NostrTools = toolkitStub;
|
||||
global.NostrTools = toolkitStub;
|
||||
global.__BITVID_CANONICAL_NOSTR_TOOLS__ = toolkitStub;
|
||||
// Use Object.defineProperty to override potential read-only global from bootstrap
|
||||
try {
|
||||
Object.defineProperty(global, "__BITVID_CANONICAL_NOSTR_TOOLS__", {
|
||||
value: toolkitStub,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
} catch (e) {
|
||||
global.__BITVID_CANONICAL_NOSTR_TOOLS__ = toolkitStub;
|
||||
}
|
||||
global.nostrToolsReady = Promise.resolve({ ok: true, value: toolkitStub });
|
||||
|
||||
const originalAccessControl = {
|
||||
|
||||
@@ -25,7 +25,15 @@ test("createNsecAdapter wires signing and cipher capabilities", async () => {
|
||||
const canonicalTools = { ...nostrTools };
|
||||
|
||||
try {
|
||||
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = canonicalTools;
|
||||
try {
|
||||
Object.defineProperty(globalThis, "__BITVID_CANONICAL_NOSTR_TOOLS__", {
|
||||
value: canonicalTools,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
} catch (e) {
|
||||
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = canonicalTools;
|
||||
}
|
||||
globalThis.NostrTools = canonicalTools;
|
||||
globalThis.nostrToolsReady = Promise.resolve({
|
||||
ok: true,
|
||||
@@ -55,7 +63,15 @@ test("createNsecAdapter wires signing and cipher capabilities", async () => {
|
||||
if (previousCanonical === undefined) {
|
||||
delete globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__;
|
||||
} else {
|
||||
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = previousCanonical;
|
||||
try {
|
||||
Object.defineProperty(globalThis, "__BITVID_CANONICAL_NOSTR_TOOLS__", {
|
||||
value: previousCanonical,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
} catch (e) {
|
||||
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = previousCanonical;
|
||||
}
|
||||
}
|
||||
|
||||
if (previousNostrTools === undefined) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import assert from 'node:assert';
|
||||
import { test, describe, beforeEach, afterEach } from 'node:test';
|
||||
import { createNip07Adapter } from '../../js/nostr/adapters/nip07Adapter.js';
|
||||
|
||||
describe('Nip07Adapter Race Condition', () => {
|
||||
let originalWindow;
|
||||
|
||||
beforeEach(() => {
|
||||
originalWindow = global.window;
|
||||
global.window = {};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.window = originalWindow;
|
||||
});
|
||||
|
||||
test('should detect capabilities dynamically even if injected late', async () => {
|
||||
// 1. Setup initial window.nostr with NO encryption capabilities
|
||||
global.window.nostr = {
|
||||
getPublicKey: async () => 'pubkey',
|
||||
signEvent: async (e) => e,
|
||||
// nip04 and nip44 are missing initially
|
||||
};
|
||||
|
||||
// 2. Initialize adapter
|
||||
const adapter = await createNip07Adapter();
|
||||
|
||||
// 3. Verify initial state: capabilities should be false
|
||||
assert.strictEqual(adapter.capabilities.nip04, false, 'Should not have nip04 capability initially');
|
||||
assert.strictEqual(adapter.capabilities.nip44, false, 'Should not have nip44 capability initially');
|
||||
|
||||
// 4. Simulate lazy injection of encryption APIs
|
||||
global.window.nostr.nip04 = { encrypt: () => {}, decrypt: () => {} };
|
||||
global.window.nostr.nip44 = { encrypt: () => {}, decrypt: () => {} };
|
||||
|
||||
// 5. Verify dynamic state: capabilities should now be true
|
||||
assert.strictEqual(adapter.capabilities.nip04, true, 'Should detect nip04 capability after injection');
|
||||
assert.strictEqual(adapter.capabilities.nip44, true, 'Should detect nip44 capability after injection');
|
||||
});
|
||||
|
||||
test('should call the injected method even if added late', async () => {
|
||||
// 1. Setup initial window.nostr with NO encryption capabilities
|
||||
global.window.nostr = {
|
||||
getPublicKey: async () => 'pubkey',
|
||||
signEvent: async (e) => e,
|
||||
};
|
||||
|
||||
const adapter = await createNip07Adapter();
|
||||
|
||||
// 2. Call nip04Encrypt - should fail or throw because it's missing
|
||||
await assert.rejects(async () => {
|
||||
await adapter.nip04Encrypt('pubkey', 'message');
|
||||
}, /NIP-04/); // Expect some error related to missing NIP-04
|
||||
|
||||
// 3. Inject
|
||||
let called = false;
|
||||
global.window.nostr.nip04 = {
|
||||
encrypt: async () => { called = true; return 'encrypted'; },
|
||||
decrypt: async () => {}
|
||||
};
|
||||
|
||||
// 4. Call again - should succeed
|
||||
const result = await adapter.nip04Encrypt('pubkey', 'message');
|
||||
assert.strictEqual(result, 'encrypted');
|
||||
assert.strictEqual(called, true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user