Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkgs/mcpx/internal/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package artifacts

import (
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/hex"
Expand Down Expand Up @@ -291,7 +290,7 @@ func (s *Store) blobPath(hash string) string {
// anything that can reach the daemon.
func NewID() string {
b := make([]byte, defaults.ArtifactIDBytes)
_, _ = rand.Read(b)
_, _ = RandSource.Read(b)
return "art-" + hex.EncodeToString(b)
}

Expand Down
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/artifacts/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package artifacts

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source NewID mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: an artifact id is the whole of the artifact's access control until OAuth scopes land, so it has to stay unguessable. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
40 changes: 40 additions & 0 deletions pkgs/mcpx/internal/artifacts/randsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package artifacts

import (
"crypto/rand"
"io"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

func swapRandSource(t *testing.T, s io.Reader) {
t.Helper()
old := RandSource
RandSource = s
t.Cleanup(func() { RandSource = old })
}

// The production default must stay crypto/rand: these ids are
// access control, and a silent swap of the default would make them
// guessable (#2640).
func TestRandSourceDefaultIsCryptoRand(t *testing.T) {
if RandSource != rand.Reader {
t.Fatal("production default RandSource is not crypto/rand")
}
}

// Two runs seeded identically mint identical ids: the #2640
// acceptance test at the package level.
func TestNewIDSeededReproducesIDs(t *testing.T) {
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "artifacts"))
first := NewID()
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "artifacts"))
if second := NewID(); second != first {
t.Fatalf("same seed minted different ids: %q vs %q", first, second)
}
swapRandSource(t, randsrc.Seeded([]byte("other-seed"), "artifacts"))
if third := NewID(); third == first {
t.Fatalf("different seed minted the same id %q", first)
}
}
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/cli/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cli

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source InstanceID mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: the instance id distinguishes this daemon from any other for the self-connection guard, so it has to stay unguessable. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/cli/randsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cli

import (
"crypto/rand"
"testing"
)

// The production default must stay crypto/rand (#2640): the
// instance id distinguishes this daemon from any other for the
// self-connection guard.
func TestRandSourceDefaultIsCryptoRand(t *testing.T) {
if RandSource != rand.Reader {
t.Fatal("production default RandSource is not crypto/rand")
}
}
3 changes: 1 addition & 2 deletions pkgs/mcpx/internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -268,7 +267,7 @@ var processInstance struct {
func (a *App) InstanceID() string {
processInstance.once.Do(func() {
b := make([]byte, 16)
if _, err := rand.Read(b); err == nil {
if _, err := RandSource.Read(b); err == nil {
processInstance.id = hex.EncodeToString(b)
} else {
processInstance.id = fmt.Sprintf("pid-%d-%d", os.Getpid(), time.Now().UnixNano())
Expand Down
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/daemon/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package daemon

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source randomHex mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: these tokens name proxied sessions, so they have to stay unguessable. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
40 changes: 40 additions & 0 deletions pkgs/mcpx/internal/daemon/randsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package daemon

import (
"crypto/rand"
"io"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

func swapRandSource(t *testing.T, s io.Reader) {
t.Helper()
old := RandSource
RandSource = s
t.Cleanup(func() { RandSource = old })
}

// The production default must stay crypto/rand: these ids are
// access control, and a silent swap of the default would make them
// guessable (#2640).
func TestRandSourceDefaultIsCryptoRand(t *testing.T) {
if RandSource != rand.Reader {
t.Fatal("production default RandSource is not crypto/rand")
}
}

// Two runs seeded identically mint identical ids: the #2640
// acceptance test at the package level.
func TestRandomHexSeededReproducesIDs(t *testing.T) {
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "daemon"))
first := randomHex(16)
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "daemon"))
if second := randomHex(16); second != first {
t.Fatalf("same seed minted different ids: %q vs %q", first, second)
}
swapRandSource(t, randsrc.Seeded([]byte("other-seed"), "daemon"))
if third := randomHex(16); third == first {
t.Fatalf("different seed minted the same id %q", first)
}
}
3 changes: 1 addition & 2 deletions pkgs/mcpx/internal/daemon/routes_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package daemon

import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -300,6 +299,6 @@ func extractUserQuery(msgs []chatMessage) string {

func randomHex(n int) string {
b := make([]byte, n)
_, _ = rand.Read(b)
_, _ = RandSource.Read(b)
return hex.EncodeToString(b)
}
3 changes: 1 addition & 2 deletions pkgs/mcpx/internal/elicit/elicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package elicit
import (
"bytes"
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -359,7 +358,7 @@ CREATE TABLE IF NOT EXISTS elicitation_answers (
// NewID mints an identifier.
func NewID() string {
var b [8]byte
_, _ = rand.Read(b[:])
_, _ = RandSource.Read(b[:])
return "elc-" + hex.EncodeToString(b[:])
}

Expand Down
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/elicit/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package elicit

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source NewID mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: an elicitation id names a pending question to its session, so it has to stay unguessable. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
40 changes: 40 additions & 0 deletions pkgs/mcpx/internal/elicit/randsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package elicit

import (
"crypto/rand"
"io"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

func swapRandSource(t *testing.T, s io.Reader) {
t.Helper()
old := RandSource
RandSource = s
t.Cleanup(func() { RandSource = old })
}

// The production default must stay crypto/rand: these ids are
// access control, and a silent swap of the default would make them
// guessable (#2640).
func TestRandSourceDefaultIsCryptoRand(t *testing.T) {
if RandSource != rand.Reader {
t.Fatal("production default RandSource is not crypto/rand")
}
}

// Two runs seeded identically mint identical ids: the #2640
// acceptance test at the package level.
func TestNewIDSeededReproducesIDs(t *testing.T) {
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "elicit"))
first := NewID()
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "elicit"))
if second := NewID(); second != first {
t.Fatalf("same seed minted different ids: %q vs %q", first, second)
}
swapRandSource(t, randsrc.Seeded([]byte("other-seed"), "elicit"))
if third := NewID(); third == first {
t.Fatalf("different seed minted the same id %q", first)
}
}
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/logging/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package logging

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source NewTraceID mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: a trace id is only a correlation handle, but production ids stay unguessable for the same reason. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
51 changes: 51 additions & 0 deletions pkgs/mcpx/internal/logging/randsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package logging

import (
"crypto/rand"
"errors"
"io"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

func swapRandSource(t *testing.T, s io.Reader) {
t.Helper()
old := RandSource
RandSource = s
t.Cleanup(func() { RandSource = old })
}

func TestRandSourceDefaultIsCryptoRand(t *testing.T) {
if RandSource != rand.Reader {
t.Fatal("production default RandSource is not crypto/rand")
}
}

type errReader struct{}

func (errReader) Read([]byte) (int, error) { return 0, errors.New("no randomness") }

// Two runs seeded identically mint identical ids: the #2640
// acceptance test at the package level.
func TestNewTraceIDSeededReproducesIDs(t *testing.T) {
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "logging"))
first := NewTraceID("trc")
swapRandSource(t, randsrc.Seeded([]byte("run-seed"), "logging"))
if second := NewTraceID("trc"); second != first {
t.Fatalf("same seed minted different ids: %q vs %q", first, second)
}
swapRandSource(t, randsrc.Seeded([]byte("other-seed"), "logging"))
if third := NewTraceID("trc"); third == first {
t.Fatalf("different seed minted the same id %q", first)
}
}

// A failing source keeps the documented fallback: logging must not
// stop because randomness did.
func TestNewTraceIDFailingSourceFallsBack(t *testing.T) {
swapRandSource(t, errReader{})
if got := NewTraceID("trc"); got != "trc-0000000000000000" {
t.Fatalf("failing source: got %q, want the zero fallback", got)
}
}
3 changes: 1 addition & 2 deletions pkgs/mcpx/internal/logging/trace.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logging

import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"net"
Expand Down Expand Up @@ -29,7 +28,7 @@ type TraceID string
// NewTraceID mints an identifier with a short readable prefix.
func NewTraceID(prefix string) TraceID {
var b [8]byte
if _, err := rand.Read(b[:]); err != nil {
if _, err := RandSource.Read(b[:]); err != nil {
// Randomness failing is not a reason to stop logging.
return TraceID(prefix + "-0000000000000000")
}
Expand Down
15 changes: 15 additions & 0 deletions pkgs/mcpx/internal/mcpserver/randsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mcpserver

import (
"io"

"github.com/developing-today/code/pkgs/mcpx/internal/randsrc"
)

// RandSource is the source newSessionID, newStateSigner and the requestState nonce mints from (#2640).
//
// The production value is randsrc.Crypto -- crypto/rand -- and must
// stay that: a session id is a bearer handle and the state signer key is secret material, so both have to stay unguessable and unreproducible in production. Tests swap in randsrc.Seeded so a run seed
// reproduces the ids a run minted; restore the old value when the
// test ends.
var RandSource io.Reader = randsrc.Crypto
Loading
Loading