mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(api): report upstream hosts in /api/system
Each registry upstream shows its scheme and host only. The endpoint drops credentials, paths and query strings from the configured URL.
This commit is contained in:
@@ -2,6 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
@@ -35,6 +36,15 @@ func scannerSwitches(s config.ScannersConfig) []scannerSwitch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// originOnly keeps scheme and host, so credentials, paths and queries in an upstream URL never leave the server.
|
||||||
|
func originOnly(raw string) string {
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil || u.Host == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return u.Scheme + "://" + u.Host
|
||||||
|
}
|
||||||
|
|
||||||
func seconds(d time.Duration) int64 { return int64(d / time.Second) }
|
func seconds(d time.Duration) int64 { return int64(d / time.Second) }
|
||||||
|
|
||||||
// systemScanners joins the configured scanners with their cached health; disabled scanners report no health.
|
// systemScanners joins the configured scanners with their cached health; disabled scanners report no health.
|
||||||
@@ -99,6 +109,11 @@ func (a *App) handleSystem(c fiber.Ctx) error {
|
|||||||
"on_access": sec.RescanOnAccess,
|
"on_access": sec.RescanOnAccess,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"upstreams": fiber.Map{
|
||||||
|
"go": originOnly(a.config.Handlers.Go.UpstreamProxy),
|
||||||
|
"npm": originOnly(a.config.Handlers.NPM.UpstreamRegistry),
|
||||||
|
"pypi": originOnly(a.config.Handlers.PyPI.UpstreamURL),
|
||||||
|
},
|
||||||
"cache": fiber.Map{
|
"cache": fiber.Map{
|
||||||
"max_size": a.config.Cache.MaxSizeBytes,
|
"max_size": a.config.Cache.MaxSizeBytes,
|
||||||
"idle_ttl": seconds(a.config.Cache.IdleTTL),
|
"idle_ttl": seconds(a.config.Cache.IdleTTL),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -112,6 +113,25 @@ func TestSystemLeaksNoSecrets(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSystemUpstreamsKeepOnlySchemeAndHost(t *testing.T) {
|
||||||
|
a := newTestApp(t, func(c *config.Config) {
|
||||||
|
c.Handlers.NPM.UpstreamRegistry = fmt.Sprintf("https://%s:%[email protected]:8443/private/path?token=abc", "deploy", "s3cret")
|
||||||
|
c.Handlers.PyPI.UpstreamURL = "not a url"
|
||||||
|
})
|
||||||
|
out, body := getJSON(t, a, "/api/system")
|
||||||
|
if got := jsonAt(out, "upstreams", "npm"); got != "https://npm.example.com:8443" {
|
||||||
|
t.Errorf("npm upstream = %v", got)
|
||||||
|
}
|
||||||
|
if got := jsonAt(out, "upstreams", "pypi"); got != "" {
|
||||||
|
t.Errorf("unparseable upstream = %v, want empty", got)
|
||||||
|
}
|
||||||
|
for _, banned := range []string{"s3cret", "deploy", "private/path", "abc"} {
|
||||||
|
if strings.Contains(body, banned) {
|
||||||
|
t.Errorf("/api/system exposes %q: %s", banned, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSystemRequiresAuthWhenEnabled(t *testing.T) {
|
func TestSystemRequiresAuthWhenEnabled(t *testing.T) {
|
||||||
a := newTestApp(t, func(c *config.Config) { c.Auth.Enabled = true })
|
a := newTestApp(t, func(c *config.Config) { c.Auth.Enabled = true })
|
||||||
code, _, _ := doRequest(t, a, "/api/system", nil)
|
code, _, _ := doRequest(t, a, "/api/system", nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user