Merge branch 'master' into live

This commit is contained in:
raucao committed 2026-11-18 20:37:45 +04:00
commit 04fc98ec61
12 files changed
+243 -43

No files matched your search

+1 -1
View File
@@ -108,7 +108,7 @@ class SettingsController < ApplicationController
def set_nostr_pubkey
signed_event = Nostr::Event.new(**nostr_event_from_params)
is_valid_sig = signed_event.verify_signature
is_valid_sig = NostrManager::VerifySignedEvent.call(event: signed_event)
is_valid_auth = NostrManager::VerifyAuth.call(
event: signed_event,
challenge: session[:shared_secret]
+1 -1
View File
@@ -23,7 +23,7 @@ class Users::SessionsController < Devise::SessionsController
def nostr_login
signed_event = Nostr::Event.new(**nostr_event_from_params)
is_valid_sig = signed_event.verify_signature
is_valid_sig = NostrManager::VerifySignedEvent.call(event: signed_event)
is_valid_auth = NostrManager::VerifyAuth.call(
event: signed_event,
challenge: session[:shared_secret]
+6 -2
View File
@@ -1,5 +1,7 @@
module NostrManager
class VerifyAuth < NostrManagerService
AUTH_KIND = 22242
def initialize(event:, challenge:)
@event = event
@challenge_expected = challenge
@@ -7,9 +9,11 @@ module NostrManager
end
def call
return false unless @event.kind == AUTH_KIND
tags = parse_tags(@event.tags)
site_given = tags[:site].first
challenge_given = tags[:challenge].first
site_given = tags[:site]&.first
challenge_given = tags[:challenge]&.first
site_given == @site_expected &&
challenge_given == @challenge_expected
@@ -0,0 +1,29 @@
module NostrManager
class VerifySignedEvent < NostrManagerService
def initialize(event:, allowed_kinds: nil)
@event = event
@allowed_kinds = allowed_kinds
end
def call
return false if @event.id.blank? || @event.pubkey.blank? || @event.sig.blank?
return false unless event_id_matches?
return false unless signature_valid?
return false if @allowed_kinds.present? && !@allowed_kinds.include?(@event.kind)
true
end
private
def signature_valid?
@event.verify_signature
rescue StandardError
false
end
def event_id_matches?
Digest::SHA256.hexdigest(JSON.dump(@event.serialize)) == @event.id
end
end
end
+6
View File
@@ -7,6 +7,12 @@ akkounts, so that notes published to the relay have to be authored by (or in
some cases just related to) local users who have verified their Nostr public
key.
It holds both Deno policies (the members relay's write policy, the member
bridge, and the manual sync tool — described below) and plain Node ESM policies
for the public submission relay and the approved-content sync. See
`extras/strfry/README.md` for details on the policies and their environment
variables.
### Requirements
[Deno](https://deno.com/) needs to be installed on the machine that you run
+60 -36
View File
@@ -1,15 +1,24 @@
# strfry policies
Write-policy plugins for [strfry][strfry] relays. Most are plain Node ESM
(`.mjs`, Node 22 LTS); the member-bridge policy (`ldap-only-policy.ts`) is a
Deno script. They are fetched by Chef from this directory into `/opt/strfry` on
the relay hosts.
Policy plugins for the Kosmos [strfry][strfry] relays. There are two runtimes in
this directory:
The legacy Deno policies (`strfry-policy.ts`, `ldap-policy.ts`,
`strfry-sync.ts`) are still deployed for `nostr.kosmos.org` and power that
relay's own write policy plus its historical member sync. `ldap-only-policy.ts`
(below) reuses their `ldap-policy.ts` module. See `docs/dev/nostr.md` for the
legacy ones.
- **Deno** — the members relay's write policy and member bridge, plus the manual
member-note sync. Dependencies are pinned in `deno.json`/`deno.lock`. See
`docs/dev/nostr.md` for the Deno setup.
- `strfry-policy.ts` — the relay's own write policy.
- `ldap-only-policy.ts` — the member bridge's `pluginDown`.
- `ldap-policy.ts` — shared LDAP membership module used by both of the above.
- `strfry-sync.ts` — manual member-note sync tool.
- **Node** (plain ESM `.mjs`, Node 24 LTS, no dependencies) — the submission
relay and the approved-content sync.
- `submission-policy.mjs`
- `sync-policy.mjs`
In development the whole directory is mounted at `/opt/strfry` (see the
`strfry` service in `docker-compose.yml`, and `docker/strfry/strfry.conf` for
the write-policy wiring); in production the ops tooling deploys it to the relay
hosts.
## How strfry invokes a policy
@@ -35,9 +44,8 @@ go to **stderr** and end up in the strfry log.
## `submission-policy.mjs`
Write policy for the **public submission relay** (`submit.nostr.kosmos.org`).
Open to all authors — there is **no LDAP/membership check**. It applies only the
"usual gates":
Write policy for the **public submission relay**. Open to all authors — there is
**no LDAP/membership check**. It applies only the "usual gates":
1. `type` must be `new`.
2. Event must be well-formed (`pubkey`, integer `kind`, `tags` array).
@@ -69,7 +77,7 @@ on file mtime change or config change).
| `SUBMISSION_DEDUPE_WINDOW_MS` | `60000` | Dedupe window. |
| `SUBMISSION_DEDUPE_MIN_LENGTH` | `50` | Minimum content length that is deduped. |
### Chef / strfry configuration
### strfry configuration
```ini
relay {
@@ -81,8 +89,8 @@ relay {
## `sync-policy.mjs`
Write policy for the **approved-content sync** config on strfry-1
(`/etc/strfry-sync.conf`, same LMDB database as the production relay). It is
Write policy for the **approved-content sync** on the members relay. It runs
under a separate strfry config that shares the relay's database, and it is
**fail-closed** and does **source verification only**; all content gating
(kinds, sizes, rates, …) is handled by the other policies. An event is accepted
only when *all* of the following hold:
@@ -99,11 +107,9 @@ policy rejects every event.
| Variable | Default | Description |
| --- | --- | --- |
| `STAGING_RELAY_URL` | _(unset)_ | Expected `sourceInfo` for synced events, e.g. `ws://10.1.1.183:7779`. Unset/empty rejects everything. Whitespace around the configured value is trimmed; `sourceInfo` is compared as-is. |
| `STAGING_RELAY_URL` | _(unset)_ | Expected `sourceInfo` for synced events, e.g. `ws://staging-relay:7779`. Unset/empty rejects everything. Whitespace around the configured value is trimmed; `sourceInfo` is compared as-is. |
### Chef / strfry configuration
`/etc/strfry-sync.conf`:
### strfry configuration
```ini
relay {
@@ -113,44 +119,62 @@ relay {
}
```
The matching sync is driven by `strfry --config /etc/strfry-sync.conf sync
ws://<nuc-zt-ip>:7779 --dir down`, which sets `sourceType=Sync` and
The matching sync is driven by `strfry --config <sync-config> sync
ws://<staging-relay>:7779 --dir down`, which sets `sourceType=Sync` and
`sourceInfo` to the relay URL passed on the command line. `STAGING_RELAY_URL`
must be that exact URL.
## `strfry-policy.ts`
Deno write policy for the members relay itself. It combines the hellthread cap,
anti-duplication (via Deno KV with a 60s window), and the shared `LdapPolicy`
membership check, but lets NIP-46 (kind `24133`) ephemeral events through from
anyone. See `docs/dev/nostr.md` for the Deno requirements and configuration.
## `ldap-only-policy.ts`
Deno write policy for the production relay's **member bridge** — a `strfry
router` stream group (`dir = "down"`) that subscribes to the local submission
relay (`ws://localhost:7778`) and writes member-authored events into the
production database. It is this stream group's `pluginDown`, and it accepts an
event **only** when its author is an LDAP member (or a whitelisted pubkey).
Deno write policy for the members relay's **member bridge** — a `strfry router`
stream group (`dir = "down"`) that subscribes to the local submission relay and
writes member-authored events into the members relay's database. It is this
stream group's `pluginDown`, and it accepts an event **only** when its author is
an LDAP member (or a whitelisted pubkey).
It reuses `ldap-policy.ts` and the shared `/opt/strfry/.env` (the same read-only
LDAP service account as `strfry-policy.ts`), but deliberately does **no**
It reuses `ldap-policy.ts` and the shared `.env` (the same read-only LDAP
service account as `strfry-policy.ts`), but deliberately does **no**
hellthread/anti-duplication work and opens **no** Deno KV store — so it can run
alongside the relay's own policy process without shared state.
### Chef / strfry configuration
`/etc/strfry-member-bridge.conf`:
### strfry configuration
```ini
streams {
members {
dir = "down"
filter = { "kinds": [0, 3, 5, 360, 30360] }
urls = [ "ws://localhost:7778" ]
urls = [ "ws://<submission-relay>:7778" ]
pluginDown = "/opt/strfry/ldap-only-policy.ts"
}
}
```
driven by `strfry router /etc/strfry-member-bridge.conf`. `dir = "down"` keeps
it strictly one-way (the router never watches or forwards the local DB). The
driven by `strfry router <member-bridge-config>`. `dir = "down"` keeps it
strictly one-way (the router never watches or forwards the local DB). The
`filter` bounds the LDAP lookups to the submission kinds; membership is decided
per event by the policy, so no member list is maintained.
## `ldap-policy.ts`
Shared Deno module exporting `LdapConfig` and the `LdapPolicy` class: it looks
up the event author's `nostrKey` in LDAP (with a pubkey whitelist and special
handling for zap receipts) and rejects non-members. Imported by both
`strfry-policy.ts` and `ldap-only-policy.ts`.
## `strfry-sync.ts`
Deno script for one-off/manual syncs of all notes authored by local users (any
account that has verified its Nostr pubkey with akkounts) from a remote relay via
negentropy sync. See the "Manual tasks" section in `docs/dev/nostr.md`.
## Tests
Unit tests use the built-in `node:test` runner (no dependencies):
@@ -167,8 +191,8 @@ printf '%s\n' \
| ./submission-policy.mjs
printf '%s\n' \
'{"type":"new","sourceType":"Sync","sourceInfo":"ws://10.1.1.183:7779","event":{"id":"a","pubkey":"b","kind":360,"tags":[],"content":"hi"}}' \
| STAGING_RELAY_URL='ws://10.1.1.183:7779' ./sync-policy.mjs
'{"type":"new","sourceType":"Sync","sourceInfo":"ws://staging-relay:7779","event":{"id":"a","pubkey":"b","kind":360,"tags":[],"content":"hi"}}' \
| STAGING_RELAY_URL='ws://staging-relay:7779' ./sync-policy.mjs
```
[strfry]: https://github.com/hoytech/strfry
+1 -1
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env node
// Fail-closed strfry write policy for the strfry-1 approved-content sync.
// Fail-closed strfry write policy for the members relay's approved-content sync.
//
// This policy only accepts events that arrived through `strfry sync` from the
// dedicated staging relay. Everything else is rejected, including events with
+1 -1
View File
@@ -4,7 +4,7 @@ import { Readable, Writable } from 'node:stream';
import { createState, decide, run } from '../sync-policy.mjs';
const STAGING = 'ws://10.1.1.183:7779';
const STAGING = 'ws://staging-relay:7779';
function syncMsg(overrides = {}) {
const { event: eventOverrides, ...rest } = overrides;
+1 -1
View File
@@ -11,7 +11,7 @@
"postcss-preset-env": "^7.8.3",
"tailwindcss": "^3.4.0"
},
"version": "0.12.0",
"version": "0.13.0",
"scripts": {
"build:css:tailwind": "bun ./node_modules/tailwindcss/lib/cli.js --postcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css",
"build:css": "bun run build:css:tailwind"
+23
View File
@@ -127,5 +127,28 @@ RSpec.describe "Settings", type: :request do
expect(flash[:alert]).to eq("Public key could not be verified")
end
end
context "With a forged event body" do
before do
expect(LdapManager::UpdateNostrKey).not_to receive(:call)
forged_event = auth_event.merge("content" => "injected")
post set_nostr_pubkey_settings_path,
params: { signed_event: forged_event }.to_json,
headers: {
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json"
}
end
it "returns a 422 status" do
expect(response).to have_http_status(422)
end
it "informs the user about the failure" do
expect(flash[:alert]).to eq("Public key could not be verified")
end
end
end
end
+23
View File
@@ -90,5 +90,28 @@ RSpec.describe "Devise login sessions", type: :request do
expect(flash[:alert]).to eq("Login verification failed")
end
end
context "With a forged event body" do
before do
expect(LdapManager::FetchUserByNostrKey).not_to receive(:call)
forged_event = auth_event.merge("content" => "injected")
post users_nostr_login_path,
params: { signed_event: forged_event }.to_json,
headers: {
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json"
}
end
it "returns an unauthorized status" do
expect(response).to have_http_status(401)
end
it "informs the user about the failure" do
expect(flash[:alert]).to eq("Login verification failed")
end
end
end
end
@@ -0,0 +1,91 @@
require 'rails_helper'
RSpec.describe NostrManager::VerifySignedEvent, type: :model do
let(:keypair) do
Nostr::KeyPair.new(
private_key: Nostr::PrivateKey.new(Setting.nostr_private_key),
public_key: Nostr::PublicKey.new(Setting.nostr_public_key)
)
end
let(:nostr_user) { Nostr::User.new(keypair: keypair) }
let(:event) do
nostr_user.create_event(kind: 22242, content: "", tags: [["challenge", "abc"]])
end
describe "with a correctly signed event" do
it "is accepted" do
expect(described_class.call(event: event)).to be(true)
end
end
describe "with an injected event body but a valid id/sig pair" do
let(:forged) do
Nostr::Event.new(
id: event.id, sig: event.sig, pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: "injected"
)
end
it "still passes the raw signature check" do
expect(forged.verify_signature).to be(true)
end
it "is rejected because the id no longer matches the body" do
expect(described_class.call(event: forged)).to be(false)
end
end
describe "with a tampered signature" do
let(:forged) do
Nostr::Event.new(
id: event.id, sig: "0" * 128, pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: event.content
)
end
it "is rejected" do
expect(described_class.call(event: forged)).to be(false)
end
end
describe "with a malformed signature" do
let(:malformed) do
Nostr::Event.new(
id: event.id, sig: "abc", pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: event.content
)
end
it "is rejected instead of raising" do
expect(described_class.call(event: malformed)).to be(false)
end
end
describe "with missing id, pubkey or signature" do
let(:unsigned) do
Nostr::Event.new(
pubkey: event.pubkey, created_at: event.created_at,
kind: event.kind, tags: [], content: ""
)
end
it "is rejected" do
expect(described_class.call(event: unsigned)).to be(false)
end
end
describe "with an allowed_kinds restriction" do
it "accepts an event of an allowed kind" do
expect(described_class.call(event: event, allowed_kinds: [22242])).to be(true)
end
it "rejects an event of a disallowed kind" do
expect(described_class.call(event: event, allowed_kinds: [0, 10002, 10063])).to be(false)
end
end
end