From f68a82f7eed8b8075d249e45c087f7ce6193b911 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Mon, 27 Oct 2025 15:38:25 -0400 Subject: [PATCH] Fix comment composer login check for nip07 --- js/app.js | 6 +- tests/app/is-user-logged-in.test.mjs | 91 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 tests/app/is-user-logged-in.test.mjs diff --git a/js/app.js b/js/app.js index 48da3666..a6a5d0f8 100644 --- a/js/app.js +++ b/js/app.js @@ -3494,10 +3494,8 @@ class Application { return false; } - const sessionActorPubkey = this.normalizeHexPubkey( - nostrClient?.sessionActor?.pubkey, - ); - if (sessionActorPubkey && sessionActorPubkey === normalizedPubkey) { + const clientPubkey = this.normalizeHexPubkey(nostrClient?.pubkey); + if (clientPubkey && clientPubkey !== normalizedPubkey) { return false; } diff --git a/tests/app/is-user-logged-in.test.mjs b/tests/app/is-user-logged-in.test.mjs new file mode 100644 index 00000000..2ad3c09b --- /dev/null +++ b/tests/app/is-user-logged-in.test.mjs @@ -0,0 +1,91 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +import { Application } from "../../js/app.js"; +import { nostrClient } from "../../js/nostrClientFacade.js"; + +const HEX64_REGEX = /^[0-9a-f]{64}$/i; + +function createTestApp(pubkey) { + const app = { + _pubkey: pubkey, + normalizeHexPubkey(value) { + if (typeof value !== "string") { + return null; + } + + const trimmed = value.trim(); + if (!trimmed) { + return null; + } + + if (HEX64_REGEX.test(trimmed)) { + return trimmed.toLowerCase(); + } + + return null; + }, + isUserLoggedIn: Application.prototype.isUserLoggedIn, + }; + + Object.defineProperty(app, "pubkey", { + get() { + return this._pubkey; + }, + set(value) { + this._pubkey = value; + }, + }); + + return app; +} + +test("isUserLoggedIn returns false when no user pubkey is set", () => { + const previousPubkey = nostrClient.pubkey; + const previousSessionActor = nostrClient.sessionActor; + + try { + nostrClient.pubkey = null; + nostrClient.sessionActor = null; + + const app = createTestApp(null); + assert.equal(app.isUserLoggedIn(), false); + } finally { + nostrClient.pubkey = previousPubkey; + nostrClient.sessionActor = previousSessionActor; + } +}); + +test("isUserLoggedIn treats extension logins as authenticated", () => { + const previousPubkey = nostrClient.pubkey; + const previousSessionActor = nostrClient.sessionActor; + + try { + const pubkey = "893a31fa5a6481295bb76ea2dd8d5ec70640e6e6ad398e6547d771e8256af11f"; + nostrClient.pubkey = pubkey; + nostrClient.sessionActor = { pubkey }; + + const app = createTestApp(pubkey); + assert.equal(app.isUserLoggedIn(), true); + } finally { + nostrClient.pubkey = previousPubkey; + nostrClient.sessionActor = previousSessionActor; + } +}); + +test("isUserLoggedIn guards against mismatched nostrClient state", () => { + const previousPubkey = nostrClient.pubkey; + const previousSessionActor = nostrClient.sessionActor; + + try { + const pubkey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + nostrClient.pubkey = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + nostrClient.sessionActor = { pubkey }; + + const app = createTestApp(pubkey); + assert.equal(app.isUserLoggedIn(), false); + } finally { + nostrClient.pubkey = previousPubkey; + nostrClient.sessionActor = previousSessionActor; + } +});