devbox/internal/hub/blob_traversal_test.go
shoemoney 03469958d3
Some checks are pending
CI / vet-build-test (push) Waiting to run
chore: prep for public release — MIT license + github.com module path 🌍⚖️
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved).
- go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox,
  so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo
  stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are
  unchanged. vet + build + go test -race all green after the rename.
- README: document the go install path.

Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found
ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
2026-06-23 19:19:09 -05:00

63 lines
1.8 KiB
Go

package hub
import (
"crypto/ed25519"
"crypto/rand"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/pkg/proto"
)
// enroll joins a device and returns its bearer token.
func enroll(t *testing.T, base string, db *meta.DB) string {
t.Helper()
if err := db.CreateToken(HashToken("tok"), time.Now().Unix()+3600); err != nil {
t.Fatal(err)
}
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
status, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{
Token: "tok", Name: "dev", Pubkey: pub,
Signature: ed25519.Sign(priv, proto.JoinChallenge("tok", pub)),
}))
if status != http.StatusOK {
t.Fatalf("join status %d: %s", status, body)
}
var jr proto.JoinResponse
if err := json.Unmarshal(body, &jr); err != nil {
t.Fatal(err)
}
return jr.Bearer
}
// An authenticated device must NOT be able to read files outside the blob root
// via a path-traversal blob hash (ServeMux URL-decodes %2f in the {hash} wildcard).
func TestBlobGetRejectsTraversal(t *testing.T) {
base, db := testHub(t)
bearer := enroll(t, base, db)
for _, key := range []string{
"..%2f..%2f..%2f..%2fetc%2fpasswd",
"..%2f..%2fdevbox-hub.db",
"not-a-hash",
"deadbeef", // valid hex but wrong length
} {
status, body := do(t, "GET", base+proto.PathBlob+key, bearer, nil)
if status == http.StatusOK {
t.Errorf("GET blob %q returned 200 (leak): %s", key, body)
}
}
// /v1/have must reject a traversal hash rather than stat an arbitrary path.
status, _ := do(t, "POST", base+proto.PathHave, bearer,
mustJSON(t, proto.HaveRequest{Hashes: []string{"../../../../etc/hosts"}}))
if status != http.StatusBadRequest {
t.Errorf("have with traversal hash = %d, want 400", status)
}
}