buzz icon indicating copy to clipboard operation
buzz copied to clipboard

Self-referential p tag is stripped at signing: channels remove-member/add-member targeting your own key always fails with 400 missing p tag

Open LarsKlawitter opened this issue 3 months ago • 4 comments

Summary

A p tag whose value equals the signing key's own pubkey is dropped between the SDK builder and the wire. The builder constructs it correctly; the signed event that reaches the relay does not contain it.

This makes every self-targeted channel-membership operation impossible to express:

  • buzz channels remove-member --pubkey <your own key> → relay replies 400 invalid: missing p tag
  • buzz channels add-member --pubkey <your own key> --role <r> → same shape (self-add is explicitly permitted relay-side, so this path is unreachable in practice)

The relay is behaving correctly — extract_p_tag returns None because the event genuinely has no p tag. The loss happens client-side.

Confirmed on main @ 7fc0cc82, and reproduced with a clean rebuild of buzz-cli from that commit (byte-identical behaviour), so this is not a stale-binary artifact.

Reproduction

Point BUZZ_RELAY_URL at any local HTTP listener that logs the POST body — submit_event POSTs the signed event JSON to {relay_url}/events, so the exact bytes are directly observable without a relay.

BUZZ_RELAY_URL=http://127.0.0.1:9999 \
BUZZ_PRIVATE_KEY=<key K> \
  buzz channels remove-member --channel <uuid> --pubkey <pubkey of K>   # self
BUZZ_RELAY_URL=http://127.0.0.1:9999 \
BUZZ_PRIVATE_KEY=<key K> \
  buzz channels remove-member --channel <uuid> --pubkey <some other key>  # non-self

Captured tags arrays, same command, only the target differs:

kind 9001, target = SOME OTHER KEY
  "tags":[["h","<uuid>"],["p","<other pubkey>"]]          <- p tag present

kind 9001, target = THE SIGNER'S OWN KEY
  "tags":[["h","<uuid>"]]                                 <- p tag absent

Same pattern on kind 9000:

kind 9000, target = SOME OTHER KEY
  "tags":[["h","<uuid>"],["p","<other pubkey>"],["role","member"]]
kind 9000, target = THE SIGNER'S OWN KEY
  "tags":[["h","<uuid>"],["role","member"]]               <- p tag absent

Why the tests don't catch it

crates/buzz-sdk/src/builders.rs:582-592 build_remove_member does construct both tags:

let tags = vec![
    tag(&["h", &channel_id.to_string()])?,
    tag(&["p", &target_pubkey.to_ascii_lowercase()])?,
];
Ok(EventBuilder::new(Kind::Custom(9001), "").tags(tags))

Its unit test remove_member_happy_path (builders.rs:2377-2383) asserts has_tag(&ev, "p", pubkey) and passes — but only because the target pubkey it uses is not the key it signs with. The self-referential case is untested, and that is the only case that fails.

I have not pinned the exact stripping site. BuzzClient::sign_event (crates/buzz-cli/src/client.rs:588-613) only injects the optional auth tag and counts auth tags, so the drop appears to be inside EventBuilder::sign_with_keys. Reporting the observable rather than guessing the internals.

Impact

Any actor removing itself from a channel via the CLI is blocked. That includes the legitimate "an agent/identity should relinquish standing capability in a channel it no longer belongs in" case, which is exactly the ownership-hygiene operation #2928 discusses.

Workaround

buzz channels leave --channel <id> (NIP-29 kind 9022). It carries only the h tag so nothing can be stripped, and the relay's own last-owner guard (side_effects.rs:654-671) still protects against orphaning the channel. Note it is a self-only primitive, so it does not cover the general case.

Suggested fix

Either preserve self-referential p tags through signing, or — if the strip is deliberate upstream behaviour — have build_remove_member / build_add_member detect target == signer and fail client-side with a clear message pointing at channels leave, rather than emitting an event the relay must reject.

Adding a signer-is-target case to the builder tests would catch regressions here.

Related but distinct

#3032 (threaded replies omit the parent author's p tag) is the same symptom class but a different mechanism: there the p tag is never constructed, being sourced only from explicit @mentions. Here the tag is constructed and then lost.

LarsKlawitter avatar Aug 02 '26 13:08 LarsKlawitter

Opened #4338 with a targeted fix.

Root cause confirmed: nostr 0.44's EventBuilder::build explicitly removes p tags matching the author's public key by default (see nostr-event/builder.rs:430 — "By default, this method removes any p tags that match the author's public key. To allow self-tagging, call EventBuilder::allow_self_tagging first.").

The SDK builders constructed the p tag correctly, but sign_with_keys stripped it when target_pubkey == signer. The existing happy-path tests only covered non-self targets, which is why this shipped undetected.

Fix: Chain .allow_self_tagging() on both build_add_member (kind 9000) and build_remove_member (kind 9001), matching the existing convention in NIP-IA builders (builders.rs:1816,1837).

Regression tests: Added two tests that sign with the same key as the target pubkey and assert the p tag survives — closing the test gap you identified.

iroiro147 avatar Aug 02 '26 14:08 iroiro147

Confirmed from the reporting side — nostr-event/builder.rs:430 matches the captured behaviour exactly: the tag is present in the builder output and absent from the signed event, only when target == signer, on both kind 9000 and 9001. That was the one link I hadn't pinned, thank you.

Also verified against main @ 7fc0cc82 that the NIP-IA builders already chain .allow_self_tagging() (builders.rs around 1800/1821 at that pin), so #4338 is consistent with the existing in-repo convention rather than introducing a new pattern, and the regression tests signing with the target's own key close precisely the gap that let this ship.

Happy to re-run the local capture against a build of #4338 if that's useful for review.

LarsKlawitter avatar Aug 02 '26 17:08 LarsKlawitter

This also breaks a visible control in Desktop, which may be the easiest reproduction to date and needs no CLI:

  1. Open any task in a project.
  2. Click Assign to me — nothing happens.
  3. Click Unassign on yourself — nothing happens either.

Both report success and add a comment to the task history ("Assigned this issue to …", "Unassigned … from this task"), while the Assignees row stays unchanged. Assigning or unassigning anyone else works normally.

Same root cause as described here. Assignment is a kind:1 note whose p tags are the assignees, so when the assignee is the signer the tag is dropped at build time and the reducer adds or removes nobody. Confirmed on the wire: the stored event carried ["e", "a", "t", "prior"] and no p, and re-hashing those tags reproduces the stored event id exactly — so the tag was never signed, rather than stripped later by the relay.

Two paths beyond the builders already covered by #4975 and the open PRs:

  • build_git_issue_assignee_operation in buzz-sdk (assign/unassign)
  • build_labeled_recipient_note_event in Desktop, which also covers review requests

And one that looks like the reason this keeps coming back: the sign_event Tauri command builds an EventBuilder from caller-supplied tags without allow_self_tagging, so any TypeScript caller that composes a self p tag silently loses it. Every new builder inherits the same trap.

Opened #6516 with a fix for all three, plus regression tests mirroring the existing message_preserves_self_mention_p_tag.

One consequence worth noting for anyone testing a fix: events written before it are not repaired. The assignee list is derived by replaying those notes, and they genuinely carry no assignee, so an affected task needs reassigning once.

sumit-m avatar Aug 21 '26 20:08 sumit-m

@sumit-m Your reproduction found a third builder with this defect, and it was not covered by #4338 until now. Thank you — a visible Desktop control that reports success and silently does nothing is a far better repro than the CLI path, and it is the one that would have shipped.

Confirmed and fixed. #4338 had applied .allow_self_tagging() to the kind:9000 and 9001 member builders. Assignment is a different builder — build_git_issue_assignee_operation in crates/buzz-sdk/src/builders.rs — which returned:

Ok(EventBuilder::new(Kind::Custom(1), content).tags(tags))

Same root cause exactly as you diagnosed: an assignment note's p tags are the assignees, so a self-assignment has target == signer, and nostr 0.44 strips matching p tags by default. The event signs and publishes fine, which is why the history comment appears — only the tag is missing, so the Assignees row never changes. Assigning anyone else works because target != signer.

Pushed as e1770281a, with self_assignment_preserves_p_tag pinning it. I checked the test actually catches the bug rather than assuming: with .allow_self_tagging() removed it fails, with it present it passes. 192 builder tests green.

Two notes on the update:

  • The branch was 449 commits behind main, and build_git_issue_assignee_operation did not exist when this PR was opened — so this was not an oversight in the original fix so much as a builder that arrived afterwards with the same latent bug. Merged current main as part of the change.
  • I deliberately did not touch build_note, the other kind:1 builder. Its p tags are recipients rather than subjects, so a self-p there is a genuinely different case and I did not want to widen the blast radius on the strength of this bug.

If you get a chance to run the Desktop control against the branch, that would close the loop better than my unit test can — the test proves the builder emits the tag, not that the row updates.

iroiro147 avatar Aug 27 '26 02:08 iroiro147