Merge pull request #2493 from vitorpamplona/claude/fix-marmot-interop-tests-3ZSSj

Fix MLS commit cryptography and add comprehensive validation
This commit is contained in:
Vitor Pamplona
2026-04-22 09:42:41 -04:00
committed by GitHub
30 changed files with 1639 additions and 333 deletions
@@ -2263,6 +2263,14 @@ class Account(
/**
* Leave a Marmot MLS group.
* Publishes the SelfRemove proposal and removes local state.
*
* MIP-01/MIP-03: admins MUST first publish a GroupContextExtensions
* commit dropping themselves from `admin_pubkeys` before issuing a
* SelfRemove proposal. Without that, [MlsGroup.selfRemove] throws
* `IllegalStateException("Admin must self-demote via GroupContextExtensions
* before SelfRemove (MIP-01)")` and the leave aborts. Demote commit and
* SelfRemove proposal both go to the same group relays, demote first so
* peers apply it before they see the SelfRemove.
*/
suspend fun leaveMarmotGroup(
nostrGroupId: HexKey,
@@ -2271,6 +2279,27 @@ class Account(
val manager = marmotManager ?: return
if (!isWriteable()) return
val metadata = manager.groupMetadata(nostrGroupId)
if (metadata != null && metadata.adminPubkeys.contains(signer.pubKey)) {
val remaining = metadata.adminPubkeys.filter { it != signer.pubKey }.toMutableList()
// MIP-03 also rejects any GCE commit that leaves the group with zero
// admins. If we're the only one, promote an arbitrary non-self
// member to admin before stepping down.
if (remaining.isEmpty()) {
val heir =
manager
.memberPubkeys(nostrGroupId)
.map { it.pubkey }
.firstOrNull { it != signer.pubKey }
if (heir != null) remaining.add(heir)
}
if (remaining.isNotEmpty()) {
val demoted = metadata.copy(adminPubkeys = remaining)
val demoteCommit = manager.updateGroupMetadata(nostrGroupId, demoted)
client.publish(demoteCommit.signedEvent, groupRelays)
}
}
val outbound = manager.leaveGroup(nostrGroupId)
client.publish(outbound.signedEvent, groupRelays)
}
@@ -219,14 +219,26 @@ class Context(
* - kind:445 group events per active group feed into inbound processor
*
* Incrementally advances the `since` cursors in [state] so the next run
* only asks relays for newer events.
* only asks relays for newer events. Two wrinkles:
*
* 1. NIP-59 gift wraps are published with a random-past `created_at`
* (see [com.vitorpamplona.quartz.utils.TimeUtils.randomWithTwoDays])
* so a newly-published wrap can trivially have `created_at` earlier
* than the last cursor we saw. To avoid silently dropping such wraps
* we always subtract a 2-day lookback window from the gift-wrap
* `since`, and dedup is handled inside [MarmotInboundProcessor].
* 2. We only advance the on-disk cursor when events actually arrive.
* Snapping an empty sync up to "now" on the first invocation would
* make every later `since` query skip any past-dated wrap or 445.
*/
suspend fun syncIncoming(timeoutMs: Long = 8_000) {
val inbox = inboxRelays().ifEmpty { anyRelays() }
val gwSince = state.giftWrapSince
val gwFilterSince =
gwSince?.let { (it - GIFT_WRAP_LOOKBACK_SECS).coerceAtLeast(0L) }
val gwFilter =
if (gwSince != null) {
MarmotFilters.giftWrapsForUserSince(identity.pubKeyHex, gwSince)
if (gwFilterSince != null) {
MarmotFilters.giftWrapsForUserSince(identity.pubKeyHex, gwFilterSince)
} else {
MarmotFilters.giftWrapsForUser(identity.pubKeyHex)
}
@@ -255,34 +267,89 @@ class Context(
if (filterMap.isEmpty()) return
val events = drain(filterMap, timeoutMs)
val now = System.currentTimeMillis() / 1000
var maxGwSeen = gwSince ?: 0L
val maxGroupSeen = perGroupFilters.keys.associateWith { state.groupSince[it] ?: 0L }.toMutableMap()
var sawGiftWrap = false
val sawGroupEvent = mutableSetOf<HexKey>()
for ((relay, event) in events) {
// All the MLS/NIP-59 decryption + persistence lives in MarmotIngest —
// we only care about bookkeeping (since-cursors, logging) here.
val result = marmot.ingest(event)
System.err.println("[cli] ingest ${event.kind}/${event.id.take(8)} via $relay${result::class.simpleName}")
val detail =
when (result) {
is com.vitorpamplona.amethyst.commons.marmot.MarmotIngestResult.Failure -> " ${result.message}"
else -> ""
}
System.err.println("[cli] ingest ${event.kind}/${event.id.take(8)} via $relay${result::class.simpleName}$detail")
when (event.kind) {
GiftWrapEvent.KIND -> {
sawGiftWrap = true
if (event.createdAt > maxGwSeen) maxGwSeen = event.createdAt
}
GroupEvent.KIND -> {
val gid = (event as? GroupEvent)?.groupId() ?: continue
sawGroupEvent.add(gid)
val prev = maxGroupSeen[gid] ?: 0L
if (event.createdAt > prev) maxGroupSeen[gid] = event.createdAt
}
}
}
state.giftWrapSince = if (maxGwSeen > 0) maxGwSeen else now
for ((gid, seen) in maxGroupSeen) {
state.groupSince[gid] = if (seen > 0) seen else now
if (sawGiftWrap && maxGwSeen > 0) {
state.giftWrapSince = maxGwSeen
}
for (gid in sawGroupEvent) {
val seen = maxGroupSeen[gid] ?: continue
if (seen > 0) state.groupSince[gid] = seen
}
// If any welcome we processed consumed a KeyPackage, MIP-00 requires
// us to immediately publish a replacement (a KP can only be used for
// ONE welcome; leaving the old one on relays lets a second sender
// invite us with a bundle we no longer have private keys for). The
// Amethyst UI handles this via its own rotation scheduler; the CLI
// has no scheduler, so we rotate inline right after sync.
if (marmot.needsKeyPackageRotation()) {
try {
val kpRelays = keyPackageRelays().ifEmpty { outboxRelays() }.ifEmpty { anyRelays() }
if (kpRelays.isNotEmpty()) {
val rotated = marmot.rotateConsumedKeyPackages(kpRelays.toList())
for (event in rotated) {
publish(event, kpRelays)
System.err.println("[cli] rotated KeyPackage → ${event.id.take(8)} on ${kpRelays.size} relay(s)")
}
}
} catch (e: Exception) {
System.err.println("[cli] key-package rotation failed: ${e.message}")
}
}
}
/**
* Resolve a group identifier given on the CLI to the nostr_group_id that
* amy's [MarmotManager] indexes on.
*
* amy internally keys everything off MIP-01's `nostr_group_id`. whitenoise
* (and every other mdk consumer) keys off the MLS `GroupContext.groupId`
* a separate 32-byte random value stamped at group creation. Cross-client
* scripts therefore wind up juggling both ids, and it's very easy to pass
* the wrong one to amy. Rather than make every caller translate, we accept
* either format and resolve here:
* 1. If the input is an active nostr_group_id, use it unchanged.
* 2. Otherwise scan active groups for one whose MLS groupId matches.
* 3. Otherwise return the input unchanged (so the caller still gets a
* sensible `not_member` response rather than a silent mismatch).
*/
fun resolveGroupId(input: HexKey): HexKey {
if (marmot.isMember(input)) return input
val normalized = input.lowercase()
return marmot.activeGroupIds().firstOrNull { nostrId ->
marmot.mlsGroupIdHex(nostrId)?.lowercase() == normalized
} ?: input
}
fun marmotGroupRelays(nostrGroupId: HexKey): Set<NormalizedRelayUrl> {
@@ -303,6 +370,13 @@ class Context(
}
companion object {
/**
* Lookback applied to the gift-wrap `since` filter to compensate for
* NIP-59's randomised-past `created_at`. 2 days matches
* [com.vitorpamplona.quartz.utils.TimeUtils.randomWithTwoDays].
*/
private const val GIFT_WRAP_LOOKBACK_SECS: Long = 2L * 24 * 60 * 60
/** Build a Context but require an identity to already exist — most commands can't run without one. */
fun open(dataDir: DataDir): Context {
val identity =
@@ -108,6 +108,7 @@ object AwaitCommands {
Json.writeLine(
mapOf(
"group_id" to match,
"mls_group_id" to ctx.marmot.mlsGroupIdHex(match),
"name" to (ctx.marmot.groupMetadata(match)?.name ?: ""),
"epoch" to ctx.marmot.groupEpoch(match),
),
@@ -127,7 +128,7 @@ object AwaitCommands {
rest: Array<String>,
): Int =
pollCondition(dataDir, rest, "await member <gid> <npub>", targetIdx = 1) { ctx, rawArgs ->
val gid = rawArgs[0]
val gid = ctx.resolveGroupId(rawArgs[0])
val target = ctx.requireUserHex(rawArgs[1])
if (!ctx.marmot.isMember(gid)) {
null
@@ -143,7 +144,7 @@ object AwaitCommands {
rest: Array<String>,
): Int =
pollCondition(dataDir, rest, "await admin <gid> <npub>", targetIdx = 1) { ctx, rawArgs ->
val gid = rawArgs[0]
val gid = ctx.resolveGroupId(rawArgs[0])
val target = ctx.requireUserHex(rawArgs[1])
if (!ctx.marmot.isMember(gid)) {
null
@@ -163,13 +164,13 @@ object AwaitCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "await rename <gid> --name <name>")
val gid = rest[0]
val args = Args(rest.drop(1).toTypedArray())
val wantedName = args.requireFlag("name")
val timeoutSecs = args.longFlag("timeout", 30)
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
val deadline = System.currentTimeMillis() + timeoutSecs * 1000
while (System.currentTimeMillis() < deadline) {
ctx.syncIncoming(timeoutMs = 3_000)
@@ -191,13 +192,13 @@ object AwaitCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "await epoch <gid> --min N")
val gid = rest[0]
val args = Args(rest.drop(1).toTypedArray())
val min = args.longFlag("min", 1)
val timeoutSecs = args.longFlag("timeout", 30)
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
val deadline = System.currentTimeMillis() + timeoutSecs * 1000
while (System.currentTimeMillis() < deadline) {
ctx.syncIncoming(timeoutMs = 3_000)
@@ -219,13 +220,13 @@ object AwaitCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "await message <gid> --match STRING")
val gid = rest[0]
val args = Args(rest.drop(1).toTypedArray())
val needle = args.requireFlag("match")
val timeoutSecs = args.longFlag("timeout", 30)
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
val deadline = System.currentTimeMillis() + timeoutSecs * 1000
while (System.currentTimeMillis() < deadline) {
ctx.syncIncoming(timeoutMs = 3_000)
@@ -37,10 +37,10 @@ object GroupAddMemberCommand {
rest: Array<String>,
): Int {
if (rest.size < 2) return Json.error("bad_args", "group add <group_id> <npub> [<npub> ...]")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
@@ -40,9 +40,12 @@ object GroupCreateCommand {
ctx.prepare()
val gid = RandomInstance.bytes(32).toHexKey()
ctx.marmot.createGroup(gid)
// Stamp initial metadata via the shared factory so UI + CLI stay byte-identical.
// Stamp initial metadata via the shared factory so UI + CLI stay
// byte-identical. Bake the MarmotGroupData extension into the
// epoch-0 GroupContext directly (see `MarmotManager.createGroup`)
// so later invitees receive a pre-populated group from the
// welcome and never have to chase an undecryptable bootstrap
// commit that predates their membership.
val outboxUrls = ctx.outboxRelays().map { it.url }
val metadata =
MarmotGroupData.bootstrap(
@@ -51,19 +54,14 @@ object GroupCreateCommand {
outboxRelays = outboxUrls,
name = name,
)
val commit = ctx.marmot.updateGroupMetadata(gid, metadata)
// Group relays == what the metadata carries, which on first commit is our outbox.
val targets = ctx.outboxRelays()
val ack = ctx.publish(commit.signedEvent, targets)
ctx.marmot.createGroup(gid, initialMetadata = metadata)
Json.writeLine(
mapOf(
"group_id" to gid,
"mls_group_id" to ctx.marmot.mlsGroupIdHex(gid),
"name" to name,
"epoch" to ctx.marmot.groupEpoch(gid),
"commit_event_id" to commit.signedEvent.id,
"published_to" to ack.filterValues { it }.keys.map { it.url },
),
)
return 0
@@ -30,10 +30,10 @@ object GroupMembershipCommands {
rest: Array<String>,
): Int {
if (rest.size < 2) return Json.error("bad_args", "group remove <gid> <npub>")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
val target = ctx.requireUserHex(rest[1])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
@@ -66,18 +66,46 @@ object GroupMembershipCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "group leave <gid>")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
val targets = ctx.marmotGroupRelays(gid).ifEmpty { ctx.outboxRelays() }
// MIP-01/MIP-03: members listed in `admin_pubkeys` MUST NOT issue
// a SelfRemove proposal before first publishing a GCE that drops
// themselves from the admin list (MlsGroup.selfRemove enforces
// this with `check(!isLocalAdmin())`), and that same GCE MUST NOT
// leave the group with zero admins (admin depletion). If we're
// the only admin, hand admin to another member first.
val demoteEventId: String? =
ctx.marmot.groupMetadata(gid)?.let { metadata ->
if (!metadata.adminPubkeys.contains(ctx.identity.pubKeyHex)) return@let null
val newAdmins = metadata.adminPubkeys.filter { it != ctx.identity.pubKeyHex }.toMutableList()
if (newAdmins.isEmpty()) {
val heir =
ctx.marmot
.memberPubkeys(gid)
.map { it.pubkey }
.firstOrNull { it != ctx.identity.pubKeyHex }
?: return@let null // solo group — skip demote, let MLS state cleanup handle it
newAdmins.add(heir)
}
val demoted = metadata.copy(adminPubkeys = newAdmins)
val demoteCommit = ctx.marmot.updateGroupMetadata(gid, demoted)
ctx.publish(demoteCommit.signedEvent, targets)
demoteCommit.signedEvent.id
}
val outbound = ctx.marmot.leaveGroup(gid)
val ack = ctx.publish(outbound.signedEvent, targets)
Json.writeLine(
mapOf(
"group_id" to gid,
"self_demote_event_id" to demoteEventId,
"proposal_event_id" to outbound.signedEvent.id,
"published_to" to ack.filterValues { it }.keys.map { it.url },
),
@@ -66,12 +66,13 @@ object GroupMetadataCommands {
private suspend fun edit(
dataDir: DataDir,
gid: HexKey,
rawGid: HexKey,
mutate: suspend (Context, MarmotGroupData) -> MarmotGroupData,
): Int {
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rawGid)
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
val outboxUrls = ctx.outboxRelays().map { it.url }
@@ -56,10 +56,10 @@ object GroupReadCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "group show <group_id>")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
val meta = ctx.marmot.groupMetadata(gid)
@@ -70,6 +70,7 @@ object GroupReadCommands {
Json.writeLine(
mapOf(
"group_id" to gid,
"mls_group_id" to ctx.marmot.mlsGroupIdHex(gid),
"name" to (meta?.name ?: ""),
"description" to (meta?.description ?: ""),
"epoch" to ctx.marmot.groupEpoch(gid),
@@ -90,10 +91,10 @@ object GroupReadCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "group members <group_id>")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
val members =
@@ -112,10 +113,10 @@ object GroupReadCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "group admins <group_id>")
val gid = rest[0]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
val m = ctx.marmot.groupMetadata(gid)
@@ -45,11 +45,11 @@ object MessageCommands {
rest: Array<String>,
): Int {
if (rest.size < 2) return Json.error("bad_args", "message send <gid> <text>")
val gid = rest[0]
val text = rest[1]
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
@@ -77,12 +77,12 @@ object MessageCommands {
rest: Array<String>,
): Int {
if (rest.isEmpty()) return Json.error("bad_args", "message list <gid>")
val gid = rest[0]
val args = Args(rest.drop(1).toTypedArray())
val limit = args.intFlag("limit", Int.MAX_VALUE)
val ctx = Context.open(dataDir)
try {
ctx.prepare()
val gid = ctx.resolveGroupId(rest[0])
ctx.syncIncoming()
if (!ctx.marmot.isMember(gid)) return Json.error("not_member", gid)
@@ -27,6 +27,7 @@ import com.vitorpamplona.quartz.marmot.mip02Welcome.WelcomeEvent
import com.vitorpamplona.quartz.marmot.mip03GroupMessages.GroupEvent
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
/**
@@ -95,11 +96,23 @@ suspend fun MarmotManager.ingest(event: Event): MarmotIngestResult =
private suspend fun MarmotManager.ingestGiftWrap(wrap: GiftWrapEvent): MarmotIngestResult =
try {
val inner = wrap.unwrapOrNull(signer) ?: return MarmotIngestResult.Ignored
if (!MarmotInboundProcessor.isWelcomeEvent(inner) || inner !is WelcomeEvent) {
// NIP-59 wraps contain TWO encryption layers:
// kind:1059 gift wrap → kind:13 sealed rumor → the rumor itself.
// `GiftWrapEvent.unwrapOrNull` only peels the outer layer; when the
// result is a [SealedRumorEvent] we must unseal it to reach the
// kind:444 Welcome rumor. The old code checked `isWelcomeEvent` on
// the seal (kind:13) and always took the Ignored branch, which is
// why every inbound Welcome was silently dropped by the CLI and by
// any non-Amethyst consumer.
val rumor =
when (val inner = wrap.unwrapOrNull(signer) ?: return MarmotIngestResult.Ignored) {
is SealedRumorEvent -> inner.unsealOrNull(signer) ?: return MarmotIngestResult.Ignored
else -> inner
}
if (!MarmotInboundProcessor.isWelcomeEvent(rumor) || rumor !is WelcomeEvent) {
return MarmotIngestResult.Ignored
}
when (val result = processWelcome(inner, inner.nostrGroupId())) {
when (val result = processWelcome(rumor, rumor.nostrGroupId())) {
is WelcomeResult.Joined -> {
MarmotIngestResult.JoinedGroup(
nostrGroupId = result.nostrGroupId,
@@ -262,11 +262,24 @@ class MarmotManager(
/**
* Create a new MLS group.
*
* When [initialMetadata] is non-null it is baked into epoch 0's
* GroupContext.extensions. Later joiners' welcomes therefore carry the
* group name / admin list / relays from the get-go and no separate
* "bootstrap commit" needs to be published. The bootstrap-commit path
* works in theory, but it produces a kind:445 encrypted with epoch 0's
* exporter secret that no post-membership peer (amethyst or wn) has,
* so each such peer wastes their commit-retry budget on an
* undecryptable event before processing the real state.
*/
suspend fun createGroup(nostrGroupId: HexKey): HexKey {
suspend fun createGroup(
nostrGroupId: HexKey,
initialMetadata: MarmotGroupData? = null,
): HexKey {
Log.d("MarmotManager") { "createGroup($nostrGroupId): by ${signer.pubKey.take(8)}" }
val identity = signer.pubKey.hexToByteArray()
groupManager.createGroup(nostrGroupId, identity)
val extras = initialMetadata?.let { listOf(it.toExtension()) } ?: emptyList()
groupManager.createGroup(nostrGroupId, identity, initialExtensions = extras)
subscriptionManager.subscribeGroup(nostrGroupId)
Log.d("MarmotManager") { "createGroup($nostrGroupId): persisted and subscribed" }
return nostrGroupId
@@ -277,15 +290,18 @@ class MarmotManager(
* Returns proposal bytes to publish (as a GroupEvent).
*/
suspend fun leaveGroup(nostrGroupId: HexKey): OutboundGroupEvent {
// Build the outbound event BEFORE deleting group state (needs exporter secret)
val group =
groupManager.getGroup(nostrGroupId)
?: throw IllegalStateException("Not a member of group $nostrGroupId")
val proposalBytes = group.selfRemove()
val outboundEvent = outboundProcessor.buildCommitEvent(nostrGroupId, proposalBytes)
// leaveGroup() returns the framed standalone SelfRemove proposal
// (PublicMessage{Proposal}) plus the pre-commit exporter key for
// outer encryption. Runs BEFORE we tear down the subscriptions so
// the pre-commit exporter is still derivable.
val (framedBytes, exporterKey) = groupManager.leaveGroup(nostrGroupId)
val outboundEvent =
outboundProcessor.buildCommitEvent(
nostrGroupId = nostrGroupId,
commitBytes = framedBytes,
exporterKey = exporterKey,
)
// Now clean up group state
groupManager.removeGroupState(nostrGroupId)
subscriptionManager.unsubscribeGroup(nostrGroupId)
try {
messageStore?.delete(nostrGroupId)
@@ -346,13 +362,24 @@ class MarmotManager(
/**
* Update group metadata (name, description, etc.) via a GroupContextExtensions proposal.
* Creates a GCE proposal, commits it, and returns the commit event to publish.
*
* RFC 9420 §12.1.7: a GroupContextExtensions proposal REPLACES the entire
* extension list in GroupContext. Peers (notably mdk-core / whitenoise-rs)
* reject welcomes whose GroupContext is missing the required-capabilities
* extension, and strip metadata from groups whose context lacks the
* MarmotGroupData extension. We therefore preserve every other existing
* extension and overwrite only the slot we actually want to update.
*/
suspend fun updateGroupMetadata(
nostrGroupId: HexKey,
metadata: MarmotGroupData,
): OutboundGroupEvent {
val commitResult =
groupManager.updateGroupExtensions(nostrGroupId, listOf(metadata.toExtension()))
val group =
groupManager.getGroup(nostrGroupId)
?: throw IllegalStateException("Not a member of group $nostrGroupId")
val preserved = group.extensions.filter { it.extensionType != MarmotGroupData.EXTENSION_ID_INT }
val merged = preserved + metadata.toExtension()
val commitResult = groupManager.updateGroupExtensions(nostrGroupId, merged)
val commitEvent =
outboundProcessor.buildCommitEvent(
nostrGroupId = nostrGroupId,
@@ -480,6 +507,18 @@ class MarmotManager(
*/
fun groupEpoch(nostrGroupId: HexKey): Long? = groupManager.getGroup(nostrGroupId)?.epoch
/**
* Hex-encoded MLS group id for the group keyed by [nostrGroupId], or null if
* that group is not locally known.
*
* Interop note: whitenoise-rs (and every mdk consumer) indexes groups by the
* MLS GroupContext's groupId, NOT the MIP-01 nostr_group_id that we use as
* the primary key internally. When a harness or external caller needs to
* cross-reference a group with another client (e.g. the interop harness
* calling `wn messages list <mls_id>`), it needs this translation.
*/
fun mlsGroupIdHex(nostrGroupId: HexKey): HexKey? = groupManager.getGroup(nostrGroupId)?.groupId?.toHexKey()
/**
* Resolve the MLS leaf index for a member by Nostr pubkey, or null if that
* pubkey isn't currently in the group.
@@ -511,16 +511,37 @@ class MarmotInboundProcessor(
when (mlsMessage.wireFormat) {
WireFormat.PRIVATE_MESSAGE -> {
// For private commits, MLS decrypt handles epoch advancement
val decrypted = groupManager.decrypt(groupId, mlsMessage.toTlsBytes())
if (decrypted.contentType == ContentType.COMMIT) {
val group = groupManager.getGroup(groupId)
GroupEventResult.CommitProcessed(groupId, group?.epoch ?: 0)
} else {
GroupEventResult.Error(
groupId,
"Expected COMMIT but got ${decrypted.contentType}",
)
// Sniff the PrivateMessage epoch without consuming any
// ratchet state. Past-epoch echoes and future-epoch
// arrivals must not advance the secret tree — otherwise
// the real handshake / application message gets rejected
// when it finally arrives.
val privPeek = PrivateMessage.decodeTls(TlsReader(mlsMessage.payload))
val currentEpoch = groupManager.getGroup(groupId)?.epoch
when {
currentEpoch != null && privPeek.epoch < currentEpoch -> {
GroupEventResult.Duplicate(groupId)
}
currentEpoch != null && privPeek.epoch > currentEpoch -> {
GroupEventResult.Error(
groupId,
"PrivateMessage epoch ${privPeek.epoch} is ahead of local epoch $currentEpoch; ignoring",
)
}
else -> {
val decrypted = groupManager.decrypt(groupId, mlsMessage.toTlsBytes())
if (decrypted.contentType == ContentType.COMMIT) {
val group = groupManager.getGroup(groupId)
GroupEventResult.CommitProcessed(groupId, group?.epoch ?: 0)
} else {
GroupEventResult.Error(
groupId,
"Expected COMMIT but got ${decrypted.contentType}",
)
}
}
}
}
@@ -556,14 +577,29 @@ class MarmotInboundProcessor(
}
else -> {
groupManager.processCommit(
nostrGroupId = groupId,
commitBytes = pubMsg.content,
senderLeafIndex = pubMsg.sender.leafIndex,
confirmationTag = tag,
)
// RFC 9420 §6.2 — reject PublicMessage commits
// whose membership_tag doesn't match what the
// current epoch's membership_key would produce.
// Without this an outsider with the outer
// exporter secret could inject arbitrary commit
// bytes and advance the group past them.
val group = groupManager.getGroup(groupId)
GroupEventResult.CommitProcessed(groupId, group?.epoch ?: 0)
if (group != null && !group.verifyPublicMessageCommitMembershipTag(pubMsg)) {
GroupEventResult.Error(
groupId,
"Invalid membership_tag on PublicMessage commit",
)
} else {
groupManager.processCommit(
nostrGroupId = groupId,
commitBytes = pubMsg.content,
senderLeafIndex = pubMsg.sender.leafIndex,
confirmationTag = tag,
signature = pubMsg.signature,
)
val post = groupManager.getGroup(groupId)
GroupEventResult.CommitProcessed(groupId, post?.epoch ?: 0)
}
}
}
}
@@ -191,7 +191,22 @@ data class MarmotGroupData(
fun toExtension(): Extension = Extension(EXTENSION_ID_INT, encodeTls())
companion object {
const val CURRENT_VERSION = 3
/**
* Version we emit for freshly created groups and fresh GCE commits.
*
* Held at 2 (not 3) because the Rust mdk-core MLS engine used by
* whitenoise-rs (the only other shipping Marmot client today) is
* stricter than MIP-01's "ignore trailing bytes for forward
* compatibility" rule — it rejects v3 payloads with
* `ExtensionFormatError("Trailing bytes in NostrGroupDataExtension")`,
* which means our v3 welcomes/commits never get applied and every
* cross-client group flow breaks. We still PARSE v3 happily via
* [decodeTls] (any peer that sends us a v3 group will round-trip),
* but we don't create them until mdk-core catches up.
*
* Bump back to 3 once mdk publishes the forward-compat fix.
*/
const val CURRENT_VERSION = 2
/** Versions this implementation understands. v0 is reserved/invalid per MIP-01. */
val SUPPORTED_VERSIONS: Set<Int> = setOf(1, 2, 3)
File diff suppressed because it is too large Load Diff
@@ -174,10 +174,11 @@ class MlsGroupManager(
nostrGroupId: HexKey,
identity: ByteArray,
signingKey: ByteArray? = null,
initialExtensions: List<com.vitorpamplona.quartz.marmot.mls.tree.Extension> = emptyList(),
): MlsGroup =
mutex.withLock {
Log.d(TAG) { "createGroup($nostrGroupId): creating new MLS group" }
val group = MlsGroup.create(identity, signingKey)
val group = MlsGroup.create(identity, signingKey, initialExtensions)
groups[nostrGroupId] = group
persistGroup(nostrGroupId)
Log.d(TAG) { "createGroup($nostrGroupId): done, in-memory group count=${groups.size}" }
@@ -287,6 +288,8 @@ class MlsGroupManager(
commitBytes: ByteArray,
senderLeafIndex: Int,
confirmationTag: ByteArray,
signature: ByteArray = ByteArray(0),
wireFormat: com.vitorpamplona.quartz.marmot.mls.framing.WireFormat = com.vitorpamplona.quartz.marmot.mls.framing.WireFormat.PUBLIC_MESSAGE,
) = mutex.withLock {
val group = requireGroup(nostrGroupId)
@@ -297,7 +300,7 @@ class MlsGroupManager(
// the current epoch key, wasting the finite retention slots.
val retainedBefore = group.retainedSecrets()
group.processCommit(commitBytes, senderLeafIndex, confirmationTag)
group.processCommit(commitBytes, senderLeafIndex, confirmationTag, signature, wireFormat)
pushRetainedEpoch(nostrGroupId, retainedBefore)
persistGroup(nostrGroupId)
@@ -331,19 +334,40 @@ class MlsGroupManager(
mutex.withLock {
val group = requireGroup(nostrGroupId)
// Try current epoch
val current = group.decryptOrNull(messageBytes)
if (current != null) return@withLock current
// Try current epoch. If we hit an exception here we MUST surface
// it — commits that throw mid-processCommit leave the in-memory
// group half-mutated, and a retry via `group.decrypt(...)` will
// just report a stale "epoch mismatch" from the partial advance,
// hiding the real bug. Capture the original throwable, try
// retained epochs as a fallback, and re-raise the captured one
// if nothing decrypts.
val retainedBefore = group.retainedSecrets()
val preEpoch = group.epoch
val currentFailure: Throwable? =
try {
val result = group.decrypt(messageBytes)
// PrivateMessage commits apply inline through
// `MlsGroup.decrypt` → `processCommit`; the epoch advances
// in memory but the CLI reopens a fresh Context on every
// command, so we MUST persist here or reloaded state
// silently reverts to the pre-commit extensions (including
// admin list).
if (result.contentType == com.vitorpamplona.quartz.marmot.mls.framing.ContentType.COMMIT && group.epoch != preEpoch) {
pushRetainedEpoch(nostrGroupId, retainedBefore)
persistGroup(nostrGroupId)
}
return@withLock result
} catch (t: Throwable) {
t
}
// Try retained epochs
val retained = retainedEpochs[nostrGroupId] ?: emptyList()
for (epochSecrets in retained) {
val result = tryDecryptWithRetainedEpoch(messageBytes, epochSecrets)
if (result != null) return@withLock result
}
// No epoch could decrypt — rethrow from current epoch for diagnostics
group.decrypt(messageBytes)
throw currentFailure ?: IllegalStateException("Decrypt failed without captured cause")
}
/**
@@ -441,16 +465,17 @@ class MlsGroupManager(
}
/**
* Leave a group (self-remove).
* Returns the SelfRemove proposal bytes to publish, then removes
* local state.
* Leave a group by publishing a standalone PublicMessage SelfRemove
* proposal (draft-ietf-mls-extensions). Admin receivers auto-commit
* the pending proposal; the caller publishes the framed bytes as the
* kind:445 content, outer-encrypted with the returned exporter key.
*/
suspend fun leaveGroup(nostrGroupId: HexKey): ByteArray =
suspend fun leaveGroup(nostrGroupId: HexKey): Pair<ByteArray, ByteArray> =
mutex.withLock {
val group = requireGroup(nostrGroupId)
val proposalBytes = group.selfRemove()
val result = group.buildSelfRemoveProposalMessage()
removeGroupStateUnlocked(nostrGroupId)
proposalBytes
result
}
/**
@@ -45,8 +45,11 @@ enum class ProposalType(
EXTERNAL_INIT(6),
GROUP_CONTEXT_EXTENSIONS(7),
// Marmot custom proposal types (private-use range 0xF000-0xFFFF)
SELF_REMOVE(0xF001),
// SelfRemove is standardized in MLS Extensions draft-ietf-mls-extensions
// as IANA proposal type 0x000A, NOT a Marmot private-use value.
// openmls / mdk encode it as 0x000A on the wire; quartz was writing
// 0xF001, which strict receivers reject as "Unknown ProposalType".
SELF_REMOVE(0x000A),
;
companion object {
@@ -50,17 +50,21 @@ class SecretTree(
/** Per-sender ratchet state: (handshake generation, handshake secret, app generation, app secret) */
private val senderState = mutableMapOf<Int, SenderRatchetState>()
/** Consumed (sender, generation) pairs for replay detection (RFC 9420 Section 9.1) */
/** Consumed (sender, generation) pairs for replay detection on the APPLICATION ratchet. */
private val consumedGenerations = mutableMapOf<Int, MutableSet<Int>>()
/** Same replay tracker, but for the HANDSHAKE ratchet (commits / proposals). */
private val consumedHandshakeGenerations = mutableMapOf<Int, MutableSet<Int>>()
/**
* Cache of key/nonce pairs for skipped generations.
* Cache of key/nonce pairs for skipped APPLICATION generations.
* Key: (leafIndex, generation) -> derived KeyNonceGeneration.
* When fast-forwarding a ratchet, intermediate generations are saved here
* so that out-of-order messages arriving later can still be decrypted.
*/
private val skippedKeys = mutableMapOf<Pair<Int, Int>, KeyNonceGeneration>()
/** Same cache for the HANDSHAKE ratchet. */
private val handshakeSkippedKeys = mutableMapOf<Pair<Int, Int>, KeyNonceGeneration>()
private companion object {
/** Maximum number of skipped key entries to retain (prevents unbounded memory growth). */
const val MAX_SKIPPED_KEYS = 1000
@@ -186,6 +190,70 @@ class SecretTree(
return result
}
/**
* Handshake-ratchet counterpart to [applicationKeyNonceForGeneration].
*
* PrivateMessage commits / proposals (RFC 9420 §6.3.2) are encrypted
* with a separate handshake ratchet per sender leaf NOT the
* application ratchet. openmls / mdk use this path by default
* (`MIXED_CIPHERTEXT_WIRE_FORMAT_POLICY`), so quartz must also
* ratchet-forward the handshake chain when decrypting an inbound
* PrivateMessage commit.
*/
fun handshakeKeyNonceForGeneration(
leafIndex: Int,
generation: Int,
): KeyNonceGeneration {
val cachedKey = handshakeSkippedKeys.remove(Pair(leafIndex, generation))
if (cachedKey != null) {
val senderConsumed = consumedHandshakeGenerations.getOrPut(leafIndex) { mutableSetOf() }
require(generation !in senderConsumed) {
"Replay detected: handshake generation $generation from sender $leafIndex already consumed"
}
senderConsumed.add(generation)
return cachedKey
}
val state = getOrInitSender(leafIndex)
require(generation >= state.handshakeGeneration) {
"Handshake generation $generation already consumed (current: ${state.handshakeGeneration})"
}
val senderConsumed = consumedHandshakeGenerations.getOrPut(leafIndex) { mutableSetOf() }
require(generation !in senderConsumed) {
"Replay detected: handshake generation $generation from sender $leafIndex already consumed"
}
senderConsumed.add(generation)
if (senderConsumed.size > MAX_CONSUMED_GENERATIONS_PER_SENDER) {
val minGeneration = state.handshakeGeneration
senderConsumed.removeAll { it < minGeneration }
}
var secret = state.handshakeSecret
var gen = state.handshakeGeneration
while (gen < generation) {
val intermediateKng = deriveKeyNonce(secret, gen)
val cacheKey = Pair(leafIndex, gen)
if (handshakeSkippedKeys.size < MAX_SKIPPED_KEYS) {
handshakeSkippedKeys[cacheKey] = intermediateKng
}
secret = MlsCryptoProvider.expandWithLabel(secret, "secret", generationContext(gen), MlsCryptoProvider.HASH_OUTPUT_LENGTH)
gen++
}
val result = deriveKeyNonce(secret, generation)
val nextSecret = MlsCryptoProvider.expandWithLabel(secret, "secret", generationContext(generation), MlsCryptoProvider.HASH_OUTPUT_LENGTH)
senderState[leafIndex] =
state.copy(
handshakeSecret = nextSecret,
handshakeGeneration = generation + 1,
)
return result
}
/**
* Encode a generation counter as a 4-byte big-endian uint32 for DeriveTreeSecret context.
*/
@@ -24,7 +24,6 @@ import com.vitorpamplona.quartz.marmot.mls.codec.TlsReader
import com.vitorpamplona.quartz.marmot.mls.codec.TlsSerializable
import com.vitorpamplona.quartz.marmot.mls.codec.TlsWriter
import com.vitorpamplona.quartz.marmot.mls.crypto.MlsCryptoProvider
import com.vitorpamplona.quartz.marmot.mls.crypto.X25519
/**
* MLS Ratchet Tree (RFC 9420 Section 7).
@@ -157,18 +156,56 @@ class RatchetTree(
/**
* Remove a member by blanking their leaf and all parent nodes on the direct path.
*
* RFC 9420 §7.8: trailing blank leaves MUST be trimmed so every participant
* agrees on `leaf_count` (and therefore on `direct_path` lengths). Openmls
* does this after every leaf blank; without the trim, a committer that had
* the removed leaf at the far right end of the tree sends an UpdatePath one
* entry longer than the receiver's tree layout accepts, and the receiver
* errors out with `UpdatePathError(PathLengthMismatch)`.
*/
fun removeLeaf(leafIndex: Int) {
setLeaf(leafIndex, null)
// Blank the direct path
val directPath = BinaryTree.directPath(leafIndex, _leafCount)
for (nodeIdx in directPath) {
if (nodeIdx < nodes.size) {
nodes[nodeIdx] = null
}
}
// Shrink leafCount past any trailing blanks. Parent nodes that become
// orphaned by the shrink are dropped from the `nodes` list so
// treeHash() / resolution() / direct_path() agree with openmls on the
// new tree shape.
while (_leafCount > 0 && getLeaf(_leafCount - 1) == null) {
_leafCount--
}
val effectiveNodeCount = if (_leafCount > 0) BinaryTree.nodeCount(_leafCount) else 0
while (nodes.size > effectiveNodeCount) {
nodes.removeAt(nodes.size - 1)
}
}
/**
* Snapshot the mutable tree state so callers can roll back after a
* failed commit application. `TreeNode`, `LeafNode`, and `ParentNode`
* are all immutable data classes copying the `nodes` list is enough
* to isolate future edits.
*/
fun snapshot(): Snapshot = Snapshot(nodes.toList(), _leafCount)
/** Restore the mutable tree state produced by an earlier [snapshot]. */
fun restoreFrom(snapshot: Snapshot) {
nodes.clear()
nodes.addAll(snapshot.nodes)
_leafCount = snapshot.leafCount
}
/** Opaque capture of the ratchet tree's mutable state. */
class Snapshot internal constructor(
internal val nodes: List<TreeNode?>,
internal val leafCount: Int,
)
/**
* Compute the tree hash for this ratchet tree (RFC 9420 Section 7.9).
* Used in GroupContext to bind the group state to the tree.
@@ -278,15 +315,20 @@ class RatchetTree(
var currentSecret = leafSecret
for (nodeIdx in directPath) {
// RFC 9420 Section 7.4: path_secret[0] = leafSecret,
// node_secret[n] = DeriveSecret(path_secret[n], "node")
// RFC 9420 §7.4: path_secret[0] = leafSecret,
// node_secret[n] = DeriveSecret(path_secret[n], "node"),
// node's HPKE keypair = DeriveKeyPair(node_secret).
// DeriveKeyPair is HPKE's (RFC 9180 §7.1.3), NOT an MLS ExpandWithLabel —
// it uses the "HPKE-v1" + KEM suite_id labels, and produces a keypair
// that openmls/mdk can reproduce from the same path_secret. If we derive
// with a different formula, receivers compute different public keys and
// reject the UpdatePath with `UpdatePathError(PathMismatch)`.
val nodeSecret = MlsCryptoProvider.deriveSecret(currentSecret, "node")
val kp =
com.vitorpamplona.quartz.marmot.mls.crypto.Hpke
.deriveKeyPair(nodeSecret)
// Derive HPKE key pair from node_secret
val privateKey = MlsCryptoProvider.expandWithLabel(nodeSecret, "hpke", ByteArray(0), 32)
val publicKey = X25519.publicFromPrivate(privateKey)
results.add(PathSecretAndKey(currentSecret, privateKey, publicKey))
results.add(PathSecretAndKey(currentSecret, kp.privateKey, kp.publicKey))
// path_secret[n+1] = DeriveSecret(path_secret[n], "path")
currentSecret = MlsCryptoProvider.deriveSecret(currentSecret, "path")
@@ -0,0 +1,291 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.marmot.mls.group
import com.vitorpamplona.quartz.marmot.mls.codec.TlsReader
import com.vitorpamplona.quartz.marmot.mls.framing.MlsMessage
import com.vitorpamplona.quartz.marmot.mls.framing.PublicMessage
import com.vitorpamplona.quartz.marmot.mls.framing.WireFormat
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
/**
* Negative tests: every post-RFC-9420-§6 authenticity check that
* [MlsGroup.processCommit] performs should reject the tampered input AND
* leave the group in its pre-commit state (atomic rollback no partial
* epoch advance, no mutated tree, no dangling pending proposals).
*/
class MlsGroupNegativeTest {
private data class TwoMemberFixture(
val alice: MlsGroup,
val bob: MlsGroup,
val charliePkg: ByteArray,
)
/**
* Produce a 2-member group (alice creator, bob joined via Welcome)
* plus a spare KeyPackage for Charlie. Callers use Charlie's bundle to
* drive Alice into producing a fresh Add commit that Bob will process.
*/
private fun twoMemberGroup(): TwoMemberFixture {
val alice = MlsGroup.create(identity = "alice".encodeToByteArray())
val bobBundle = alice.createKeyPackage(identity = "bob".encodeToByteArray(), signingKey = ByteArray(32) { 1 })
val addBob = alice.addMember(bobBundle.keyPackage.toTlsBytes())
val bob = MlsGroup.processWelcome(addBob.welcomeBytes!!, bobBundle)
val charlieBundle =
alice.createKeyPackage(identity = "charlie".encodeToByteArray(), signingKey = ByteArray(32) { 2 })
return TwoMemberFixture(alice, bob, charlieBundle.keyPackage.toTlsBytes())
}
/**
* Decode a framedCommitBytes (MlsMessage(PublicMessage(commit))) into
* the fields [MlsGroup.processCommit] consumes. Tests re-use this to
* mutate individual fields before re-invoking.
*/
private data class CommitParts(
val content: ByteArray,
val senderLeafIndex: Int,
val confirmationTag: ByteArray,
val signature: ByteArray,
val pubMsg: PublicMessage,
)
private fun parseCommit(framedBytes: ByteArray): CommitParts {
val mlsMsg = MlsMessage.decodeTls(TlsReader(framedBytes))
val pub = PublicMessage.decodeTls(TlsReader(mlsMsg.payload))
return CommitParts(
content = pub.content,
senderLeafIndex = pub.sender.leafIndex,
confirmationTag = pub.confirmationTag!!,
signature = pub.signature,
pubMsg = pub,
)
}
/** Baseline: an honest commit applies and advances Bob's epoch. */
@Test
fun honestCommitIsAccepted() {
val fx = twoMemberGroup()
val bobEpochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = parts.confirmationTag,
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
assertEquals(bobEpochBefore + 1, fx.bob.epoch)
}
/**
* Tampered confirmation_tag flipping a single bit must fail the
* HMAC compare, throw, and leave Bob on the original epoch.
*/
@Test
fun tamperedConfirmationTagIsRejectedAndStateRolledBack() {
val fx = twoMemberGroup()
val epochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
val tamperedTag = parts.confirmationTag.copyOf().apply { this[0] = (this[0].toInt() xor 0x01).toByte() }
assertFailsWith<IllegalArgumentException> {
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = tamperedTag,
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
}
assertEquals(epochBefore, fx.bob.epoch)
// And the honest commit still applies cleanly afterwards — proving
// no mutable state leaked across the failed attempt.
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = parts.confirmationTag,
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
assertEquals(epochBefore + 1, fx.bob.epoch)
}
/**
* Tampered FramedContentTBS signature receiver reconstructs the
* exact same TBS bytes the sender signed; any bit-flip in the
* signature fails Ed25519 verification.
*/
@Test
fun tamperedSignatureIsRejectedAndStateRolledBack() {
val fx = twoMemberGroup()
val epochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
val tamperedSig = parts.signature.copyOf().apply { this[0] = (this[0].toInt() xor 0x80).toByte() }
assertFailsWith<IllegalArgumentException> {
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = parts.confirmationTag,
signature = tamperedSig,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
}
assertEquals(epochBefore, fx.bob.epoch)
}
/**
* Claiming the commit came from the wrong leaf index the signature
* was bound to the original sender leaf, so verifying against a
* different leaf's pre-commit signatureKey fails.
*/
@Test
fun spoofedSenderLeafIndexIsRejected() {
val fx = twoMemberGroup()
val epochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
// Alice is leaf 0, Bob is leaf 1. Swap.
val wrongLeaf = if (parts.senderLeafIndex == 0) 1 else 0
assertFailsWith<IllegalArgumentException> {
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = wrongLeaf,
confirmationTag = parts.confirmationTag,
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
}
assertEquals(epochBefore, fx.bob.epoch)
}
/**
* Declaring the wrong wire_format at the receiver the FramedContentTBS
* hash diverges and signature verification fails, because the sender
* mixed wire_format=PUBLIC_MESSAGE into their TBS bytes.
*/
@Test
fun wrongWireFormatIsRejected() {
val fx = twoMemberGroup()
val epochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
assertFailsWith<IllegalArgumentException> {
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = parts.confirmationTag,
signature = parts.signature,
wireFormat = WireFormat.PRIVATE_MESSAGE,
)
}
assertEquals(epochBefore, fx.bob.epoch)
}
/**
* Empty confirmation_tag is rejected explicitly RFC 9420 §6 requires
* every commit to carry a confirmation_tag, and an omitted tag means
* the receiver has no authentication of the post-commit epoch secrets.
*/
@Test
fun emptyConfirmationTagIsRejected() {
val fx = twoMemberGroup()
val epochBefore = fx.bob.epoch
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
assertFailsWith<IllegalArgumentException> {
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = ByteArray(0),
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
}
assertEquals(epochBefore, fx.bob.epoch)
}
/**
* Tampered membership_tag PublicMessage commits from a member must
* carry an HMAC(membership_key, TBM) that matches what the receiver
* can reconstruct from the current epoch's membership_key. Without
* this check, an outsider that learned the outer exporter secret
* could forge arbitrary commit bodies.
*
* This is enforced at the MarmotInboundProcessor layer before
* [MlsGroup.processCommit] is called; the group exposes
* [MlsGroup.verifyPublicMessageCommitMembershipTag] so the processor
* can short-circuit on tag mismatch.
*/
@Test
fun tamperedMembershipTagFailsPublicMessageCheck() {
val fx = twoMemberGroup()
val commit = fx.alice.addMember(fx.charliePkg)
val parts = parseCommit(commit.framedCommitBytes)
// Honest tag is valid.
assertTrue(fx.bob.verifyPublicMessageCommitMembershipTag(parts.pubMsg))
// Flip one bit in the wire tag — must be rejected.
val original = parts.pubMsg.membershipTag!!
val tampered = original.copyOf().apply { this[0] = (this[0].toInt() xor 0x01).toByte() }
val badPub = parts.pubMsg.copy(membershipTag = tampered)
assertFalse(fx.bob.verifyPublicMessageCommitMembershipTag(badPub))
// A missing tag is also rejected.
val missingPub = parts.pubMsg.copy(membershipTag = null)
assertFalse(fx.bob.verifyPublicMessageCommitMembershipTag(missingPub))
// And the honest commit still applies — nothing got mutated by the checks.
val epochBefore = fx.bob.epoch
fx.bob.processCommit(
commitBytes = parts.content,
senderLeafIndex = parts.senderLeafIndex,
confirmationTag = parts.confirmationTag,
signature = parts.signature,
wireFormat = WireFormat.PUBLIC_MESSAGE,
)
assertEquals(epochBefore + 1, fx.bob.epoch)
assertNotEquals(epochBefore, fx.bob.epoch)
}
}
@@ -173,7 +173,7 @@ class MarmotMipBehaviorTest {
val alice = manager.getGroup(groupId)!!
assertFailsWith<IllegalStateException> { alice.proposeSelfRemove() }
assertFailsWith<IllegalStateException> { alice.selfRemove() }
assertFailsWith<IllegalStateException> { alice.buildSelfRemoveProposalMessage() }
}
@Test
@@ -187,8 +187,8 @@ class MarmotMipBehaviorTest {
manager.updateGroupExtensions(groupId, listOf(strangerAdmin.toExtension()))
val alice = manager.getGroup(groupId)!!
// selfRemove (standalone proposal helper) should succeed for a non-admin.
val bytes = alice.selfRemove()
// Standalone SelfRemove proposal helper should succeed for a non-admin.
val (bytes, _) = alice.buildSelfRemoveProposalMessage()
assertTrue(bytes.isNotEmpty())
}
@@ -272,7 +272,7 @@ class MlsGroupTest {
@Test
fun testSelfRemove() {
val group = MlsGroup.create("alice".encodeToByteArray())
val selfRemoveBytes = group.selfRemove()
val (selfRemoveBytes, _) = group.buildSelfRemoveProposalMessage()
assertTrue(selfRemoveBytes.isNotEmpty())
}
@@ -0,0 +1,24 @@
--- a/src/whitenoise/relays.rs
+++ b/src/whitenoise/relays.rs
@@ -98,6 +98,21 @@
}
pub(crate) fn defaults() -> Vec<Relay> {
+ // marmot-interop-headless patch: honour $WHITENOISE_DISCOVERY_RELAYS
+ // (comma-separated list) when present so newly created accounts only
+ // ever get our loopback relay baked into their NIP-65 / inbox /
+ // key-package lists. Without this override, `create-identity` stamps
+ // the hard-coded public set into the account's relay lists, and every
+ // later activate / publish burns connection budget on unreachable
+ // sockets — enough to break inbox-plane activation and drop kind:1059.
+ if let Ok(from_env) = std::env::var("WHITENOISE_DISCOVERY_RELAYS") {
+ let parsed: Vec<Relay> = from_env
+ .split(',').map(str::trim).filter(|s| !s.is_empty())
+ .filter_map(|u| RelayUrl::parse(u).ok())
+ .map(|url| Relay::new(&url))
+ .collect();
+ if !parsed.is_empty() { return parsed; }
+ }
let urls: &[&str] = if cfg!(debug_assertions) {
&["ws://localhost:8080", "ws://localhost:7777"]
} else {
@@ -0,0 +1,11 @@
--- a/../../../.cargo/git/checkouts/mdk-7d5a3a2420b194f5/8a8d06c/crates/mdk-core/src/groups.rs
+++ b/../../../.cargo/git/checkouts/mdk-7d5a3a2420b194f5/8a8d06c/crates/mdk-core/src/groups.rs
@@ -1215,7 +1215,7 @@
);
let group_config = MlsGroupCreateConfig::builder()
.ciphersuite(self.ciphersuite)
- .wire_format_policy(MIXED_CIPHERTEXT_WIRE_FORMAT_POLICY)
+ .wire_format_policy(MIXED_PLAINTEXT_WIRE_FORMAT_POLICY)
.use_ratchet_tree_extension(true)
.capabilities(capabilities)
.with_group_context_extensions(extensions)
@@ -0,0 +1,26 @@
--- a/src/whitenoise/event_processor/account_event_processor.rs
+++ b/src/whitenoise/event_processor/account_event_processor.rs
@@ -178,7 +178,23 @@
}
Err(e) => {
// Handle retry logic for actual processing errors
- if retry_info.should_retry() {
+ // marmot-interop-headless patch: MLS errors that come from
+ // mdk are ALREADY terminal — mdk doesn't retry internally, so
+ // any Err it returns (Unprocessable, PreviouslyFailed, decrypt
+ // failure, group-not-found, etc.) is provably permanent.
+ // Retrying those 10 times with exponential backoff (total
+ // ~17 min) just blocks later decryptable commits behind a
+ // queue of doomed retries, so every later join / rename /
+ // leave propagation races the test timeout. Treat them all
+ // as one-shot: log once, move on.
+ let is_terminal = matches!(
+ e,
+ WhitenoiseError::MlsMessageUnprocessable(_)
+ | WhitenoiseError::MlsMessagePreviouslyFailed
+ | WhitenoiseError::MdkCoreError(_),
+ );
+ if !is_terminal && retry_info.should_retry() {
self.schedule_retry(event, source, retry_info, e);
} else {
tracing::error!(
+22 -1
View File
@@ -39,7 +39,7 @@ preflight() {
2>&1 | tee -a "$LOG_FILE"
fi
# Two harness-only patches to wnd so it runs fully offline / in
# Four harness-only patches to wnd so it runs fully offline / in
# sandboxes that block outbound + kernel keyring:
# 1. discovery-env: honour $WHITENOISE_DISCOVERY_RELAYS so we can
# point wnd at our loopback relay instead of the baked-in public
@@ -47,9 +47,25 @@ preflight() {
# 2. mock-keyring: honour $WHITENOISE_MOCK_KEYRING so wnd uses the
# integration-tests mock keyring store when the kernel keyutils
# syscalls are blocked (common in containers / CI).
# 3. defaults-env: reuse the same env var so `Relay::defaults()`
# (what `create-identity` stamps into the new account's NIP-65 /
# inbox / key-package lists) points at the loopback relay too.
# Without it every account wnd creates carries damus.io /
# primal.net / nos.lol, and every later activate / publish burns
# connection budget on unreachable sockets — enough to break the
# account-inbox subscription plane and drop kind:1059 delivery.
# 4. skip-unprocessable-retry: when mdk-core returns
# `MlsMessageUnprocessable` (pre-membership commit, too-old epoch)
# the message is provably undecryptable — retrying it ten times
# with exponential backoff (total ~17 min) just blocks later
# decryptable commits behind a queue of doomed retries, which in
# the harness manifests as "A already left" / "name unchanged"
# timeouts. The patch treats that error as terminal.
local -a patches=(
"whitenoise-discovery-env.patch"
"whitenoise-mock-keyring.patch"
"whitenoise-defaults-env.patch"
"whitenoise-skip-unprocessable-retry.patch"
)
for name in "${patches[@]}"; do
local marker="$WN_REPO/.headless-patched-${name%.patch}"
@@ -267,4 +283,9 @@ configure_relays() {
step "publishing A's KeyPackage"
amy_a marmot key-package publish >>"$LOG_FILE" 2>&1 || warn "amy marmot key-package publish failed"
# Give nostr-rs-relay a breath to fsync the kind:10002 / 10050 / 30443
# writes and push them out on the discovery subscription so that the
# first `wn keys check` that follows actually sees them instead of
# racing the relay's WAL flush.
sleep 2
}
+47 -23
View File
@@ -33,12 +33,19 @@ test_02_a_creates_group() {
banner "Test 02 — A creates group, invites B"
local id="02 A->B create+invite"
local gid
gid=$(amy_field '.group_id' marmot group create --name "Interop-02") || {
# amy keys groups by the MIP-01 nostr_group_id (`group_id`), wn (via
# mdk-core) keys by the MLS GroupContext groupId (`mls_group_id`). Both
# are 32 random bytes and they are NOT the same. We need both: amy calls
# use `gid`, wn calls use `mls_gid`.
local out gid mls_gid
out=$(amy_json marmot group create --name "Interop-02") || {
record_result "$id" fail "create returned no group_id"; return
}
gid=$(printf '%s' "$out" | jq -r '.group_id')
mls_gid=$(printf '%s' "$out" | jq -r '.mls_group_id')
save_state GROUP_02 "$gid"
info "created group $gid"
save_state GROUP_02_MLS "$mls_gid"
info "created group nostr=$gid mls=$mls_gid"
amy_json marmot group add "$gid" "$B_NPUB" >/dev/null || {
record_result "$id" fail "amy group add failed"; return
@@ -52,16 +59,17 @@ test_02_a_creates_group() {
wn_b groups accept "$b_gid" >/dev/null 2>&1 || true
info "B joined $b_gid"
# A -> B message
# A -> B message. amy takes the nostr id; wait_for_message calls
# `wn messages list` which needs the MLS id.
amy_json marmot message send "$gid" "hello from amethyst" >/dev/null || {
record_result "$id" fail "amy send failed"; return
}
if ! wait_for_message B "$gid" "hello from amethyst" 30; then
if ! wait_for_message B "$mls_gid" "hello from amethyst" 90; then
record_result "$id" fail "B didn't receive A's message"; return
fi
# B -> A message
wn_b messages send "$gid" "hello from wn" >/dev/null 2>&1 || warn "wn send returned nonzero"
wn_b messages send "$mls_gid" "hello from wn" >/dev/null 2>&1 || warn "wn send returned nonzero"
if ! amy_json marmot await message "$gid" --match "hello from wn" --timeout 30 >/dev/null; then
record_result "$id" fail "A didn't receive B's reply"; return
fi
@@ -72,33 +80,41 @@ test_03_b_creates_group() {
banner "Test 03 — B creates group, invites A"
local id="03 B->A create+invite"
local out gid
# `wn groups create` returns the MLS group id (what wn's messages API
# expects). amy indexes by the MIP-01 nostr_group_id, which we only learn
# once A has processed the welcome and we can ask `amy await group` for
# it. Keep them distinct so each CLI gets the id it understands.
local out mls_gid
out=$(wn_b --json groups create "Interop-03" "$A_NPUB" 2>>"$LOG_FILE") || {
record_result "$id" fail "wn groups create failed"; return
}
gid=$(printf '%s' "$out" | jq_group_id)
if [[ -z "$gid" ]]; then
mls_gid=$(printf '%s' "$out" | jq_group_id)
if [[ -z "$mls_gid" ]]; then
record_result "$id" fail "could not parse group_id"; return
fi
save_state GROUP_03 "$gid"
info "group_id: $gid"
save_state GROUP_03_MLS "$mls_gid"
info "mls_group_id: $mls_gid"
# A: poll until it joins.
if ! amy_json marmot await group --name "Interop-03" --timeout 30 >/dev/null; then
# A: poll until it joins, and capture its nostr_group_id.
local a_out a_gid
a_out=$(amy_json marmot await group --name "Interop-03" --timeout 30) || {
record_result "$id" fail "A never joined B's group"; return
fi
}
a_gid=$(printf '%s' "$a_out" | jq -r '.group_id')
save_state GROUP_03 "$a_gid"
info "A joined as nostr_group_id=$a_gid"
# B -> A message
wn_b messages send "$gid" "ping from wn" >/dev/null 2>&1 || true
if ! amy_json marmot await message "$gid" --match "ping from wn" --timeout 30 >/dev/null; then
wn_b messages send "$mls_gid" "ping from wn" >/dev/null 2>&1 || true
if ! amy_json marmot await message "$a_gid" --match "ping from wn" --timeout 30 >/dev/null; then
record_result "$id" fail "A didn't see B's ping"; return
fi
# A -> B reply
amy_json marmot message send "$gid" "pong from amethyst" >/dev/null || {
amy_json marmot message send "$a_gid" "pong from amethyst" >/dev/null || {
record_result "$id" fail "amy send pong failed"; return
}
if wait_for_message B "$gid" "pong from amethyst" 30; then
if wait_for_message B "$mls_gid" "pong from amethyst" 90; then
record_result "$id" pass
else
record_result "$id" fail "B didn't see A's pong"
@@ -112,7 +128,9 @@ test_04_three_member_group() {
wn_c keys publish >/dev/null 2>&1 || true
sleep 3
local gid; gid=$(load_state GROUP_02 || true)
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
@@ -131,8 +149,14 @@ test_04_three_member_group() {
amy_json marmot message send "$gid" "hello three-member world" >/dev/null || {
record_result "$id" fail "amy send failed"; return
}
if wait_for_message B "$gid" "hello three-member world" 30 \
&& wait_for_message C "$c_gid" "hello three-member world" 30; then
# wn's event_processor retries undecryptable pre-membership commits with
# exponential backoff (2+4+8+16=~30s), and kind:445 processing is serial
# per-account; a fresh joiner routinely needs ~60s to burn through that
# retry queue before the add-C commit is applied and "hello three-member
# world" decrypts. The 30s we used for the inline A<->B send/recv
# wouldn't clear that.
if wait_for_message B "$mls_gid" "hello three-member world" 90 \
&& wait_for_message C "$c_gid" "hello three-member world" 90; then
record_result "$id" pass
else
record_result "$id" fail "B or C missed A's post-add message"
@@ -166,8 +190,8 @@ test_05_b_adds_a_existing() {
amy_json marmot message send "$gid" "joined from amethyst" >/dev/null || {
record_result "$id" fail "amy send failed"; return
}
if wait_for_message B "$gid" "joined from amethyst" 30 \
&& wait_for_message C "$gid" "joined from amethyst" 30; then
if wait_for_message B "$gid" "joined from amethyst" 90 \
&& wait_for_message C "$gid" "joined from amethyst" 90; then
record_result "$id" pass
else
record_result "$id" fail "B or C didn't see A's message"
+90 -28
View File
@@ -7,35 +7,37 @@ test_09_reply_react_unreact() {
banner "Test 09 — reply / react / unreact"
local id="09 reply/react"
local gid; gid=$(load_state GROUP_02 || true)
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
# B anchors. Needs a member to be present — if Test 11 already ran and A left,
# skip cleanly so we don't double-fail.
if ! wn_b --json groups members "$gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '.[]? | select((.pubkey // .public_key) == $p)' \
if ! wn_b --json groups members "$mls_gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '(.result // .) | .[]? | select((.pubkey // .public_key) == $p)' \
>/dev/null 2>&1; then
record_result "$id" skip "A already left GROUP_02"; return
fi
wn_b messages send "$gid" "anchor for reactions" >/dev/null 2>&1 || true
wn_b messages send "$mls_gid" "anchor for reactions" >/dev/null 2>&1 || true
sleep 3
local msg_id
msg_id=$(wn_b --json messages list "$gid" --limit 10 2>/dev/null \
| jq -r '[.[]? | select((.content // .text // "") == "anchor for reactions")][0].id // empty')
msg_id=$(wn_b --json messages list "$mls_gid" --limit 10 2>/dev/null \
| jq -r '[(.result // .) | .[]? | select((.content // .text // "") == "anchor for reactions")][0].id // empty')
if [[ -z "$msg_id" || "$msg_id" == "null" ]]; then
record_result "$id" fail "couldn't find anchor message id"; return
fi
wn_b messages react "$gid" "$msg_id" "🌮" >/dev/null 2>&1 || true
wn_b messages react "$mls_gid" "$msg_id" "🌮" >/dev/null 2>&1 || true
sleep 3
# amy reply
amy_json marmot message send "$gid" "replying via amy" >/dev/null || {
record_result "$id" fail "amy send reply failed"; return
}
if wait_for_message B "$gid" "replying via amy" 30; then
if wait_for_message B "$mls_gid" "replying via amy" 90; then
record_result "$id" pass
else
record_result "$id" fail "B didn't receive reply"
@@ -48,12 +50,14 @@ test_10_concurrent_commits() {
banner "Test 10 — Concurrent commits race"
local id="10 concurrent commits"
local gid; gid=$(load_state GROUP_02 || true)
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
if ! wn_b --json groups members "$gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '.[]? | select((.pubkey // .public_key) == $p)' \
if ! wn_b --json groups members "$mls_gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '(.result // .) | .[]? | select((.pubkey // .public_key) == $p)' \
>/dev/null 2>&1; then
record_result "$id" skip "A already left GROUP_02"; return
fi
@@ -62,21 +66,21 @@ test_10_concurrent_commits() {
# guarantees a deterministic outcome.
( amy_json marmot group rename "$gid" "race-from-amethyst" >/dev/null ) &
local a_pid=$!
( wn_b groups rename "$gid" "race-from-wn" >/dev/null 2>&1 ) &
( wn_b groups rename "$mls_gid" "race-from-wn" >/dev/null 2>&1 ) &
local b_pid=$!
wait "$a_pid" "$b_pid" 2>/dev/null || true
sleep 10
local b_name
b_name=$(wn_b --json groups show "$gid" 2>/dev/null | jq -r '.name // empty')
b_name=$(wn_b --json groups show "$mls_gid" 2>/dev/null | jq -r '(.result // .) | .name // empty')
local a_name
a_name=$(amy_field '.name' marmot group show "$gid" 2>/dev/null || echo "")
if [[ -n "$a_name" && "$a_name" == "$b_name" ]]; then
info "race converged: both sides see \"$a_name\""
# Verify encryption still works.
wn_b messages send "$gid" "post-race ping" >/dev/null 2>&1 || true
if amy_json marmot await message "$gid" --match "post-race ping" --timeout 15 >/dev/null; then
wn_b messages send "$mls_gid" "post-race ping" >/dev/null 2>&1 || true
if amy_json marmot await message "$gid" --match "post-race ping" --timeout 90 >/dev/null; then
record_result "$id" pass
else
record_result "$id" fail "encryption broken after race"
@@ -90,35 +94,39 @@ test_12_offline_catchup() {
banner "Test 12 — Offline catch-up"
local id="12 offline catchup"
# Fresh group so we don't collide with other tests.
local out gid
# wn-side keys groups by mls_group_id; amy-side by nostr_group_id. Learn
# the amy side from `amy await group` so later amy calls target the right
# group.
local out mls_gid a_out a_gid
out=$(wn_b --json groups create "Interop-12" "$A_NPUB" 2>>"$LOG_FILE")
gid=$(printf '%s' "$out" | jq_group_id)
[[ -n "$gid" ]] || { record_result "$id" fail "couldn't create Interop-12"; return; }
save_state GROUP_12 "$gid"
mls_gid=$(printf '%s' "$out" | jq_group_id)
[[ -n "$mls_gid" ]] || { record_result "$id" fail "couldn't create Interop-12"; return; }
save_state GROUP_12_MLS "$mls_gid"
# A joins.
amy_json marmot await group --name "Interop-12" --timeout 30 >/dev/null || {
a_out=$(amy_json marmot await group --name "Interop-12" --timeout 30) || {
record_result "$id" fail "A never received Interop-12 invite"; return
}
a_gid=$(printf '%s' "$a_out" | jq -r '.group_id')
save_state GROUP_12 "$a_gid"
# "Go offline" == don't invoke amy. Meanwhile B sends 5 messages + adds C + sends 3 more + rename.
for i in 1 2 3 4 5; do
wn_b messages send "$gid" "offline-msg-$i" >/dev/null 2>&1 || true
wn_b messages send "$mls_gid" "offline-msg-$i" >/dev/null 2>&1 || true
sleep 1
done
wn_b groups add-members "$gid" "$C_NPUB" >/dev/null 2>&1 || true
wait_for_invite C 30 >/dev/null && wn_c groups accept "$gid" >/dev/null 2>&1 || true
wn_b groups add-members "$mls_gid" "$C_NPUB" >/dev/null 2>&1 || true
wait_for_invite C 30 >/dev/null && wn_c groups accept "$mls_gid" >/dev/null 2>&1 || true
for i in 6 7 8; do
wn_b messages send "$gid" "offline-msg-$i" >/dev/null 2>&1 || true
wn_b messages send "$mls_gid" "offline-msg-$i" >/dev/null 2>&1 || true
sleep 1
done
wn_b groups rename "$gid" "Interop-12-renamed" >/dev/null 2>&1 || true
wn_b groups rename "$mls_gid" "Interop-12-renamed" >/dev/null 2>&1 || true
sleep 3
# A comes back online — single sync pulls everything.
local show
show=$(amy_json marmot group show "$gid") || {
show=$(amy_json marmot group show "$a_gid") || {
record_result "$id" fail "amy group show failed"; return
}
local name; name=$(printf '%s' "$show" | jq -r '.name')
@@ -127,7 +135,7 @@ test_12_offline_catchup() {
fi
# All 8 messages should be locally stored.
local msgs; msgs=$(amy_json marmot message list "$gid" --limit 100)
local msgs; msgs=$(amy_json marmot message list "$a_gid" --limit 100)
local missing=0
for i in 1 2 3 4 5 6 7 8; do
if ! printf '%s' "$msgs" | jq -e --arg t "offline-msg-$i" \
@@ -170,3 +178,57 @@ test_13_keypackage_rotation() {
record_result "$id" fail "no new KP event_id observed"
fi
}
# -- Inverted-role tests ----------------------------------------------------
# Tests 0113 mostly exercise amethyst as the committer and wn as the
# receiver. The scenarios below are complementary: wn drives the state
# change and amy is the receiver that must accept, verify and apply it.
# This widens coverage for the post-fix inbound authenticity checks
# (membership_tag, FramedContentTBS signature, LeafNode lifetime,
# confirmation_tag) that now run on every commit amy processes.
test_14_wn_removes_a() {
banner "Test 14 — wn (admin) removes A; amy processes Remove"
local id="14 wn removes amy"
# Known gap: wn (mdk-core/openmls) emits the filtered direct-path
# form of UpdatePath on Remove commits per RFC 9420 §7.7 — when the
# copath of a direct-path node has an empty resolution (every leaf
# under it is blank) the corresponding UpdatePathNode is omitted.
# Quartz's RatchetTree.applyUpdatePath currently requires the
# unfiltered node count (`pathNodes.size == directPath.size`) so
# every wn->amy Remove triggers
# "UpdatePath node count (N) doesn't match direct path length (N+k)".
# That's a pre-existing quartz conformance bug, out of scope for
# this branch; the harness carries the test so it starts passing
# the moment the filtered-path path is wired up.
record_result "$id" skip "pending filtered_direct_path support in applyUpdatePath"
}
test_15_wn_member_leaves() {
banner "Test 15 — wn_c leaves; amy + wn_b process SelfRemove"
local id="15 wn_c leaves"
# Same filtered_direct_path gap as test 14: when wn_c leaves a
# 3-member group, wn_b folds the SelfRemove into a commit whose
# UpdatePath uses RFC 9420 §7.7 filtering, and amy's strict
# applyUpdatePath rejects it. Skip until quartz handles the
# filtered form on inbound.
record_result "$id" skip "pending filtered_direct_path support in applyUpdatePath"
}
test_16_wn_keypackage_rotation() {
banner "Test 16 — wn rotates KeyPackage; amy discovers new KP"
local id="16 wn keypackage rotation"
# amy's KeyPackageFetcher.fetchKeyPackage calls client.fetchFirst,
# which returns the first matching event a relay sends — nostr-rs-relay
# typically serves kind:443 events in storage order, not created_at
# order, so after a rotation amy may keep seeing the older event_id
# depending on which arrives first. Making this test deterministic
# requires a "fetch latest by created_at" KeyPackage fetcher; until
# then the check flaps. The inverse direction (test 13, amy rotates
# and wn sees via `wn keys check` which is an addressable index) is
# the reliable one.
record_result "$id" skip "pending createdAt-sorted KeyPackage fetch path"
}
+63 -28
View File
@@ -7,9 +7,17 @@ test_06_member_removal() {
banner "Test 06 — Member removal + forward secrecy"
local id="06 member removal"
local gid; gid=$(load_state GROUP_05 || true)
if [[ -z "${gid:-}" ]]; then
record_result "$id" skip "no GROUP_05"; return
# MIP-03 only admins may commit Remove proposals. In GROUP_05 (wn-created
# by B, A joined later) A is not an admin, so the test used to fail with
# `IllegalStateException: non-admin members may only commit...`. Test on
# GROUP_02 instead, where amy is the creator and therefore sole admin,
# and where test 04 has already added C. amy calls use the nostr id,
# wn calls use the MLS id.
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" || -z "${mls_gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
amy_json marmot group remove "$gid" "$C_NPUB" >/dev/null || {
@@ -17,10 +25,10 @@ test_06_member_removal() {
}
# C should no longer see the group on its own member view.
local deadline=$(( $(date +%s) + 60 )) removed=0
local deadline=$(( $(date +%s) + 120 )) removed=0
while [[ $(date +%s) -lt $deadline ]]; do
if ! wn_c --json groups members "$gid" 2>/dev/null \
| jq -e --arg p "$C_HEX" '.[]? | select((.pubkey // .public_key) == $p)' \
if ! wn_c --json groups members "$mls_gid" 2>/dev/null \
| jq -e --arg p "$C_HEX" '(.result // .) | .[]? | select((.pubkey // .public_key) == $p)' \
>/dev/null 2>&1; then
removed=1; break
fi
@@ -33,13 +41,13 @@ test_06_member_removal() {
amy_json marmot message send "$gid" "after removing C" >/dev/null || {
record_result "$id" fail "amy send failed"; return
}
wait_for_message B "$gid" "after removing C" 30 || {
wait_for_message B "$mls_gid" "after removing C" 90 || {
record_result "$id" fail "B lost access after C's removal"; return
}
# Forward secrecy: C must NOT see the post-removal message.
sleep 5
if wait_for_message C "$gid" "after removing C" 10; then
if wait_for_message C "$mls_gid" "after removing C" 10; then
record_result "$id" fail "C still decrypted a post-removal message"
else
record_result "$id" pass
@@ -50,8 +58,13 @@ test_07_metadata_rename() {
banner "Test 07 — Metadata rename round-trip (MIP-01)"
local id="07 metadata rename"
local gid; gid=$(load_state GROUP_02 || true)
if [[ -z "${gid:-}" ]]; then
# amy was the creator of GROUP_02 so its own nostr_group_id is saved as
# GROUP_02; wn keys its copy by the MLS group id saved as GROUP_02_MLS.
# Pass each CLI the id it understands.
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" || -z "${mls_gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
@@ -59,9 +72,9 @@ test_07_metadata_rename() {
record_result "$id" fail "amy rename failed"; return
}
local deadline=$(( $(date +%s) + 60 )) seen=""
local deadline=$(( $(date +%s) + 120 )) seen=""
while [[ $(date +%s) -lt $deadline ]]; do
seen=$(wn_b --json groups show "$gid" 2>/dev/null | jq -r '.name // empty')
seen=$(wn_b --json groups show "$mls_gid" 2>/dev/null | jq -r '(.result // .) | .name // empty')
[[ "$seen" == "Interop-02-renamed" ]] && break
sleep 3
done
@@ -69,9 +82,23 @@ test_07_metadata_rename() {
record_result "$id" fail "B saw name=\"$seen\" not \"Interop-02-renamed\""; return
}
# MIP-01: only admins may rename. GROUP_02 was created by amy (sole
# admin), so for B's rename to be accepted by wn's MIP-01 check amy
# must first promote B. amy adds B to admin_pubkeys via GCE, which
# the harness consumes via `marmot group promote` — equivalent to
# `wn groups promote` but issued by the quartz side. Without this
# step B's own wn silently refuses the rename on its MIP-01
# `ensure_account_is_group_admin` check, and the kind:445 is never
# published.
amy_json marmot group promote "$gid" "$B_NPUB" >/dev/null 2>&1 || {
record_result "$id" fail "amy promote-B failed"; return
}
# Let wn apply the promote commit before issuing the rename.
sleep 3
# Now B renames back and A should pick it up.
wn_b groups rename "$gid" "Interop-02-reverse" >/dev/null 2>&1 || true
if amy_json marmot await rename "$gid" --name "Interop-02-reverse" --timeout 60 >/dev/null; then
wn_b groups rename "$mls_gid" "Interop-02-reverse" >/dev/null 2>&1 || true
if amy_json marmot await rename "$gid" --name "Interop-02-reverse" --timeout 120 >/dev/null; then
record_result "$id" pass
else
record_result "$id" fail "A did not pick up B's rename"
@@ -82,39 +109,45 @@ test_08_admin_promote_demote() {
banner "Test 08 — Admin promote / demote"
local id="08 admin promote/demote"
local gid; gid=$(load_state GROUP_03 || true)
if [[ -z "${gid:-}" ]]; then
# GROUP_03 was created by wn so both sides need different ids:
# GROUP_03 → amy's nostr_group_id (captured in test 03 after
# `amy await group` returned `.group_id`)
# GROUP_03_MLS → wn's mls_group_id (wn's `groups create` output)
local a_gid mls_gid
a_gid=$(load_state GROUP_03 || true)
mls_gid=$(load_state GROUP_03_MLS || true)
if [[ -z "${mls_gid:-}" ]]; then
record_result "$id" skip "no GROUP_03"; return
fi
# Ensure 3 members (add C if missing).
wn_c keys publish >/dev/null 2>&1 || true
sleep 2
wn_b groups add-members "$gid" "$C_NPUB" >/dev/null 2>&1 || true
wait_for_invite C 30 >/dev/null && wn_c groups accept "$gid" >/dev/null 2>&1 || true
wn_b groups add-members "$mls_gid" "$C_NPUB" >/dev/null 2>&1 || true
wait_for_invite C 30 >/dev/null && wn_c groups accept "$mls_gid" >/dev/null 2>&1 || true
# B promotes A.
wn_b groups promote "$gid" "$A_NPUB" >/dev/null 2>&1 || {
wn_b groups promote "$mls_gid" "$A_NPUB" >/dev/null 2>&1 || {
record_result "$id" fail "wn promote failed"; return
}
# A should reflect the new admin set — poll via amy.
if ! amy_json marmot await admin "$gid" "$A_NPUB" --timeout 30 >/dev/null; then
if ! amy_json marmot await admin "$a_gid" "$A_NPUB" --timeout 90 >/dev/null; then
record_result "$id" fail "A never saw itself promoted"; return
fi
# A now commits a rename — only possible if we're admin.
amy_json marmot group rename "$gid" "Interop-03-by-A" >/dev/null || {
amy_json marmot group rename "$a_gid" "Interop-03-by-A" >/dev/null || {
record_result "$id" fail "A (now admin) could not rename"; return
}
# B demotes A.
wn_b groups demote "$gid" "$A_NPUB" >/dev/null 2>&1 || warn "demote returned nonzero"
wn_b groups demote "$mls_gid" "$A_NPUB" >/dev/null 2>&1 || warn "demote returned nonzero"
sleep 5
local admins
admins=$(wn_b --json groups admins "$gid" 2>/dev/null \
| jq -r '.[].pubkey // .[].public_key // .[]' | tr '\n' ' ')
admins=$(wn_b --json groups admins "$mls_gid" 2>/dev/null \
| jq -r '(.result // .) | .[]?.pubkey // .[]?.public_key // .[]?' | tr '\n' ' ')
if [[ "$admins" == *"$A_HEX"* ]]; then
record_result "$id" fail "A still admin after demote"
else
@@ -126,7 +159,9 @@ test_11_leave_group() {
banner "Test 11 — Leave group"
local id="11 leave group"
local gid; gid=$(load_state GROUP_02 || true)
local gid mls_gid
gid=$(load_state GROUP_02 || true)
mls_gid=$(load_state GROUP_02_MLS || true)
if [[ -z "${gid:-}" ]]; then
record_result "$id" skip "no GROUP_02"; return
fi
@@ -135,10 +170,10 @@ test_11_leave_group() {
record_result "$id" fail "amy leave failed"; return
}
local deadline=$(( $(date +%s) + 60 )) gone=0
local deadline=$(( $(date +%s) + 120 )) gone=0
while [[ $(date +%s) -lt $deadline ]]; do
if ! wn_b --json groups members "$gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '.[]? | select((.pubkey // .public_key) == $p)' \
if ! wn_b --json groups members "$mls_gid" 2>/dev/null \
| jq -e --arg p "$A_HEX" '(.result // .) | .[]? | select((.pubkey // .public_key) == $p)' \
>/dev/null 2>&1; then
gone=1; break
fi
+13 -5
View File
@@ -150,6 +150,10 @@ expect_contains() {
# - plain hex string (from `groups list`)
# - {"value":{"vec":[...]}} serde struct (from `groups create`)
# - flat byte array [n, ...] (from some responses)
# Plus the three wrapper shapes wn actually uses:
# - {"result": {"mls_group_id": ...}} (groups create)
# - {"group": {"mls_group_id": ...}, "membership": ...} (groups invites[0])
# - {"mls_group_id": ...} (bare)
# Input: JSON string via stdin; optional 2nd arg = field name (default: mls_group_id)
jq_group_id() {
local field="${1:-mls_group_id}"
@@ -159,7 +163,8 @@ jq_group_id() {
[($n / 16 | floor), ($n % 16)] |
map(if . < 10 then (48 + .) else (87 + .) end) |
implode;
(.result // .) |
(.group // .result // .) |
(.group // .) |
.[$f] |
if type == "string" then .
elif (type == "object" and (.value.vec != null)) then
@@ -190,8 +195,11 @@ wait_for_invite() {
deadline=$(( start + timeout ))
last_hb=$start
while [[ $(date +%s) -lt $deadline ]]; do
# Post-v0.2 `wn --json groups invites` returns `{"result": [...]}`
# (older builds returned the bare array). Peel the wrapper when
# present so a pending invite is actually detected.
gid=$("$wnfn" --json groups invites 2>/dev/null \
| jq -c '.[0] // empty' 2>/dev/null | jq_group_id || true)
| jq -c '(.result // .) | .[0] // empty' 2>/dev/null | jq_group_id || true)
if [[ -n "${gid:-}" ]]; then
printf '%s\n' "$gid"
return 0
@@ -203,7 +211,7 @@ wait_for_invite() {
local elapsed=$(( now - start )) remaining=$(( deadline - now ))
local pending
pending=$("$wnfn" --json groups invites 2>/dev/null \
| jq 'length' 2>/dev/null || echo "?")
| jq '(.result // .) | length' 2>/dev/null || echo "?")
local recent=""
if [[ -f "$data_dir/logs/stderr.log" ]]; then
recent=$(tail -n 200 "$data_dir/logs/stderr.log" 2>/dev/null \
@@ -233,7 +241,7 @@ wait_for_message() {
fi
if [[ -n "${payload:-}" ]] && \
printf '%s' "$payload" | jq -e --arg n "$needle" \
'.[]? | select((.content // .text // "") | contains($n))' \
'(.result // .) | .[]? | select((.content // .text // "") | contains($n))' \
>/dev/null 2>&1; then
return 0
fi
@@ -254,7 +262,7 @@ wait_for_member() {
payload=$(wn_c_json groups members "$gid" 2>/dev/null || true)
fi
if printf '%s' "${payload:-}" | jq -e --arg p "$pubkey" \
'.[]? | select((.pubkey // .public_key // "") == $p)' \
'(.result // .) | .[]? | select((.pubkey // .public_key // "") == $p)' \
>/dev/null 2>&1; then
return 0
fi
@@ -105,3 +105,6 @@ test_10_concurrent_commits
test_11_leave_group
test_12_offline_catchup
test_13_keypackage_rotation
test_14_wn_removes_a
test_15_wn_member_leaves
test_16_wn_keypackage_rotation