Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
12 changes: 12 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ tasks:
- task: test:renovate-schedule-groups
- task: test:forge-fake
- task: test:forge-fake-contract
- task: test:issue-tracker-forge-fake-contract
# Hermetic bot-provisioning regressions (issues #2010 and #2016): fake glab,
# no network, covering host selection, JSON, PAT headers and safe diagnostics.
- task: test:create-gitlab-bot
Expand Down Expand Up @@ -1213,6 +1214,17 @@ tasks:
# Anchored run selects only this external-package contract, not the suite.
- env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null node e2e/forge-fake/operation-contracts.test.mjs -- go test -buildvcs=false -count=1 -v -timeout=60s -run='^TestForgeFakeOperationContracts$' ./internal/forge

test:issue-tracker-forge-fake-contract:
desc: shared issue tracker contract against the real GitLab, GitHub and Forgejo drivers over the Node HTTPS fake
preconditions:
- sh: command -v node
msg: the issue tracker forge fake contract requires Node
cmds:
# Same shape as test:forge-fake-contract: the launcher starts the fake with
# FORGE_FAKE_ISSUE_CONTRACT=1 and a self-signed cert, then runs this exact Go
# argv from api. It allows only the moved-issue subtest to skip (the forge subjects supply no MoveIssue seam).
- env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null node e2e/forge-fake/issue-tracker-contracts.test.mjs -- go test -buildvcs=false -count=1 -v -timeout=60s -run='^TestIssueTrackerForgeFakeContracts$' ./internal/issuetracker

test:create-gitlab-bot:
desc: hermetic bot-provisioning contract for create-gitlab-bot.sh (fake glab, no network), tally-guarded
cmds:
Expand Down
31 changes: 31 additions & 0 deletions api/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ type Config struct {
// scheme+host, https only) but a DISTINCT list, so enabling one source never widens the
// other. EMPTY means the feature is off: ProductSkillsBaseURLAllowed is false for every URL.
ProductSkillsAllowedBaseURLs []string
// IssueTrackerAllowedBaseURLs is the SEPARATE SSRF allowlist for external issue tracker
// connections (PRD #4173 D8, ISSUE_TRACKER_ALLOWED_BASE_URLS): the only base URLs a
// connection may target. Same shape as the other allowlists (normalized scheme+host, https
// only) but a DISTINCT list, never FORGE_ALLOWED_BASE_URLS, so a forge host is not an issue
// tracker host. EMPTY by default, which means no connection can be created:
// IssueTrackerBaseURLAllowed is false for every URL.
IssueTrackerAllowedBaseURLs []string
// ForgePollInterval is the per-enabled-repo incremental poll cadence.
ForgePollInterval time.Duration
// ForgeReconcileEvery is the number of incremental polls between full
Expand Down Expand Up @@ -994,6 +1001,12 @@ func Load() (Config, error) {
return Config{}, err
}
cfg.ProductSkillsAllowedBaseURLs = productSkillsAllowed
// PRD #4173 D8: the issue tracker allowlist, default EMPTY (no connection can be created, instance still boots).
issueTrackerAllowed, err := parseBaseURLAllowlist("ISSUE_TRACKER_ALLOWED_BASE_URLS", getenv("ISSUE_TRACKER_ALLOWED_BASE_URLS", ""))
if err != nil {
return Config{}, err
}
cfg.IssueTrackerAllowedBaseURLs = issueTrackerAllowed
cfg.ForgePollInterval = parseDuration("FORGE_POLL_INTERVAL", time.Minute)
cfg.ForgeReconcileEvery = parseInt("FORGE_RECONCILE_EVERY", 10)
cfg.ForgeHTTPTimeout = parseDuration("FORGE_HTTP_TIMEOUT", 15*time.Second)
Expand Down Expand Up @@ -2231,6 +2244,24 @@ func (c Config) AgentSourceBaseURLAllowed(raw string) bool {
return false
}

// IssueTrackerBaseURLAllowed reports whether raw normalizes to an allowlisted issue tracker base
// URL (PRD #4173 D8). It checks the SEPARATE IssueTrackerAllowedBaseURLs list and is false when
// that list is empty. NormalizeForgeBaseURL keeps only scheme and host, so it does NOT reject
// userinfo, a path, a query or a fragment: the issue tracker service applies its own strict check
// first and passes this method the already-normalized value.
func (c Config) IssueTrackerBaseURLAllowed(raw string) bool {
norm, err := NormalizeForgeBaseURL(raw)
if err != nil {
return false
}
for _, a := range c.IssueTrackerAllowedBaseURLs {
if a == norm {
return true
}
}
return false
}

// validateSecret enforces the boot guard: reject missing, empty, placeholder,
// or too-short secrets.
func validateSecret(name, raw string) ([]byte, error) {
Expand Down
76 changes: 76 additions & 0 deletions api/internal/config/issue_tracker_allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"os"
"strings"
"testing"
)

// TestLoadReadsIssueTrackerAllowlist (PRD #4173 D8): the variable reaches Config, defaults to an
// empty list while the instance still boots, and a non-https entry fails the boot naming the variable.
func TestLoadReadsIssueTrackerAllowlist(t *testing.T) {
autoselectEnv(t)
const env = "ISSUE_TRACKER_ALLOWED_BASE_URLS"

t.Setenv(env, "")
cfg, err := Load()
if err != nil {
t.Fatalf("Load with the variable empty: %v", err)
}
if cfg.IssueTrackerAllowedBaseURLs != nil {
t.Errorf("empty variable gave %v, want nil", cfg.IssueTrackerAllowedBaseURLs)
}

// Truly unset, not just empty: t.Setenv registers the restore, then Unsetenv removes it.
t.Setenv(env, "x")
if err := os.Unsetenv(env); err != nil {
t.Fatal(err)
}
cfg, err = Load()
if err != nil {
t.Fatalf("Load with the variable unset: %v", err)
}
if cfg.IssueTrackerAllowedBaseURLs != nil {
t.Errorf("unset variable gave %v, want nil", cfg.IssueTrackerAllowedBaseURLs)
}

t.Setenv(env, "https://Issues.Example.com/, https://issues.example.com")
cfg, err = Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if got := cfg.IssueTrackerAllowedBaseURLs; len(got) != 1 || got[0] != "https://issues.example.com" {
t.Errorf("allowlist = %v, want one normalized entry", got)
}

t.Setenv(env, "http://issues.example.com")
_, err = Load()
if err == nil {
t.Fatal("Load accepted an http entry; want a boot error")
}
if !strings.Contains(err.Error(), env) {
t.Errorf("boot error %q does not name %s", err, env)
}
}

// TestIssueTrackerBaseURLAllowed: an empty list refuses everything, a listed host is allowed, and
// the list is separate from the forge list.
func TestIssueTrackerBaseURLAllowed(t *testing.T) {
if (Config{}).IssueTrackerBaseURLAllowed("https://issues.example.com") {
t.Error("an empty allowlist allowed a URL")
}
c := Config{
ForgeAllowedBaseURLs: []string{"https://gitlab.example.com"},
IssueTrackerAllowedBaseURLs: []string{"https://issues.example.com"},
}
for _, ok := range []string{"https://issues.example.com", "https://ISSUES.example.com/"} {
if !c.IssueTrackerBaseURLAllowed(ok) {
t.Errorf("IssueTrackerBaseURLAllowed(%q) = false, want true", ok)
}
}
for _, no := range []string{"https://evil.example.com", "http://issues.example.com", "https://issues.example.com:8443", "https://gitlab.example.com", "not a url", ""} {
if c.IssueTrackerBaseURLAllowed(no) {
t.Errorf("IssueTrackerBaseURLAllowed(%q) = true, want false", no)
}
}
}
34 changes: 34 additions & 0 deletions api/internal/issuetracker/bounded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package issuetracker

import (
"io"

"github.com/vtmocanu/uzi/api/internal/forge"
)

const (
// listPageBytes caps one page of a list response.
listPageBytes int64 = 8 << 20
// objectBytes caps a single-object response.
objectBytes int64 = 2 << 20
// errorBodyBytes caps the body read from an error response.
errorBodyBytes int64 = 4 << 10
)

// readBounded reads at most limit bytes of r. A longer body returns a
// *forge.ResponseTooLargeError carrying prov, an Outcome classified from the
// HTTP status of the response, and no remote content. Overflow is checked
// before the read error so an oversize stays identifiable. The caller closes
// the body; readBounded does not. The limit counts bytes after transparent
// decompression, which is what r yields, deliberately (forge instead forces
// identity encoding).
func readBounded(r io.Reader, limit int64, prov forge.ResponseProvenance, status int) ([]byte, error) {
body, err := io.ReadAll(io.LimitReader(r, limit+1))
if int64(len(body)) > limit {
return nil, &forge.ResponseTooLargeError{Provenance: prov, Outcome: classifyStatus(status)}
}
if err != nil {
return nil, err
}
return body, nil
}
88 changes: 88 additions & 0 deletions api/internal/issuetracker/bounded_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package issuetracker

import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/iotest"

"github.com/vtmocanu/uzi/api/internal/forge"
)

func TestReadBounded(t *testing.T) {
got, err := readBounded(strings.NewReader("hello"), 5, forge.ResponseSuccess, 200)
if err != nil || string(got) != "hello" {
t.Fatalf("at limit: %q, %v", got, err)
}
_, err = readBounded(strings.NewReader("hello!"), 5, forge.ResponseIssueSuccess, 200)
if !errors.Is(err, forge.ErrResponseTooLarge) {
t.Fatalf("err = %v, want ErrResponseTooLarge", err)
}
if !forge.IsIssueResponseTooLarge(err) {
t.Fatal("provenance not issue success")
}
_, err = readBounded(strings.NewReader("hello!"), 5, forge.ResponseError, 200)
if forge.IsIssueResponseTooLarge(err) {
t.Fatal("error provenance reported as issue success")
}
}

func TestReadBoundedCapsDecompressedBytes(t *testing.T) {
var gz bytes.Buffer
zw := gzip.NewWriter(&gz)
if _, err := zw.Write(bytes.Repeat([]byte("a"), 64<<10)); err != nil {
t.Fatal(err)
}
if err := zw.Close(); err != nil {
t.Fatal(err)
}
if gz.Len() > 1<<10 {
t.Fatalf("fixture not small: %d", gz.Len())
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
_, _ = w.Write(gz.Bytes())
}))
defer srv.Close()

resp, err := newHTTPClient(0, nil).Get(srv.URL)
if err != nil {
t.Fatal(err)
}
defer func() { _ = resp.Body.Close() }()
if resp.ContentLength != -1 {
t.Fatalf("declared length %d", resp.ContentLength)
}
_, err = readBounded(resp.Body, 1<<10, forge.ResponseIssueSuccess, 200)
if !forge.IsIssueResponseTooLarge(err) {
t.Fatalf("decompressed overflow not capped: %v", err)
}
}

func TestReadBoundedOversizeClassFromStatus(t *testing.T) {
_, err := readBounded(strings.NewReader("hello!"), 5, forge.ResponseError, 503)
if got := forge.Class(err); got != forge.ErrorClassServerError {
t.Fatalf("Class = %q, want server_error", got)
}
r := newRedactor(fixtureCredential())
if got := forge.Class(r.errorStatus(&forge.ResponseTooLargeError{Provenance: forge.ResponseError}, 503)); got != forge.ErrorClassServerError {
t.Fatalf("redacted Class = %q, want server_error", got)
}
bare := fmt.Errorf("x: %w", forge.ErrResponseTooLarge)
if got := r.errorStatus(bare, 429); !errors.Is(got, forge.ErrResponseTooLarge) || forge.Class(got) != forge.ErrorClassRateLimited {
t.Fatalf("bare oversize: %v class %q", got, forge.Class(got))
}
}

func TestReadBoundedOverflowBeatsReadError(t *testing.T) {
r := io.MultiReader(strings.NewReader("hello!"), iotest.ErrReader(errors.New("late")))
if _, err := readBounded(r, 5, forge.ResponseIssueSuccess, 200); !forge.IsIssueResponseTooLarge(err) {
t.Fatalf("err = %v", err)
}
}
Loading
Loading