Skip to content

fix(analytics): count undecrypted packets apart from the named channel sharing their hash byte - #2397

Open
efiten wants to merge 7 commits into
Kpa-clawbot:masterfrom
efiten:fix/channel-analytics-encrypted-split
Open

efiten wants to merge 7 commits into
Kpa-clawbot:masterfrom
efiten:fix/channel-analytics-encrypted-split

Conversation

@efiten

@efiten efiten commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

What changes for operators

Analytics > Channels showed a named channel with far more messages than it had. The channel hash on the wire is one byte, so many channels share each value. computeAnalyticsChannels grouped by that byte alone (the #978 fix, cmd/server/store.go), so every packet the ingestor could not decrypt was counted under the decrypted channel with the same byte.

Measured on a live instance (14 days, transmissions table):

Row in the table Shown Real
#coredrive (0x76) 6568 1365 decrypted, 5203 undecrypted from another channel
#bemesh (0x63) 3123 about 1184 decrypted, 1939 undecrypted
Public (0x11) 3842 3678 in the DB

An operator read the 6568 as "#coredrive carries almost as much traffic as Public and #bemesh together" and asked for the feature to be switched off.

The change

What this undoes from #978

#978 also merged "encrypted" and "decrypted" rows of the same byte, on the premise that one byte is one channel. Decryption happens once per transmission in the ingestor, so an undecrypted packet next to a decrypted channel with the same byte is a packet that channel's key did not open. TestComputeAnalyticsChannels_MergesEncryptedAndDecrypted asserted the merge; it is replaced by TestComputeAnalyticsChannels_UndecryptedNotCountedAsNamedChannel. The exception is history ingested before a key was added: those packets now show as their own Encrypted row instead of being counted under the name.

A packet the ingestor decrypted can carry no text and no sender (an empty message, or a 2-byte MAC that matched by chance); the live DB had 1 in 14 days (of 38758 GRP_TXT). It goes to its own channel's bucket as a decrypted row, and the bucket of undecrypted packets keeps its placeholder name.

Locally saved channel keys (public/analytics.js)

decorateAnalyticsChannels gives an Encrypted row the name of a channel key the visitor saved in the browser when the hash byte matches, and moves it under My Channels. That predates this PR. With the split it showed up as a duplicate: on a live instance with a #coredrive key saved, the 3349 undecrypted packets on byte 0x76 appeared as a second #coredrive row under My Channels, next to the 1632 the server decrypted under Network. Without the key they read Encrypted (0x76).

Fixed in buildHashKeyMap, compared by key rather than by name: for the bytes a locally saved key lands on, the page derives the keys of the channels the server decrypted there (#hashtag names derive from the name, Public has a fixed key). A local key equal to one of those on the same byte no longer names the bucket, so it stays Encrypted (0xNN): those are packets that key did not open. This catches the key however it was saved: by name, as hex, or by QR (stored as psk:<hex8> with the name only as a label). A local key the server does not hold still names the bucket, also when another decrypted channel shares the byte, and with two local keys on one byte the one the server holds no longer pushes out the other.

Not covered: a channel an operator configured in channelKeys under a private (non-#) name. The page cannot know that key, so a local copy of it still names the bucket, as before this PR. A test pins that.

Tests

Go (cmd/server/channel_analytics_test.go):

  • TestComputeAnalyticsChannels_UndecryptedNotCountedAsNamedChannel: 2 decrypted + 3 undecrypted on byte 129 give two rows, 2 and 3 messages.
  • TestComputeAnalyticsChannels_DecryptedChannelsSharingHashStaySeparate: Public and Club both on byte 17, both decrypted, give two rows.

The Go fix and its tests are one commit (ad9486e4). The red step, run afterwards: the tests from ad9486e4 on top of the base a0b4e6cf code fail on an assertion, not a build error:

--- FAIL: TestComputeAnalyticsChannels_UndecryptedNotCountedAsNamedChannel (0.00s)
    channel_analytics_test.go:86: expected 2 channel buckets, got 1: [map[encrypted:false hash:129 lastActivity:2026-05-01T12:00:00Z messages:5 name:#wardriving senders:2]]
--- FAIL: TestComputeAnalyticsChannels_DecryptedChannelsSharingHashStaySeparate (0.00s)
    channel_analytics_test.go:121: expected 2 channel buckets, got 1: [map[encrypted:false hash:17 lastActivity:2026-05-01T12:00:00Z messages:3 name:Public senders:2]]
FAIL

With store.go from ad9486e4, all 5 TestComputeAnalyticsChannels* tests pass. cd cmd/server && go vet . && go test .: ok (65.6 s).

Frontend (tests/unit/test-analytics-channels-integration.js), red then green as separate commits, running the real channel-decrypt.js with hash as a string like the server sends:

  • c0ccdb64 / be452765: a first, name-based version (2 failed, 25 passed, then 27 passed). It missed keys saved as psk:<hex8>.
  • 1cadc3e8 replaces those cases: key saved as #coredrive, as psk:<hex8> with and without a label, the Public key, a private key next to another decrypted channel, upper-case hex, a second local key on the same byte. On that commit: 4 failed, 27 passed.
  • ee0f1ffc: 35 passed (adds the reverse save order, the per-byte comparison and the private-name limit). The other unit suites that load analytics.js pass as well.

Not done

  • Browser check with the new frontend code: done against the E2E fixture on a local server. A locally saved key the server also decrypts with (#test, and the Public key) leaves the undecrypted bucket on its byte under Encrypted; a private key on a byte the server cannot open names that bucket under My Channels. The Public case needed 81f672e9: the fixture's rows are named public, from before the ingestor normalised the casing.
  • Both rows of one byte link to #/channels?ch=<byte>, as the split rows did before bug: analytics channels page shows duplicate entries (encrypted/decrypted split + rainbow-table collisions) #978. I did not change the channels page.
  • activeChannels goes up by the number of bytes that have both kinds of traffic.

…l sharing their hash byte

The channel hash is one byte, so private channels collide with named
ones. Grouping by the byte alone put every undecryptable 0x76 packet
under #coredrive: on a live instance 5203 of the 6568 messages shown
for it came from another channel. Undecrypted packets now share one
bucket per byte, decrypted packets go to the bucket of their own name.

@n30nex n30nex left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source review at ad9486e4, plus an actual-JS decoration probe: encrypted rows receive a saved local channel name and “My Channels” grouping when the hash byte matches a stored key. Without that key, they read “Encrypted”. analytics.js is unchanged, so this predates the PR; please qualify the description and cover both configurations in browser validation.

The combined code/test commit also lacks the recorded RED→GREEN checkpoints required by docs/agents/TDD.md. I have not run this PR's Go suite or browser checks.

…ypted channel left

Red: with a saved #coredrive key, the undecrypted bucket on byte 0x76
(traffic the server's #coredrive key did not open) is shown as a second
#coredrive row under My Channels.
…pted channel left

When the server decrypted a channel on a hash byte, the undecrypted bucket
on that byte holds packets that channel's key did not open. A locally saved
key with the same name no longer moves that bucket under My Channels with
the channel's name; it stays Encrypted (0xNN). A local key for a channel
the server did not decrypt still names the bucket.
@efiten

efiten commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed (commits c0ccdb64, be452765)

Thanks @n30nex, both points held.

  1. Saved local keys. Confirmed in a browser on a live instance that runs ad9486e4: with a #coredrive key saved, the 3349 undecrypted packets on byte 0x76 showed as a second #coredrive row under My Channels, next to the 1632 the server decrypted. Without the key they read Encrypted (0x76). Instead of only qualifying the description I fixed it: when the server already decrypted a channel with that name on that byte, the undecrypted bucket stays Encrypted (0xNN). A local key for a channel the server did not decrypt still names the bucket. The description has a new section on this.
  2. Red then green. The frontend change is two commits: c0ccdb64 adds the test (2 failed, 25 passed on that commit) and be452765 the fix (27 passed). For the Go part, which is one commit, I did not rewrite history. The description now shows the run of the ad9486e4 tests on the base a0b4e6cf code: both fail on an assertion (1 bucket where 2 are expected), and all 5 pass with the fix.

Not done: a browser check of the new frontend code, because the fixture has no byte collision between a decrypted and an undecrypted channel.

…hex8>

Red: a key added as hex or by QR is stored as psk:<hex8> with the name only
as a label, so the name comparison misses it and the undecrypted bucket on
its byte is still shown under My Channels (#coredrive with and without a
label, and the Public key). A second local key on the same byte must still
name the bucket when the server's key was saved last. The checks run the
real channel-decrypt.js and send hash as a string, like the server.
buildHashKeyMap is exposed for the test.
… not by name

buildHashKeyMap now takes the channel rows and ChannelDecrypt, derives the
keys of the channels the server decrypted on the bytes a stored key lands
on (#hashtag names, plus the fixed Public key), and leaves out a stored key
equal to one of them on the same byte. A key added as hex or by QR
(psk:<hex8>) is caught the same way as one saved by name. With two stored
keys on one byte, the one the server holds no longer pushes out the other.
The name comparison in decorateAnalyticsChannels from be45276 goes, it is
covered by this.

Not covered: a channel an operator configured in channelKeys under a
private name. The client cannot know that key, so a local copy keeps naming
the bucket; a test pins that.
@efiten

efiten commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Follow-up to my last comment (commits 1cadc3e8, ee0f1ffc)

The name comparison from be452765 missed a key added as hex or by QR: those are stored as psk:<hex8> with the name only as a label, so a locally saved #coredrive or Public key added that way still put the undecrypted bucket under My Channels.

  1. buildHashKeyMap now compares by key: for the bytes a local key lands on, it derives the keys of the channels the server decrypted there (#name from the name, the fixed Public key) and leaves out a local key equal to one of those on that byte. The name check in decorateAnalyticsChannels is gone again; that function is back to its pre-PR shape.
  2. Tests run the real channel-decrypt.js with hash as a string, like the server sends. 1cadc3e8 fails 4 of 31 (psk with and without label, the Public key, a second local key on the same byte); ee0f1ffc passes 35.
  3. Known limit, in the description and pinned by a test: a channel an operator configured in channelKeys under a private name. The page cannot know that key, so a local copy still names the bucket, as before.

Still not done: a browser check of the new frontend code (no byte collision in the fixture).

@zfouts zfouts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at ee0f1ffc. Requesting changes, but the fix is about two lines.

The split works for the usual case, and the frontend's key-by-key comparison in buildHashKeyMap is careful and well tested. CI is green, and master hasn't touched computeAnalyticsChannels since your base.

Blocking: a decrypted packet with an empty message renames the undecrypted bucket (cmd/server/store.go, the encrypted := / chKey / placeholder-upgrade lines)

  • decryptChannelMessage cuts the text at the first NUL (cmd/ingestor/decoder.go:518). When plaintext[5] == 0 the result is Text="", Sender="", Channel="#coredrive", decryptionStatus:"decrypted". That happens for a real empty message, and for about 1 in 256 of the 2-byte-MAC false positives.
  • On the server, encrypted is then true, so chKey = hash and the packet joins the shared undecrypted bucket for that byte.
  • The "upgrade bucket name" branch then renames that bucket from ch118 to #coredrive, and it stays encrypted.
  • decorateAnalyticsChannels then shows it under Network as a second #coredrive row with the other channel's thousands of messages. That's the misread this PR is meant to remove. It's also the "1 packet in 14 days carries a channel name without text or sender" in the description.

Suggested fix: key on the name instead of on whether the text is empty:

chKey := hash
if !isPlaceholderName(name) {
	chKey = hash + "\x00" + name
}

Then remove the rename-on-upgrade branch, since the placeholder bucket never needs a name. Alternatively, use encrypted := decoded.Text == "" && decoded.Sender == "" && !ingestorDecrypted. Either way, please add a Go test: 3 undecrypted packets plus 1 makeGrpTxWithStatus(118, "#coredrive", "", "", "decrypted") on the same byte, and assert that the ch118 bucket keeps its placeholder name and a count of 3.

Notes, none blocking:

  1. The frontend change still hasn't been checked in a browser, as the description says. A live instance with a saved #coredrive key would show it quickly.
  2. PUBLIC_CHANNEL_KEY in public/analytics.js copies the ingestor's builtinChannelKeys(). A comment pointing to cmd/ingestor/main.go would help keep them in sync.
  3. Both of the earlier review's points are addressed: the local-key comparison now goes by key, and the red-to-green record is in place.

…ed bucket

A packet the ingestor decrypted can carry no text and no sender: the
message was empty, or the 2-byte MAC matched by chance and the text stopped
at its first NUL. computeAnalyticsChannels took that as undecrypted, put it
in the shared bucket of its hash byte and renamed the bucket to the
packet's channel, so the undecrypted packets were shown again as that
channel's messages.

The bucket is now chosen by name: packets without a channel name share the
bucket of their byte, which keeps its placeholder name, and a named packet
goes to its own. A packet the ingestor marked decrypted is never counted as
encrypted. The rename branch and isPlaceholderName, its only user, are gone.
Packets stored before the ingestor normalised the channel casing carry
"public". buildHashKeyMap compared the server's names with "Public" only,
so with the Public key saved locally the undecrypted 0x11 bucket was named
"Public" under My Channels, next to the decrypted "public" row. Found in a
browser check against the E2E fixture, which has such rows.
@efiten

efiten commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed (commits d5fd7cf2, 81f672e9). Thanks, the empty-message case reproduced as you described.

  1. Empty decrypted message renamed the undecrypted bucket (d5fd7cf2). The bucket is now chosen by name, as you suggested: a packet whose name is ch<hash> goes to the shared bucket of its byte, any other name to its own. I also took your alternative: a packet the ingestor marked decrypted is never counted as encrypted, so an empty #coredrive message lands in the #coredrive bucket as a decrypted row. The rename branch is gone, and with it isPlaceholderName, which had no other caller. I compare with "ch"+hash instead, because the hash can also be ? or the hex channel_hash, which isPlaceholderName did not treat as a placeholder. New test TestComputeAnalyticsChannels_EmptyDecryptedKeepsUndecryptedBucket (3 undecrypted plus 1 empty decrypted on byte 118): it failed before the fix with one encrypted #coredrive bucket of 4 messages, and passes now with ch118 at 3 and #coredrive at 1.
  2. Browser check (note 1), against the E2E fixture on a local server:
    • #test key saved locally (the server decrypts #test too): Encrypted (0xD9) keeps its 2 packets under Encrypted, #test keeps its 35 under Network.
    • A private key on byte 195: that bucket shows under My Channels with the key's name.
    • The Public key saved locally: this showed a bug. The fixture's Public rows are named public, from before the ingestor normalised the casing, and buildHashKeyMap compared with Public only, so Encrypted (0x11) became a second "Public" row under My Channels. Fixed in 81f672e9 (case-insensitive compare, with a test); after the fix Encrypted (0x11) stays under Encrypted.
  3. PUBLIC_CHANNEL_KEY (note 2): the comment now points to builtinChannelKeys() in cmd/ingestor/main.go.

Server suite and test-analytics-channels-integration.js (36 assertions) pass locally. npx eslint public/analytics.js reports 2 errors, both on existing fetch calls this PR does not touch (lines 512 and 4939).

@zfouts zfouts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 81f672e9. Approving. Thanks for the quick turnaround.

  • d5fd7cf2: choosing the bucket by name != "ch"+hash and adding && !ingestorDecrypted to encrypted together fix the case: an empty decrypted message now lands in its own channel's bucket as a decrypted row. Comparing with "ch"+hash rather than isPlaceholderName is the better choice, because it also covers the ? and hex-hash names. With the rename branch gone, a bucket's name can no longer change after it is created. TestComputeAnalyticsChannels_EmptyDecryptedKeepsUndecryptedBucket pins it: 3 packets in ch118, encrypted, and 1 in #coredrive, decrypted.
  • 81f672e9: good catch on the fixture's lowercase public rows. The case-insensitive compare is the right fix, and it comes with a test.
  • PUBLIC_CHANNEL_KEY now points to builtinChannelKeys().
  • CI is green on 81f672e9, and the PR merges cleanly.

About the two ESLint errors: they come from the branch's older base (a0b4e6cf). On master those calls are already CSApi.fetch (e.g. public/analytics.js:538), and this PR doesn't touch those lines, so they disappear on merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants