mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
This commit is contained in:
@@ -308,29 +308,146 @@ func sanitizeProviderErr(lastErr, rawBaseURL string) string {
|
||||
out := replaceURLForm(lastErr, rawBaseURL)
|
||||
u, parseErr := url.Parse(rawBaseURL)
|
||||
if parseErr != nil {
|
||||
// The parse error quotes part of the URL (invalid port ":TOKEN").
|
||||
if ue, ok := parseErr.(*url.Error); ok {
|
||||
for _, r := range textRenderings(ue.Err.Error()) {
|
||||
out = strings.ReplaceAll(out, r, errInvalidURLText)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
// net/http reports url.URL.String(), which lower-cases the scheme and
|
||||
// re-escapes userinfo, so it can differ from the configured text.
|
||||
out = replaceURLForm(out, u.String())
|
||||
if u.User == nil {
|
||||
return out
|
||||
if u.User != nil {
|
||||
if _, hasPassword := u.User.Password(); hasPassword {
|
||||
// net/http's exact stripPassword output, mirrored call for call.
|
||||
masked := strings.Replace(u.String(), u.User.String()+"@", u.User.Username()+":***@", 1)
|
||||
out = replaceURLForm(out, masked)
|
||||
}
|
||||
}
|
||||
if _, hasPassword := u.User.Password(); !hasPassword {
|
||||
return out
|
||||
}
|
||||
// net/http's exact stripPassword output, mirrored call for call.
|
||||
masked := strings.Replace(u.String(), u.User.String()+"@", u.User.Username()+":***@", 1)
|
||||
return replaceURLForm(out, masked)
|
||||
return scrubSecretComponents(out, u)
|
||||
}
|
||||
|
||||
// replaceURLForm replaces every occurrence of one rendering of a URL in
|
||||
// msg with sanitizeBaseURL's cleaned form of that same rendering.
|
||||
// errInvalidURLText replaces a configured URL's parse error, which quotes
|
||||
// part of the URL.
|
||||
const errInvalidURLText = "invalid url"
|
||||
|
||||
// secretMinBytes and commonURLWords keep the component scrub from blanking
|
||||
// short or ordinary text that a credential happens to equal.
|
||||
const secretMinBytes = 4
|
||||
|
||||
var commonURLWords = map[string]bool{"http": true, "https": true, "true": true, "false": true, "null": true}
|
||||
|
||||
// secretRedaction replaces a credential component of the configured URL.
|
||||
const secretRedaction = "***"
|
||||
|
||||
// replaceURLForm replaces every rendering of form in msg with
|
||||
// sanitizeBaseURL's cleaned form of form itself.
|
||||
func replaceURLForm(msg, form string) string {
|
||||
if form == "" || !strings.Contains(msg, form) {
|
||||
if form == "" {
|
||||
return msg
|
||||
}
|
||||
return strings.ReplaceAll(msg, form, sanitizeBaseURL(form))
|
||||
clean := sanitizeBaseURL(form)
|
||||
for _, r := range textRenderings(form) {
|
||||
if strings.Contains(msg, r) {
|
||||
msg = strings.ReplaceAll(msg, r, clean)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// scrubSecretComponents redacts every rendering of u's userinfo and query
|
||||
// parts. u must be the configured URL: a requested URL carries client
|
||||
// text, which is neither secret nor cheap to scan.
|
||||
func scrubSecretComponents(msg string, u *url.URL) string {
|
||||
var secrets []string
|
||||
if u.User != nil {
|
||||
secrets = append(secrets, u.User.Username(), url.User(u.User.Username()).String())
|
||||
if pw, ok := u.User.Password(); ok {
|
||||
secrets = append(secrets, pw, url.UserPassword("", pw).String()[1:])
|
||||
}
|
||||
}
|
||||
// Query() drops ";"-separated pairs and valueless keys, so split by hand.
|
||||
for _, part := range strings.FieldsFunc(u.RawQuery, func(r rune) bool { return r == '&' || r == ';' }) {
|
||||
// A name is a secret only when valueless (?TOKEN); otherwise it is a label such as timeout.
|
||||
piece := part
|
||||
if eq := strings.IndexByte(part, '='); eq >= 0 {
|
||||
piece = part[eq+1:]
|
||||
}
|
||||
secrets = append(secrets, piece)
|
||||
if dec, err := url.QueryUnescape(piece); err == nil {
|
||||
secrets = append(secrets, dec)
|
||||
}
|
||||
}
|
||||
var forms []string
|
||||
for _, secret := range secrets {
|
||||
if len(secret) < secretMinBytes || commonURLWords[strings.ToLower(secret)] {
|
||||
continue
|
||||
}
|
||||
forms = append(forms, textRenderings(secret)...)
|
||||
}
|
||||
sort.Slice(forms, func(a, b int) bool {
|
||||
if len(forms[a]) != len(forms[b]) {
|
||||
return len(forms[a]) > len(forms[b])
|
||||
}
|
||||
return forms[a] < forms[b]
|
||||
})
|
||||
for _, f := range forms {
|
||||
msg = strings.ReplaceAll(msg, f, secretRedaction)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// lowerPercentHex rewrites %XX escapes with lower-case hex digits.
|
||||
func lowerPercentHex(s string) string {
|
||||
b := []byte(s)
|
||||
for i := 0; i+2 < len(b); i++ {
|
||||
if b[i] == '%' {
|
||||
b[i+1] = lowerASCII(b[i+1])
|
||||
b[i+2] = lowerASCII(b[i+2])
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func lowerASCII(c byte) byte {
|
||||
if c >= 'A' && c <= 'F' {
|
||||
return c + ('a' - 'A')
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// textRenderings lists s as Go's error paths print it: verbatim, as the
|
||||
// body of a %q verb (*url.Error), and query- or path-escaped. Longest
|
||||
// first, so a rendering that contains another is replaced whole.
|
||||
func textRenderings(s string) []string {
|
||||
quoted := strconv.Quote(s)
|
||||
qe, pe := url.QueryEscape(s), url.PathEscape(s)
|
||||
cands := []string{s, quoted[1 : len(quoted)-1], qe, pe, lowerPercentHex(qe), lowerPercentHex(pe)}
|
||||
out := make([]string, 0, len(cands))
|
||||
for _, c := range cands {
|
||||
if c == "" {
|
||||
continue
|
||||
}
|
||||
dup := false
|
||||
for _, o := range out {
|
||||
if o == c {
|
||||
dup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if dup {
|
||||
continue
|
||||
}
|
||||
pos := len(out)
|
||||
out = append(out, c)
|
||||
for pos > 0 && len(out[pos-1]) < len(c) {
|
||||
out[pos], out[pos-1] = out[pos-1], out[pos]
|
||||
pos--
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// adminProviderView is one provider's read-only view in GET
|
||||
|
||||
Reference in New Issue
Block a user