Skip to content

Refactor: Move the relay boundary into core/attribution - #1700

Merged
huang195 merged 12 commits into
rossoctl:mainfrom
huang195:refactor/relay-boundary-1498
Nov 7, 2026
Merged

huang195 merged 12 commits into
rossoctl:mainfrom
huang195:refactor/relay-boundary-1498

Conversation

@huang195

@huang195 huang195 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #1498 (closed by #1682). Phase 2 of the Praxis prep (#1437), for the hairpin gateway (#1491).

What this changes

  1. The per-connection memo moves to core/attribution/conn.go. New: ConnKey, ConnKeyOf, Memo, (*Attributor).Chain, Program and SelfTraffic. The logic of the forward proxy's connProc, selfTraffic and bridgeProgram moves line for line. The forward proxy keeps its gates, its log lines and loopbackDests. One *Memo is still shared by a CONNECT and the requests that its bridged tunnel decrypts.
  2. wire.go gets the header X-Cortex-Client-Proc for Attribution.ClientProc, in the strip list. No existing header name changes.

One behaviour change

The forward proxy now also strips X-Cortex-Client-Proc from client requests. Before this PR, a client could send it to the upstream. TestForwardProxy_StripsCortexAttributionHeaders now checks all 7 headers, and it fails when the new header is not in the strip list.

Header names: #1491 differs from wire.go

This PR keeps the names in wire.go. #1491 is updated to match.

#1491 text wire.go Difference
x-cortex-session X-Cortex-Session Same header (names are case-insensitive)
x-cortex-program X-Cortex-Proc Different name and value: it carries "<pid>.<start>", not a program
"agent name" X-Cortex-Agent #1491 gave no name
— X-Cortex-Source, X-Cortex-Session-Header, X-Cortex-Chain-Session, X-Cortex-Client-Proc Not in #1491

Not in this PR

Checks run

  • In core: go vet ./..., gofmt -l ., go mod tidy -diff and go test ./.... go test -race on ./attribution/... and ./listener/....
  • New tests: conn_test.go (one lookup per memo, a failed lookup is kept, the 30 s listener cache for a hit and a miss, every listener must qualify).
  • cmd/cortex (full profile), cmd/cortex-envoy (envoy profile) and cmd/cortex-praxis build and vet.

Assisted-By: Claude (Anthropic AI) [email protected]

The forward proxy kept the attribution state of each client connection
in its own connProc type. A later hairpin listener (rossoctl#1491) must use the
same state. This change moves it to core/attribution.

New in core/attribution (conn.go):
- ConnKey and ConnKeyOf name a connection by its two ends.
- Memo holds the process chain and the listener cache of one
  connection.
- Chain looks up the chain once for each Memo. It gives the error only
  to the call that made the lookup.
- Program names the client program for the TLS bridge rules.
- SelfTraffic finds an agent that talks to its own local service.

The forward proxy keeps its gates, loopbackDests, noteSelfTraffic and
its slog lines. The Debug line for a failed lookup does not change.
The moved code reads the clock of the Attributor. In production that
clock is time.Now.

Part of rossoctl#1498.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Add Encode and Decode back to core/attribution/wire.go, from 2914e34^.
The hairpin listener (rossoctl#1491 W9) uses them. No listener calls them yet.

Changes from the old code:
- Add HeaderClientProc ("X-Cortex-Client-Proc") for ClientProc, so that
  Decode(Encode(a)) is equal to a for a worker below its launcher.
- Decode accepts only the sources that Resolve returns. It refuses
  "active" and "connection".
- Decode checks ClientProc as "<pid>.<start>" and refuses a control
  rune in Header.
- The comment says that the forward proxy strips the headers.

Encode strips every attribution header before it sets the new values.
A test shows that a field that is empty does not keep a forged value.
A test shows that Strip removes every header that Encode sets.

Part of rossoctl#1498.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Decode had three faults:
- It read only Source, Session and Proc to find an attribution. An
  Attribution with only Agent did not travel. Decode now reads all
  of the attribution headers.
- It refused only C0 and DEL runes. Decode now refuses invalid UTF-8
  and every rune for which unicode.IsControl is true, as
  session.validHeaderSessionID does.
- It accepted process keys that Proc.Key does not write, such as
  "01.2", "+1.2" and "-1.2". Decode now accepts a key only when
  Proc.Key writes it again the same, and refuses a negative value.

Part of rossoctl#1498.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Add sessionevent.Sink in core/listener/internal. It has the nine
session.Store methods that a listener calls at record time.
*session.Store satisfies it, and a compile-time assertion keeps the
signatures the same. This is the one interface to feed the control
plane (rossoctl#1497 item 2).

Each of forwardproxy, reverseproxy and extproc gets sink(). It returns
an untyped nil when Sessions is nil. The record calls use sink(). The
Sessions field and its type do not change. Every nil check still reads
the field. AddRecorder stays a call on the field, because it is
start-up wiring.

The one call that relied on a nil-safe *Store receiver is
TakesCaptures in forwardproxy's serveOutbound. It now reads
"s.Sessions != nil && s.sink().TakesCaptures()", which gives the same
result.

A new test checks that sink() gives an untyped nil without a store.

Part of rossoctl#1497

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
The strip test sent only 3 of the 7 attribution headers. It now sends
all 7, X-Cortex-Client-Proc included, and checks that the upstream
gets none of them. Without HeaderClientProc in wireHeaders, the test
fails.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Chain takes the memo as its last parameter, and SelfTraffic took it
as its second. SelfTraffic now takes it last too, before the two
methods become public API. The doc comments of the two methods also
say that m must not be nil. Both methods use m without a nil check.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
The three listeners had the same 4-line sink() body. sessionevent.SinkOf
now holds that body, and each sink() calls it. The call sites do not
change. The nil-store test moves to sessionevent. It also checks that a
store gives a non-nil Sink.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Encode sets each header with Set, so a trusted hop sends one value per
header. Decode read only the first value of a header that had more.
It now refuses the whole attribution when any wire header has more than
one value. Two new refusal cases cover this.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
One line of the comment had 108 characters. It now wraps at about 90,
as the lines near it do.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 requested a review from a team as a code owner November 7, 2026 03:14
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 49 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 49b40c6a-51cb-4afe-b750-1caa2c995a4a
Commits

Reviewing files that changed from the base of the PR and between d41332a and 85839c9.

Files selected for processing: 6

  • core/attribution/conn.go
  • core/attribution/conn_test.go
  • core/attribution/wire.go
  • core/listener/forwardproxy/process.go
  • core/listener/forwardproxy/process_test.go
  • core/listener/forwardproxy/tunnel.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

This is a clean move. I checked connProc/lookupChain/bridgeProgram/selfTraffic/agentsOwnService/listenerBehind against base line by line, and they behave the same. The once-per-connection lookup still logs a failure once at DEBUG. An invalid key still looks nothing up and logs nothing. affinityClient was already AgentOf. a.now() falls back to time.Now() because the forward proxy sets no Now. The empty-chain early return still runs before the nil-Attributor read, and the new test covers it. One *Memo is still shared with the bridged tunnel (tl.conn). No tests were dropped, and there are no import cycles (attribution -> tlsbridge is a leaf). The Sink switch is safe: TakesCaptures was the only nil-receiver-tolerant method, and both of its call sites now sit behind a Sessions != nil guard. In websocket.go that guard is the enclosing if. The one behaviour change, stripping X-Cortex-Client-Proc, is documented and tested. Encode and Decode have no caller, so nothing client-controlled reaches Decode in this PR.

Verified locally at 19b7bb6: go build ./... + go vet in core; go test ./attribution/... ./listener/... ./session/... all pass; -race on ./attribution and ./listener/internal/sessionevent; gofmt -l clean. cmd/cortex (build+vet), cmd/cortex-envoy and cmd/cortex-praxis build.

Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (core/attribution, core/listener/{forwardproxy,reverseproxy,extproc,internal/sessionevent})
Agent/IDE config (.claude/.vscode): none
Commits: 9 commits, all signed-off: yes
CI status: passing (39 pass, 2 skipped)

Comment thread core/attribution/wire.go Outdated
Header: h.Get(HeaderNamedBy),
ChainSession: h.Get(HeaderChain),
}
switch a.Source {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion (non-blocking, since nothing calls Decode yet): the checks here are per field, not per shape. Two kinds of input still decode with ok=true:

  • X-Cortex-Source: header with no X-Cortex-Session, or X-Cortex-Session-Header with no session. Resolve never returns either shape.
  • An X-Cortex-Agent of any length (I probed 100 kB) or any non-canonical name. Resolve only ever sets AgentOf's canonical names.

When the trusted hop in #1491 W9 starts calling this, consider tying Source to the fields it implies (header requires Session and Header; process/affinity require Session) and bounding Agent. That way "accepts only what Resolve returns" also holds for the combination of fields.

Fixes review: The Sink interface had one implementation and no seam to
give the listeners another one, because Sessions stays *session.Store.
Files:
- core/listener/extproc/server.go
- core/listener/forwardproxy/identity.go
- core/listener/forwardproxy/record.go
- core/listener/forwardproxy/server.go
- core/listener/forwardproxy/transparent.go
- core/listener/forwardproxy/tunnel.go
- core/listener/forwardproxy/upgrade.go
- core/listener/forwardproxy/websocket.go
- core/listener/internal/sessionevent/sink.go
- core/listener/internal/sessionevent/sink_test.go
- core/listener/reverseproxy/server.go

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Fixes review: Encode and Decode came back with no caller. Decode is a
trust boundary, and its rules must come from the hop that calls it
(rossoctl#1491 W9). X-Cortex-Client-Proc stays in the strip list.
Files:
- core/attribution/wire.go
- core/attribution/wire_test.go
- core/listener/forwardproxy/process_test.go

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
rossoctl#1699 moved the data types to core/model. Bring the branch up to date with main.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

Since my review of 19b7bb6, the two new commits remove the code that had no caller, and nothing else changes. This settles my earlier Decode suggestion: Encode, Decode and validProc are gone, and the PR body now records the rules W9 must set (tie Source to the fields it implies, put a limit on the Agent length). The Sink/SinkOf removal is a mechanical swap back to s.Sessions.X. I checked every call site. Each one is either behind the same s.Sessions != nil guard as before, or it is TakesCaptures, which accepts a nil *Store (server.go:574 now matches main exactly). Against main, the net diff is now just conn.go/conn_test.go, the process.go move, tunnel.go's *attribution.Memo, and the X-Cortex-Client-Proc strip. The wire.go comment change from "every listener" to "the forward proxy" is also more accurate: only forwardproxy (server.go:447, tunnel.go:225) calls Strip. No references to the removed symbols are left anywhere in the repo.

Verified locally at a366e0d: go build ./..., go vet (attribution, listener) and gofmt -l are clean in core. go test passes on ./attribution/..., ./listener/{forwardproxy,extproc,reverseproxy,internal/...} and ./session/...; one session/archive run flaked and passed on rerun, and this PR does not touch that package. -race passes on ./attribution and ./listener/internal/sessionevent. A merge with current main (d41332a, after #1699) builds and vets, and cmd/cortex, cmd/cortex-envoy and cmd/cortex-praxis build.

Author: @huang195 (MEMBER — maintainer)
Areas reviewed: Go (core/attribution, core/listener/{forwardproxy,reverseproxy,extproc,internal/sessionevent})
Agent/IDE config (.claude/.vscode): none
Commits: 11 commits, all signed-off: yes
CI status: passing (38 pass, 1 skipped, CodeRabbit neutral)

@huang195
huang195 merged commit a1a2b05 into rossoctl:main Nov 7, 2026
40 checks passed
@huang195
huang195 deleted the refactor/relay-boundary-1498 branch November 7, 2026 13:20
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 7, 2026

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

Re-reviewed at 85839c9. Since my last review at 19b7bb6, 7875fee and a366e0d dropped the Sink switch and Encode/Decode. That leaves the memo move plus the X-Cortex-Client-Proc strip. My earlier suggestion on Decode's per-field checks no longer applies here, since Decode is gone. It now belongs to #1491 W9, as the description says.

I checked the moved code against base, and it behaves the same:

  • Chain: an invalid key still looks nothing up and logs nothing. A failed lookup is kept, and it logs once at DEBUG, because only the call that runs the once gets the error. memoChain ignores ConnKeyOf's ok safely, because a failed parse leaves the key's ends invalid, and Chain checks that.
  • Program: matches the old bridgeProgram body exactly.
  • SelfTraffic: the nil-memo check now runs before the empty-chain check. Both return false, so the order doesn't matter. When the chain is empty, s.attributor() can be nil, and SelfTraffic returns before it reads it, which the new test covers. When the chain is non-empty, processesOn() has already guaranteed Sessions and Processes.
  • agentsOwnService: affinityClient was already AgentOf.
  • listenerBehind: a.now() falls back to time.Now(), because the forward proxy sets no Now. maxHints is the same constant, now from lookup.go.
  • Bridged tunnel: the CONNECT and the requests it decrypts still share one *Memo (tunnel.go:374 -> bridging.go:275).

No stray references to connProc, lookupChain or listenerBehind are left in Go code, only in older plan docs. The wire.go comment now says "the forward proxy" instead of "every listener". That matches the code: only forwardproxy (server.go:447, tunnel.go:225) calls Strip.

The behaviour change does what the description claims. With HeaderClientProc taken out of wireHeaders, TestForwardProxy_StripsCortexAttributionHeaders fails with upstream saw X-Cortex-Client-Proc: "2.2". It passes with the header put back.

Verified locally at 85839c9:

  • go build ./... and go vet ./... in core, and gofmt -l is clean.
  • go test ./attribution/... ./listener/... all pass, and -race on ./attribution passes.
  • cmd/cortex, cmd/cortex-envoy and cmd/cortex-praxis build.

Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (core/attribution, core/listener/forwardproxy)
Agent/IDE config (.claude/.vscode): none
Commits: 12 commits (incl. 1 merge from main), all signed-off: yes
CI status: passing (38 pass, 2 skipped)

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants