Compare commits

..
Author SHA1 Message Date
sam e801306c15 docs(deliveries): say what each field, lease and refusal does 2026-11-22 22:31:55 +11:00
sam 4707427759 feat: trust configured proxies for webhook intake
- add trusted proxy settings, migration, API, and UI
- derive client addresses from X-Forwarded-For behind trusted hops
- enforce delivery policy for listener batches and record denials
2026-11-22 22:29:36 +11:00
sam faeaa0d7f0 fix: deny orgs without active org policies
- refuse authorization when an org has no stored org-level policies instead of falling through the narrowed chain
- seed tests with shipped org policies and cover the deny-by-default cases
2026-11-22 22:19:07 +11:00
19 changed files with 467 additions and 87 deletions

No files matched your search

+13
View File
@@ -130,6 +130,12 @@ components:
type: string
public_url:
type: string
trusted_proxies:
items:
type: string
type:
- array
- "null"
required:
- public_url
type: object
@@ -1798,11 +1804,18 @@ components:
type: string
public_url:
type: string
trusted_proxies:
items:
type: string
type:
- array
- "null"
required:
- public_url
- oidc_issuer
- oidc_audience
- oidc_client_id
- trusted_proxies
type: object
GovernanceBlackout:
additionalProperties: false
+2 -1
View File
@@ -93,7 +93,8 @@ func (o *operations) Start(ctx context.Context) error {
o.settings.UseAuthorizer(checked)
o.allow.UseAuthorizer(checked)
o.tell.UseAccess(checked)
o.hear.UseDeliveryPolicy(access)
o.hear.UseDeliveryPolicy(governance.RecordingDelivery(access, o.settings, o.log.With("module", "governance")))
o.hear.UseTrustedProxies(o.settings.TrustedProxies)
o.talk.Use(chat.Deps{
People: o.settings, Access: access, Settings: o.settings, Toolbox: o.conns, Judge: o.judge,
Approver: o.allow, Memory: o.remember, Runs: o.engine, Notifier: o.tell,
@@ -0,0 +1,8 @@
-- +goose Up
-- Reverse proxies whose X-Forwarded-For header webhook intake uses as the
-- client address, stored as a JSON array of addresses and CIDR ranges.
ALTER TABLE governance.authentication_settings
ADD COLUMN trusted_proxies jsonb NOT NULL DEFAULT '[]';
-- +goose Down
ALTER TABLE governance.authentication_settings DROP COLUMN trusted_proxies;
+3 -3
View File
@@ -63,8 +63,8 @@ func (c *channelLookups) Get(string) (*connectors.Connector, bool) {
// TestDeliveryIsAuthorizedOnEveryAttempt guards that a queued message is sent
// only while the workload principal may still deliver on its channel. Each
// attempt, including a retry after a backoff or a restart, asks again: a
// revoked grant ends the message as failed, and a chain that can't decide
// leaves it queued for its next attempt instead of dropping it.
// revoked grant marks the message failed, and a chain that can't decide leaves
// it queued for its next attempt instead of dropping it.
func TestDeliveryIsAuthorizedOnEveryAttempt(t *testing.T) {
allowed, revoked, unavailable := governance.Allowed(), governance.DeniedAt(governance.LevelOrg, []string{"org-workload"}), governance.Unavailable()
tests := []struct {
@@ -76,7 +76,7 @@ func TestDeliveryIsAuthorizedOnEveryAttempt(t *testing.T) {
wantOutcomes []string
}{
{name: "an allowed attempt is sent", decisions: []governance.Decision{allowed}, attempts: 1, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running"}},
{name: "a revoked grant ends the message", decisions: []governance.Decision{revoked}, attempts: 1, wantOutcomes: []string{"gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}},
{name: "a revoked grant fails the message", decisions: []governance.Decision{revoked}, attempts: 1, wantOutcomes: []string{"gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}},
{name: "a retry after revocation is refused", decisions: []governance.Decision{allowed, revoked}, attempts: 2, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running", "gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}},
{name: "an undecidable chain leaves the message queued", decisions: []governance.Decision{unavailable}, attempts: 3},
{name: "no policy wired leaves the message queued", unwired: true, attempts: 1},
+23 -19
View File
@@ -1,11 +1,11 @@
// Package deliveries is the transport for what Archie tells people, on each
// channel. It holds no approval state: approvals own the request and its
// answer, and use deliveries to carry them. The composer, the chat box in the
// dashboard, is the first channel: its messages are rows a client reads from a
// cursor, with a live stream that only says when to read. Every accepted
// connector that delivers messages is another: each gets its own row, retried
// until the connector confirms it, rejects it permanently, or the retry limit
// is reached.
// Package deliveries carries what Archie tells people on each channel. It
// holds no approval state: approvals own the request and its answer, and use
// deliveries to carry them. The composer, the chat box in the dashboard, is
// the first channel: its messages are rows a client reads from a cursor, with
// a live stream that only says when to read. Every accepted connector that
// delivers messages is another channel: each gets its own row, retried until
// the connector confirms it, rejects it permanently, or the retry limit is
// reached.
package deliveries
import (
@@ -64,8 +64,8 @@ type Notification struct {
}
// ApprovalRequest is one judged action waiting for a person's answer. It is
// carried like a notification, with the request an answer names and the time
// it runs out.
// carried like a notification, and adds the approval request an answer refers
// to and when the request expires.
type ApprovalRequest struct {
RunID uuid.UUID
RequestID uuid.UUID
@@ -77,7 +77,8 @@ type ApprovalRequest struct {
}
// Message is a composer message as the API shows it. Attempts are the delivery
// attempts of the connector rows that carry the same message, one per channel.
// attempts recorded on the connector rows that carry the same message, one per
// channel.
type Message struct {
Seq int64 `json:"seq"`
ID uuid.UUID `json:"id"`
@@ -108,7 +109,8 @@ type Channels interface {
Get(name string) (*connectors.Connector, bool)
}
// Module delivers one organisation's notifications.
// Module delivers one organisation's notifications, approval requests and
// answers.
type Module struct {
orgID string
store *store.Store
@@ -214,9 +216,10 @@ func (m *Module) send(ctx context.Context, msg store.Message) error {
return nil
}
// SentBy reports whether channel confirmed a message under one of ids, the id
// a source's event carries when it echoes what Archie sent, and the run that
// sent it (nil when the message belonged to none).
// SentBy reports whether the channel confirmed a message under any of ids, and
// returns the run that sent it. An id is the identifier a source's event
// carries when it echoes back what Archie sent. The run is empty when no
// message matched.
func (m *Module) SentBy(ctx context.Context, channel string, ids ...string) (uuid.UUID, bool, error) {
ids = slices.DeleteFunc(slices.Clone(ids), func(id string) bool { return id == "" })
if len(ids) == 0 {
@@ -249,9 +252,9 @@ func (m *Module) Work(ctx context.Context) {
// deliver sends a claimed message when the workload principal may still deliver
// on its channel. Each attempt is decided afresh, so a retry after a backoff or
// a restart honours a revocation made meanwhile. A refusal ends the message as
// failed. When the policy chain can't decide, the message stays queued for its
// next attempt.
// a restart honours a revocation made in the meantime. A refusal marks the
// message failed. When the policy chain can't decide, the message stays queued
// for its next attempt.
func (m *Module) deliver(ctx context.Context, msg store.Message) {
err := governance.RequireWorkload(ctx, m.access, governance.ActionDeliver, governance.Resource{
Kind: governance.KindDelivery, ID: msg.Channel,
@@ -313,7 +316,8 @@ func (m *Module) sendTo(ctx context.Context, msg store.Message) (string, error)
return c.Deliver(ctx, connectors.DeliveryMessage, raw, msg.Key)
}
// backoff is how long to wait after the attempts so far before the next.
// backoff returns the wait before the next attempt, given the number of
// attempts made so far.
func backoff(attempts int) time.Duration {
if attempts >= 10 {
return maxBackoff
+9 -9
View File
@@ -29,8 +29,8 @@ type Message struct {
RequestID uuid.NullUUID `db:"request_id"`
ExpiresAt sql.NullTime `db:"expires_at"`
CreatedAt time.Time `db:"created_at"`
// Sent is set on a message its channel has confirmed; the composer's are
// sent when they are stored.
// Sent is set on a message its channel has confirmed. Composer messages
// count as sent as soon as they are stored.
Sent bool `db:"-"`
Attempts int `db:"attempts"`
}
@@ -93,9 +93,9 @@ func (s *Store) Latest(ctx context.Context, orgID, channel string, limit int) ([
return out, nil
}
// Claim takes the oldest message a channel hasn't confirmed whose next attempt
// is due, and pushes that attempt out by backoff first: the push is the lease,
// so a crash mid-delivery retries after it rather than never.
// Claim takes the oldest message its channel hasn't confirmed whose next
// attempt is due, and first pushes that attempt out by the backoff. The push
// is the lease, so a crash mid-delivery retries after it rather than never.
func (s *Store) Claim(ctx context.Context, backoff func(attempts int) time.Duration) (Message, bool, error) {
tx, err := s.db.BeginTxx(ctx, nil)
if err != nil {
@@ -148,8 +148,8 @@ func (s *Store) Sent(ctx context.Context, seq int64, attempt int, externalID str
`UPDATE deliveries.messages SET sent_at = now(), external_id = $2, last_error = NULL WHERE seq = $1`, externalID)
}
// SentBy finds a message its channel confirmed under one of ids. The run is
// the one that sent it, nil when the message belonged to none.
// SentBy finds a message its channel confirmed under any of ids, and returns
// the run that sent it. The run is invalid when the message belonged to none.
func (s *Store) SentBy(ctx context.Context, orgID, channel string, ids []string) (uuid.NullUUID, bool, error) {
var run uuid.NullUUID
err := s.db.GetContext(ctx, &run, `
@@ -164,8 +164,8 @@ func (s *Store) SentBy(ctx context.Context, orgID, channel string, ids []string)
return run, true, nil
}
// Failed records why an attempt failed, with the attempt that failed; the
// lease already set when it is tried again.
// Failed records why an attempt failed, along with the attempt that failed.
// The lease that Claim set still decides when the next attempt is due.
func (s *Store) Failed(ctx context.Context, seq int64, attempt int, reason string, took time.Duration) error {
return s.attempt(ctx, seq, attempt, attemptOutcome{failure: reason, took: took},
`UPDATE deliveries.messages SET last_error = $2 WHERE seq = $1`, reason)
+74
View File
@@ -0,0 +1,74 @@
package events
import (
"context"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// TestIntakeEvaluatesThePolicyAgainstTheClientBehindTrustedProxies guards the
// address passed to the delivery policy: X-Forwarded-For is used only when the
// peer is a configured proxy, and is read from right to left past trusted
// hops, so a client can neither hide behind the proxy's address nor spoof one.
func TestIntakeEvaluatesThePolicyAgainstTheClientBehindTrustedProxies(t *testing.T) {
proxies := []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8"), netip.MustParsePrefix("192.0.2.1/32")}
tests := []struct {
name string
peer string
headers []string
trusted []netip.Prefix
readErr error
want string
}{
{name: "no proxies configured uses the peer", peer: "203.0.113.9:4711", want: "203.0.113.9"},
{name: "a direct client can't forge a forwarded address", peer: "203.0.113.9:4711", headers: []string{"198.51.100.7"}, trusted: proxies, want: "203.0.113.9"},
{name: "a trusted proxy reports the client", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a forged entry left of the real client is ignored", peer: "10.0.0.5:4711", headers: []string{"1.2.3.4, 198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a chain of trusted proxies is skipped", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7, 10.0.0.9, 192.0.2.1"}, trusted: proxies, want: "198.51.100.7"},
{name: "multiple header lines are joined into one list", peer: "10.0.0.5:4711", headers: []string{"1.2.3.4", "198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a trusted proxy with no header uses the peer", peer: "10.0.0.5:4711", trusted: proxies, want: "10.0.0.5"},
{name: "an unreadable entry uses the peer", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7, unknown"}, trusted: proxies, want: "10.0.0.5"},
{name: "an unreadable proxy list uses the peer", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7"}, readErr: errors.New("store down"), want: "10.0.0.5"},
{name: "an IPv6 client address has no brackets", peer: "10.0.0.5:4711", headers: []string{"2001:db8::7"}, trusted: proxies, want: "2001:db8::7"},
}
const token = "not-a-real-token" //nolint:gosec // fixture bearer token the verifier must accept
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := &deliveryPolicy{decision: governance.Allowed()}
m := &Module{
orgID: "default", log: slog.New(slog.DiscardHandler), inserter: &onceInserter{stored: map[string]bool{}}, deliveries: noEcho{},
reader: fakeSources{store.Source{OrgID: "default", Name: "hook", Connector: "c", Verification: store.Token, TokenSHA256: httpjson.TokenHash(token)}},
parse: func(context.Context, string, []byte, map[string]string) ([]connectors.Event, error) {
return []connectors.Event{{Type: "alert", SourceEventID: "evt-1"}}, nil
},
}
m.UseDeliveryPolicy(policy)
m.UseTrustedProxies(func(context.Context) ([]netip.Prefix, error) { return tt.trusted, tt.readErr })
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/hooks/hook", strings.NewReader(`{}`))
req.SetPathValue("source", "hook")
req.RemoteAddr = tt.peer
req.Header.Set("Authorization", "Bearer "+token)
for _, h := range tt.headers {
req.Header.Add("X-Forwarded-For", h)
}
rec := httptest.NewRecorder()
m.intake(rec, req)
if rec.Code != http.StatusAccepted {
t.Fatalf("status %d, want %d", rec.Code, http.StatusAccepted)
}
if want := "org-sys hook " + tt.want; len(policy.asked) != 1 || policy.asked[0] != want {
t.Fatalf("delivery policy asked %v, want %q", policy.asked, want)
}
})
}
}
+121 -28
View File
@@ -18,7 +18,9 @@ import (
"log/slog"
"net"
"net/http"
"net/netip"
"regexp"
"slices"
"strings"
"sync"
"time"
@@ -94,6 +96,7 @@ type Module struct {
inserter eventInserter
types typeReader
delivery governance.DeliveryAuthorizer
proxies func(context.Context) ([]netip.Prefix, error)
sealer Sealer
parse func(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error)
connectors Connectors
@@ -144,7 +147,7 @@ func (m *Module) intake(w http.ResponseWriter, r *http.Request) {
httpjson.Error(w, status, err.Error())
return
}
stored, duplicates, err := m.save(r.Context(), d.src, d.events, d.body, d.arrival)
stored, duplicates, err := m.save(r.Context(), d.admission, d.events, d.body, d.arrival)
if err != nil {
httpjson.Error(w, http.StatusServiceUnavailable, err.Error())
return
@@ -154,10 +157,12 @@ func (m *Module) intake(w http.ResponseWriter, r *http.Request) {
// delivery is a webhook delivery that passed verification and was parsed.
type delivery struct {
src store.Source
events []connectors.Event
body []byte
arrival arrival
src store.Source
// admission is set after the source's delivery policy allows the sender.
admission admission
events []connectors.Event
body []byte
arrival arrival
}
// accept verifies a delivery against its source's scheme, then passes it to
@@ -203,7 +208,9 @@ func (m *Module) accept(r *http.Request) (d delivery, status int, err error) {
return m.refuse(ctx, d, err)
}
}
if status, err := m.deliveryAllowed(ctx, src, r); err != nil {
addr := m.clientAddr(ctx, r)
d.admission, status, err = m.admit(ctx, src, addr)
if err != nil {
return d, status, err
}
d.events, err = m.parse(ctx, src.Connector, d.body, map[string]string{"Content-Type": r.Header.Get("Content-Type")})
@@ -213,7 +220,7 @@ func (m *Module) accept(r *http.Request) (d delivery, status int, err error) {
if err != nil {
return d, http.StatusBadRequest, err
}
d.arrival.addr = senderAddr(r)
d.arrival.addr = addr
d.arrival.headers = r.Header
return d, 0, nil
}
@@ -238,35 +245,97 @@ var (
errDeliveryUnavailable = errors.New("delivery policy unavailable")
)
// deliveryAllowed applies the source's delivery policy to the sender's address.
// A verified delivery still needs it. When the policy chain can't decide the
// delivery is refused with 503, so the sender retries and nothing is stored.
func (m *Module) deliveryAllowed(ctx context.Context, src store.Source, r *http.Request) (int, error) {
// admission records that the source's delivery policy allowed a sender. Only
// admit creates one and save requires one, so no intake path can store events
// without evaluating the policy.
type admission struct {
src store.Source
}
// admit evaluates the source's delivery policy against the sender's address.
// The address is empty for a source without a remote sender, such as a
// connector's listener stream. Verified deliveries are evaluated too. If the
// policy chain returns no decision, admit returns 503 so that a webhook sender
// retries, and nothing is stored.
func (m *Module) admit(ctx context.Context, src store.Source, addr string) (admission, int, error) {
if m.delivery == nil {
return http.StatusServiceUnavailable, errDeliveryUnavailable
return admission{}, http.StatusServiceUnavailable, errDeliveryUnavailable
}
addr := senderAddr(r)
// Delivery policies belong to the operating organisation, whatever the
// source's own org id says.
d := m.delivery.AuthorizeDelivery(ctx, governance.DefaultOrgID, src.Name, addr)
switch {
case d.Allowed:
return 0, nil
return admission{src: src}, 0, nil
case errors.Is(d.Err, governance.ErrChainUnavailable):
m.log.WarnContext(ctx, "delivery policy unavailable", "source", src.Name)
return http.StatusServiceUnavailable, errDeliveryUnavailable
return admission{}, http.StatusServiceUnavailable, errDeliveryUnavailable
}
m.log.WarnContext(ctx, "refused webhook delivery", "source", src.Name, "reason", "delivery policy", "level", d.Level, "policies", d.Policies)
return http.StatusForbidden, errDeliveryDenied
m.log.WarnContext(ctx, "refused delivery", "source", src.Name, "reason", "delivery policy", "level", d.Level, "policies", d.Policies)
return admission{}, http.StatusForbidden, errDeliveryDenied
}
// senderAddr is the address a request came from, without its port.
func senderAddr(r *http.Request) string {
addr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
// clientAddr returns the request's client address: the connection's peer
// address, or the forwarded client address when the peer is a trusted proxy.
func (m *Module) clientAddr(ctx context.Context, r *http.Request) string {
peer := r.RemoteAddr
if host, _, err := net.SplitHostPort(peer); err == nil {
peer = host
}
return addr
return forwardedClient(peer, r.Header.Values("X-Forwarded-For"), m.trustedProxies(ctx))
}
// forwardedClient returns the client address from X-Forwarded-For. The header
// lists the client first, followed by each proxy that relayed the request.
// Only entries appended by trusted proxies are reliable, because a client can
// set any value at the left end, so the list is read from right to left and
// the first address that is not a trusted proxy is returned. The peer address
// is returned when the peer is not trusted or the header can't be parsed.
func forwardedClient(peer string, forwarded []string, trusted []netip.Prefix) string {
if len(trusted) == 0 || !isTrusted(peer, trusted) {
return peer
}
hops := strings.Split(strings.Join(forwarded, ","), ",")
for i, hop := range slices.Backward(hops) {
hop := strings.TrimSpace(hop)
addr, err := netip.ParseAddr(hop)
if err != nil {
return peer
}
if i == 0 || !isTrusted(hop, trusted) {
return addr.Unmap().String()
}
}
return peer
}
// isTrusted reports whether addr is inside any of the trusted prefixes.
func isTrusted(addr string, trusted []netip.Prefix) bool {
a, err := netip.ParseAddr(addr)
if err != nil {
return false
}
a = a.Unmap()
return slices.ContainsFunc(trusted, func(p netip.Prefix) bool { return p.Contains(a) })
}
// trustedProxies returns the configured trusted proxy prefixes.
func (m *Module) trustedProxies(ctx context.Context) []netip.Prefix {
if m.proxies == nil {
return nil
}
prefixes, err := m.proxies(ctx)
if err != nil {
m.log.WarnContext(ctx, "read trusted proxies", "error", err)
return nil
}
return prefixes
}
// UseTrustedProxies sets the function that reads the trusted reverse proxies.
// If it is not set, X-Forwarded-For is ignored.
func (m *Module) UseTrustedProxies(read func(context.Context) ([]netip.Prefix, error)) {
m.proxies = read
}
// parseWith has a running connector parse a delivery.
@@ -280,7 +349,8 @@ func (m *Module) parseWith(ctx context.Context, connector string, body []byte, h
// save stores each event with the payload it arrived in, recording at arrival
// whether it echoes a delivery Archie sent, so those never reach grouping.
func (m *Module) save(ctx context.Context, src store.Source, parsed []connectors.Event, body []byte, a arrival) (stored, duplicates int, err error) {
func (m *Module) save(ctx context.Context, adm admission, parsed []connectors.Event, body []byte, a arrival) (stored, duplicates int, err error) {
src := adm.src
for i, e := range parsed {
run, own, err := m.deliveries.SentBy(ctx, src.Connector, e.SourceEventID, e.LinkKey)
if err != nil {
@@ -365,10 +435,7 @@ func (m *Module) listen(ctx context.Context, src store.Source) {
err = fmt.Errorf("connector %q isn't running", src.Connector)
} else {
err = conn.Listen(ctx, cursor, func(events []connectors.Event, next string) error {
if _, _, err := m.save(ctx, src, events, received(events), arrival{}); err != nil {
return err
}
if err := m.store.SetCursor(ctx, m.orgID, src.Name, next); err != nil {
if err := m.listenBatch(ctx, src, events, next); err != nil {
return err
}
cursor = next
@@ -386,6 +453,32 @@ func (m *Module) listen(ctx context.Context, src store.Source) {
}
}
// listenBatch stores one streamed batch, then moves the source's cursor past it.
func (m *Module) listenBatch(ctx context.Context, src store.Source, events []connectors.Event, next string) error {
if err := m.storeListened(ctx, src, events); err != nil {
return err
}
return m.store.SetCursor(ctx, m.orgID, src.Name, next)
}
// storeListened stores a batch streamed by a connector listener if the source's
// delivery policy allows it. A listener has no remote sender, so the policy is
// evaluated with an empty address: rules on the source apply, and a rule that
// allows only specific addresses denies every listener. A denied batch is
// dropped without an error, because evaluating it again would return the same
// decision.
func (m *Module) storeListened(ctx context.Context, src store.Source, events []connectors.Event) error {
adm, _, err := m.admit(ctx, src, "")
if errors.Is(err, errDeliveryDenied) {
return nil
}
if err != nil {
return err
}
_, _, err = m.save(ctx, adm, events, received(events), arrival{})
return err
}
// answer settles the approval request an answer event names. An answer that
// can't settle it is logged and done with: the request's own record already
// says why. A channel account no account has linked is told how to link it.
+48
View File
@@ -0,0 +1,48 @@
package events
import (
"log/slog"
"testing"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// TestListenedEventsPassTheDeliveryPolicyBeforeStoring guards connector
// listener intake: a streamed batch is evaluated against its source's delivery
// policy, as a webhook delivery is. A denial stores nothing and does not fail
// the stream, so the cursor advances. An unavailable decision fails the stream
// so that the batch is fetched again.
func TestListenedEventsPassTheDeliveryPolicyBeforeStoring(t *testing.T) {
tests := []struct {
name string
policy governance.Decision
unwired bool
wantErr bool
wantStored int
}{
{name: "an allowed batch is stored", policy: governance.Allowed(), wantStored: 1},
{name: "a denied batch is dropped without failing the stream", policy: governance.DeniedAt(governance.LevelObject, []string{"tg-deny"})},
{name: "an unavailable decision fails the stream so the batch is fetched again", policy: governance.Unavailable(), wantErr: true},
{name: "no policy wired fails the stream", unwired: true, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := &deliveryPolicy{decision: tt.policy}
inserter := &onceInserter{stored: map[string]bool{}}
m := &Module{orgID: "default", log: slog.New(slog.DiscardHandler), inserter: inserter, deliveries: noEcho{}}
if !tt.unwired {
m.UseDeliveryPolicy(policy)
}
src := store.Source{OrgID: "default", Name: "tg", Connector: "telegram"}
err := m.storeListened(t.Context(), src, []connectors.Event{{Type: "message", SourceEventID: "evt-1"}})
if (err != nil) != tt.wantErr {
t.Fatalf("error = %v, want error %v", err, tt.wantErr)
}
if len(inserter.stored) != tt.wantStored {
t.Fatalf("events stored = %d, want %d", len(inserter.stored), tt.wantStored)
}
})
}
}
+3 -3
View File
@@ -500,9 +500,9 @@ func TestSeedShippedPolicies(t *testing.T) {
}
}
// TestAuthorizeWithoutOrgPolicies guards the deny-by-default rule: an org
// with no stored org policies, or only narrowing ones, must refuse every
// request instead of falling through to allow.
// TestAuthorizeWithoutOrgPolicies guards deny by default: an org with no
// org-level policies, even with instance or workspace policies present, must
// deny every request instead of falling through to allow.
func TestAuthorizeWithoutOrgPolicies(t *testing.T) {
const org = OrgID("acme")
owner := Principal{IdentityID: "o", Org: org, Memberships: []Membership{{IdentityID: "o", OrgID: org, Role: RoleOwner}}}
+6 -5
View File
@@ -333,10 +333,11 @@ func (m *Module) getAuthentication(ctx context.Context, _ *struct{}) (*httpapi.R
type authenticationInput struct {
Session string `cookie:"archie_session"`
Body struct {
PublicURL string `json:"public_url"`
OIDCIssuer string `json:"oidc_issuer,omitempty"`
OIDCAudience string `json:"oidc_audience,omitempty"`
OIDCClientID string `json:"oidc_client_id,omitempty"`
PublicURL string `json:"public_url"`
OIDCIssuer string `json:"oidc_issuer,omitempty"`
OIDCAudience string `json:"oidc_audience,omitempty"`
OIDCClientID string `json:"oidc_client_id,omitempty"`
TrustedProxies []string `json:"trusted_proxies,omitempty"`
}
}
@@ -346,7 +347,7 @@ func (m *Module) putAuthentication(ctx context.Context, in *authenticationInput)
}
err := m.SaveAuthenticationSettings(ctx, AuthenticationSettings(in.Body))
if errors.Is(err, ErrInvalidIdentity) {
return nil, huma.Error400BadRequest("Use a valid instance origin and complete OIDC settings. Remove enrolled passkeys before changing the instance URL.")
return nil, huma.Error400BadRequest("Use a valid instance origin, trusted proxies that are addresses or CIDR ranges, and complete OIDC settings. Remove enrolled passkeys before changing the instance URL.")
}
if err != nil {
return nil, m.accountError(ctx, err)
+28
View File
@@ -111,3 +111,31 @@ func (m *Module) mayUse(ctx context.Context, p Principal, action Action, kind Re
}
return chain.Authorize(ctx, p, action, Resource{Kind: kind, Org: p.Org}, AccessContext{Time: time.Now()}).Allowed
}
type recordingDelivery struct {
DeliveryAuthorizer
denials DenialRecorder
log *slog.Logger
}
// RecordingDelivery wraps a so that every denied delivery is stored as a
// denial, for webhook and listener intake alike. An unavailable decision is
// not a denial and is not stored.
func RecordingDelivery(a DeliveryAuthorizer, denials DenialRecorder, log *slog.Logger) DeliveryAuthorizer {
return recordingDelivery{DeliveryAuthorizer: a, denials: denials, log: log}
}
func (r recordingDelivery) AuthorizeDelivery(ctx context.Context, orgID OrgID, sourcePath, addr string) Decision {
d := r.DeliveryAuthorizer.AuthorizeDelivery(ctx, orgID, sourcePath, addr)
if d.Allowed || errors.Is(d.Err, ErrChainUnavailable) {
return d
}
err := r.denials.RecordDenial(context.WithoutCancel(ctx), Denial{
Principal: SenderID, Org: orgID, Action: ActionDeliver, Kind: KindSource, ResourceID: sourcePath,
Level: d.Level, Policies: d.Policies, At: time.Now(), Count: 1,
})
if err != nil {
r.log.ErrorContext(ctx, "record delivery denial", "source", sourcePath, "error", err)
}
return d
}
+3 -3
View File
@@ -180,9 +180,9 @@ func (e *Engine) Authorize(_ context.Context, p Principal, a Action, r Resource,
if p.Org == "" || resOrg != p.Org {
return DeniedAt(LevelInstance, []string{CrossOrgForbidID})
}
// Lower levels only narrow, so an org with no policies of its own has
// nothing that grants access. Evaluating the chain alone would skip every
// empty level and allow the request.
// Only org-level policies grant access within an org. evaluate skips empty
// levels, so without this check an org with no org-level policies would be
// allowed.
if !e.orgs[resOrg].active() {
return DeniedAt(LevelOrg, nil)
}
+10 -2
View File
@@ -433,19 +433,27 @@ type AccountSecurity interface {
RecoverAccount(ctx context.Context, email, code, password string) error
}
// AuthenticationSettings stores instance public origin and OIDC parameters.
// AuthenticationSettings stores the instance's public origin, its trusted
// reverse proxies and the OIDC parameters.
type AuthenticationSettings struct {
PublicURL string `json:"public_url"`
OIDCIssuer string `json:"oidc_issuer"`
OIDCAudience string `json:"oidc_audience"`
OIDCClientID string `json:"oidc_client_id"`
// TrustedProxies are the addresses and CIDR ranges of reverse proxies whose
// X-Forwarded-For header webhook intake uses as the client address.
TrustedProxies []string `json:"trusted_proxies"`
}
// Validate checks that PublicURL is a valid origin and OIDC fields are paired.
// Validate checks that PublicURL is a valid origin, the trusted proxies are
// addresses or ranges, and OIDC fields are paired.
func (s AuthenticationSettings) Validate() error {
if !validInstanceOrigin(s.PublicURL) {
return ErrInvalidIdentity
}
if _, err := normalizeProxies(s.TrustedProxies); err != nil {
return err
}
if s.OIDCIssuer == "" {
if s.OIDCClientID != "" || s.OIDCAudience != "" {
return ErrInvalidIdentity
@@ -29,6 +29,8 @@ type AuthSettingsRow struct {
OIDCIssuer string `db:"oidc_issuer"`
OIDCAudience string `db:"oidc_audience"`
OIDCClientID string `db:"oidc_client_id"`
// TrustedProxies is a JSON array of addresses and CIDR ranges.
TrustedProxies []byte `db:"trusted_proxies"`
}
// RegisterAccountParams carries the validated inputs to create a user account.
@@ -342,7 +344,7 @@ func (s *Store) RecoverAccount(ctx context.Context, email, recoveryHash, newPass
func (s *Store) AuthenticationSettings(ctx context.Context) (AuthSettingsRow, error) {
var out AuthSettingsRow
err := s.db.GetContext(ctx, &out, `
SELECT public_url, oidc_issuer, oidc_audience, oidc_client_id
SELECT public_url, oidc_issuer, oidc_audience, oidc_client_id, trusted_proxies
FROM governance.authentication_settings WHERE singleton`)
if errors.Is(err, sql.ErrNoRows) {
return AuthSettingsRow{}, nil
@@ -357,9 +359,9 @@ func (s *Store) AuthenticationSettings(ctx context.Context) (AuthSettingsRow, er
func (s *Store) SaveAuthenticationSettings(ctx context.Context, row AuthSettingsRow) (updated bool, err error) {
res, err := s.db.ExecContext(ctx, `
UPDATE governance.authentication_settings
SET public_url = $1, oidc_issuer = $2, oidc_audience = $3, oidc_client_id = $4
SET public_url = $1, oidc_issuer = $2, oidc_audience = $3, oidc_client_id = $4, trusted_proxies = $5
WHERE singleton AND (public_url = $1 OR NOT EXISTS (SELECT 1 FROM governance.passkeys))`,
row.PublicURL, row.OIDCIssuer, row.OIDCAudience, row.OIDCClientID)
row.PublicURL, row.OIDCIssuer, row.OIDCAudience, row.OIDCClientID, string(row.TrustedProxies))
if err != nil {
return false, fmt.Errorf("save authentication settings: %w", err)
}
+1 -1
View File
@@ -35,7 +35,7 @@ func (o *oidcLogin) flow(ctx context.Context, settings AuthenticationSettingsSto
}
o.mu.Lock()
defer o.mu.Unlock()
if o.provider == nil || o.current != value {
if o.provider == nil || o.current.OIDCIssuer != value.OIDCIssuer || o.current.OIDCAudience != value.OIDCAudience {
provider, err := newOIDCProvider(value.OIDCIssuer, value.OIDCAudience)
if err != nil {
return nil, err
+81
View File
@@ -0,0 +1,81 @@
package governance
import (
"context"
json "encoding/json/v2"
"fmt"
"net/netip"
"strings"
)
// maxTrustedProxies limits the size of the proxy list, which is read on every
// webhook delivery.
const maxTrustedProxies = 64
// normalizeProxies trims each entry and checks that it is an address or a CIDR
// range. A bare address means that one host.
func normalizeProxies(entries []string) ([]string, error) {
if len(entries) > maxTrustedProxies {
return nil, ErrInvalidIdentity
}
out := make([]string, 0, len(entries))
for _, entry := range entries {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
prefix, err := parseProxy(entry)
if err != nil {
return nil, ErrInvalidIdentity
}
out = append(out, prefix.String())
}
return out, nil
}
func parseProxy(entry string) (netip.Prefix, error) {
if strings.Contains(entry, "/") {
return netip.ParsePrefix(entry)
}
addr, err := netip.ParseAddr(entry)
if err != nil {
return netip.Prefix{}, err
}
return netip.PrefixFrom(addr, addr.BitLen()), nil
}
// TrustedProxies returns the reverse proxies whose X-Forwarded-For header
// webhook intake uses as the client address.
func (m *Module) TrustedProxies(ctx context.Context) ([]netip.Prefix, error) {
settings, err := m.AuthenticationSettings(ctx)
if err != nil {
return nil, err
}
out := make([]netip.Prefix, 0, len(settings.TrustedProxies))
for _, entry := range settings.TrustedProxies {
prefix, err := parseProxy(entry)
if err != nil {
return nil, fmt.Errorf("stored trusted proxy %q: %w", entry, err)
}
out = append(out, prefix)
}
return out, nil
}
func encodeProxies(entries []string) ([]byte, error) {
if entries == nil {
entries = []string{}
}
return json.Marshal(entries)
}
func decodeProxies(raw []byte) ([]string, error) {
entries := []string{}
if len(raw) == 0 {
return entries, nil
}
if err := json.Unmarshal(raw, &entries); err != nil {
return nil, fmt.Errorf("read trusted proxies: %w", err)
}
return entries, nil
}
+22 -8
View File
@@ -534,11 +534,16 @@ func (m *Module) AuthenticationSettings(ctx context.Context) (AuthenticationSett
if err != nil {
return AuthenticationSettings{}, err
}
proxies, err := decodeProxies(row.TrustedProxies)
if err != nil {
return AuthenticationSettings{}, err
}
return AuthenticationSettings{
PublicURL: row.PublicURL,
OIDCIssuer: row.OIDCIssuer,
OIDCAudience: row.OIDCAudience,
OIDCClientID: row.OIDCClientID,
PublicURL: row.PublicURL,
TrustedProxies: proxies,
OIDCIssuer: row.OIDCIssuer,
OIDCAudience: row.OIDCAudience,
OIDCClientID: row.OIDCClientID,
}, nil
}
@@ -552,11 +557,20 @@ func (m *Module) SaveAuthenticationSettings(ctx context.Context, value Authentic
if err := value.Validate(); err != nil {
return err
}
proxies, err := normalizeProxies(value.TrustedProxies)
if err != nil {
return err
}
encoded, err := encodeProxies(proxies)
if err != nil {
return err
}
updated, err := m.store.SaveAuthenticationSettings(ctx, store.AuthSettingsRow{
PublicURL: value.PublicURL,
OIDCIssuer: value.OIDCIssuer,
OIDCAudience: value.OIDCAudience,
OIDCClientID: value.OIDCClientID,
PublicURL: value.PublicURL,
TrustedProxies: encoded,
OIDCIssuer: value.OIDCIssuer,
OIDCAudience: value.OIDCAudience,
OIDCClientID: value.OIDCClientID,
})
if err != nil {
return err
+7 -2
View File
@@ -7,14 +7,16 @@ import PageHeader from "@/base/PageHeader.vue";
import { Button } from "@/components/ui/button";
import CodeView from "@/base/CodeView.vue";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { api, classifyActionError, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
import { SettingRow } from "@/components/ui/setting-row";
import { Section } from "@/components/ui/section";
interface Settings {public_url:string;oidc_issuer:string;oidc_audience:string;oidc_client_id:string}
const value=ref<Settings>({public_url:"",oidc_issuer:"",oidc_audience:"",oidc_client_id:""});const loaded=ref(false);const error=ref("");const sessionExpired=ref(false);const busy=ref(false);const saved=ref(false);
interface Settings {public_url:string;oidc_issuer:string;oidc_audience:string;oidc_client_id:string;trusted_proxies:string[]}
const value=ref<Settings>({public_url:"",oidc_issuer:"",oidc_audience:"",oidc_client_id:"",trusted_proxies:[]});const loaded=ref(false);const error=ref("");const sessionExpired=ref(false);const busy=ref(false);const saved=ref(false);
async function load(){try{value.value=await api.authentication<Settings>();loaded.value=true;error.value=""}catch(cause){error.value=describeError(cause, errors.common.unknown)}finally{loaded.value=true}}
onMounted(load);
const proxyText=computed({get:()=>value.value.trusted_proxies.join("\n"),set:(text:string|number)=>{value.value.trusted_proxies=String(text).split("\n")}});
const redirectURI=computed(()=>`${value.value.public_url.replace(/\/$/,"")}/oauth2/callback`);
async function save(){busy.value=true;error.value="";saved.value=false;try{await api.saveAuthentication(value.value);saved.value=true}catch(cause){error.value=describeError(cause, errors.common.unknown);sessionExpired.value=classifyActionError(cause).kind==="session-expired"}finally{busy.value=false}}
</script>
@@ -26,6 +28,9 @@ async function save(){busy.value=true;error.value="";saved.value=false;try{await
<Section title="Instance URL">
<SettingRow label="Public address" for="instance-url" hint="Passkeys require HTTPS and a domain name, or localhost. Once passkeys are enrolled, their domain is fixed."><Input id="instance-url" v-model="value.public_url" type="url" required /></SettingRow>
</Section>
<Section title="Reverse proxies">
<SettingRow label="Trusted proxies" for="trusted-proxies" hint="One address or CIDR range per line. Webhook deliveries from these proxies are checked against the client address in X-Forwarded-For."><Textarea id="trusted-proxies" v-model="proxyText" rows="3" class="max-w-xl font-mono" placeholder="10.0.0.0/8" /></SettingRow>
</Section>
<Section title="OpenID Connect">
<SettingRow label="Issuer URL" for="oidc-issuer"><Input id="oidc-issuer" v-model="value.oidc_issuer" type="url" placeholder="https://identity.example.com" /></SettingRow>
<SettingRow label="Client ID" for="oidc-client"><Input id="oidc-client" v-model="value.oidc_client_id" /></SettingRow>