fix: close Ticket 11 hardening review gaps
This commit is contained in:
+135
-28
@@ -263,27 +263,140 @@ func normalizeBypassEntry(raw string) (string, error) {
|
||||
return host, nil
|
||||
}
|
||||
|
||||
var (
|
||||
schemeCredentialPattern = regexp.MustCompile(`(?i)((?:https?|socks5h?)://)([^/@:\r\n]+):([^@\r\n]*)@`)
|
||||
bareCredentialPattern = regexp.MustCompile(`(?i)([^/@\s:]+):([^@/\s]*)@`)
|
||||
)
|
||||
|
||||
// RedactText removes URL passwords from diagnostic text without requiring the
|
||||
// surrounding text to be a valid URL. The scheme-aware first pass deliberately
|
||||
// accepts spaces and other malformed password bytes up to @; diagnostics and
|
||||
// form validation must not expose a credential merely because parsing failed.
|
||||
func RedactText(value string) string {
|
||||
value = schemeCredentialPattern.ReplaceAllString(value, `${1}${2}:REDACTED@`)
|
||||
return bareCredentialPattern.ReplaceAllString(value, `${1}:REDACTED@`)
|
||||
type credentialSpan struct {
|
||||
userinfoStart int
|
||||
passwordStart int
|
||||
at int
|
||||
}
|
||||
|
||||
// credentialSpans is deliberately a scanner rather than a URL regular
|
||||
// expression. Diagnostics routinely contain values rejected by net/url, and a
|
||||
// malformed password may contain whitespace, CR/LF, slashes, or additional @
|
||||
// signs. The scanner requires the comparatively stable user:...@host shape,
|
||||
// chooses an @ only when what follows can be an authority, and stops each
|
||||
// replacement at that @ so surrounding diagnostics remain untouched.
|
||||
func credentialSpans(value string) []credentialSpan {
|
||||
var spans []credentialSpan
|
||||
for cursor := 0; cursor < len(value); {
|
||||
colon := strings.IndexByte(value[cursor:], ':')
|
||||
if colon < 0 {
|
||||
break
|
||||
}
|
||||
colon += cursor
|
||||
if colon+2 < len(value) && value[colon+1:colon+3] == "//" {
|
||||
cursor = colon + 3
|
||||
continue
|
||||
}
|
||||
start := credentialUsernameStart(value, colon)
|
||||
if start == colon || (colon+1 < len(value) && isAuthorityBoundary(value[colon+1]) && !hasSchemeAuthorityPrefix(value, start)) {
|
||||
cursor = colon + 1
|
||||
continue
|
||||
}
|
||||
at := credentialDelimiter(value, colon+1)
|
||||
if at < 0 || hasNestedCredentialStart(value, colon+1, at) {
|
||||
cursor = colon + 1
|
||||
continue
|
||||
}
|
||||
spans = append(spans, credentialSpan{userinfoStart: start, passwordStart: colon + 1, at: at})
|
||||
cursor = at + 1
|
||||
}
|
||||
return spans
|
||||
}
|
||||
|
||||
func credentialUsernameStart(value string, colon int) int {
|
||||
start := colon
|
||||
for start > 0 && isCredentialUsernameByte(value[start-1]) {
|
||||
start--
|
||||
}
|
||||
if start > 0 && (isCredentialUsernameByte(value[start-1]) || value[start-1] == '@') {
|
||||
return colon
|
||||
}
|
||||
if start > 0 && value[start-1] == '/' && (start < 3 || value[start-3:start] != "://") {
|
||||
return colon
|
||||
}
|
||||
return start
|
||||
}
|
||||
|
||||
func hasSchemeAuthorityPrefix(value string, usernameStart int) bool {
|
||||
return usernameStart >= 3 && value[usernameStart-3:usernameStart] == "://"
|
||||
}
|
||||
|
||||
func isCredentialUsernameByte(value byte) bool {
|
||||
return value >= 'a' && value <= 'z' || value >= 'A' && value <= 'Z' ||
|
||||
value >= '0' && value <= '9' || strings.ContainsRune("._~%+-", rune(value))
|
||||
}
|
||||
|
||||
func credentialDelimiter(value string, passwordStart int) int {
|
||||
for at := passwordStart; at < len(value); at++ {
|
||||
if value[at] != '@' {
|
||||
continue
|
||||
}
|
||||
authorityStart := at + 1
|
||||
end := authorityStart
|
||||
for end < len(value) && !isAuthorityBoundary(value[end]) {
|
||||
end++
|
||||
}
|
||||
if end == authorityStart || strings.ContainsRune(value[authorityStart:end], '@') {
|
||||
continue
|
||||
}
|
||||
return at
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func isAuthorityBoundary(value byte) bool {
|
||||
switch value {
|
||||
case ' ', '\t', '\r', '\n', '/', '?', '#', '\'', '"', '<', '>', '[', ']', '(', ')', '{', '}', ',', ';':
|
||||
return true
|
||||
default:
|
||||
return value < 0x20 || value == 0x7f
|
||||
}
|
||||
}
|
||||
|
||||
func hasNestedCredentialStart(value string, passwordStart, at int) bool {
|
||||
for nested := passwordStart; nested < at; nested++ {
|
||||
if value[nested] != ':' {
|
||||
continue
|
||||
}
|
||||
// A later scheme authority proves the earlier colon was prose (for
|
||||
// example "Proxy URL:") rather than the credential separator.
|
||||
if nested+2 < len(value) && value[nested+1:nested+3] == "//" {
|
||||
return true
|
||||
}
|
||||
if credentialUsernameStart(value, nested) < nested && credentialDelimiter(value, nested+1) == at {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func redactCredentialSpans(value string, removeUsername bool) string {
|
||||
spans := credentialSpans(value)
|
||||
if len(spans) == 0 {
|
||||
return value
|
||||
}
|
||||
var redacted strings.Builder
|
||||
last := 0
|
||||
for _, span := range spans {
|
||||
redacted.WriteString(value[last:span.userinfoStart])
|
||||
if !removeUsername {
|
||||
redacted.WriteString(value[span.userinfoStart:span.passwordStart])
|
||||
}
|
||||
redacted.WriteString("REDACTED@")
|
||||
last = span.at + 1
|
||||
}
|
||||
redacted.WriteString(value[last:])
|
||||
return redacted.String()
|
||||
}
|
||||
|
||||
// RedactText removes passwords from valid and malformed credential-bearing
|
||||
// text while retaining usernames for diagnostics where that behavior is useful.
|
||||
func RedactText(value string) string { return redactCredentialSpans(value, false) }
|
||||
|
||||
// RedactUserinfoText removes usernames as well as passwords. Form frames use
|
||||
// this stricter boundary because even the identity of the probe source is
|
||||
// private while a Profile is being edited.
|
||||
func RedactUserinfoText(value string) string {
|
||||
value = schemeCredentialPattern.ReplaceAllString(value, `${1}REDACTED@`)
|
||||
return bareCredentialPattern.ReplaceAllString(value, `REDACTED@`)
|
||||
}
|
||||
func RedactUserinfoText(value string) string { return redactCredentialSpans(value, true) }
|
||||
|
||||
func ObscureURL(value string) string {
|
||||
u, err := url.Parse(value)
|
||||
@@ -301,22 +414,16 @@ func ObscureURL(value string) string {
|
||||
// rune count. A text input clone can therefore retain its real cursor and
|
||||
// horizontal viewport positions without rendering either username or password.
|
||||
func ObscureURLForEditing(value string) string {
|
||||
masked := maskCredentialMatches(value, schemeCredentialPattern, 4, 7)
|
||||
return maskCredentialMatches(masked, bareCredentialPattern, 2, 5)
|
||||
}
|
||||
|
||||
func maskCredentialMatches(value string, pattern *regexp.Regexp, userinfoStartGroup, userinfoEndGroup int) string {
|
||||
matches := pattern.FindAllStringSubmatchIndex(value, -1)
|
||||
if len(matches) == 0 {
|
||||
spans := credentialSpans(value)
|
||||
if len(spans) == 0 {
|
||||
return value
|
||||
}
|
||||
var masked strings.Builder
|
||||
last := 0
|
||||
for _, match := range matches {
|
||||
userinfoStart, userinfoEnd := match[userinfoStartGroup], match[userinfoEndGroup]
|
||||
masked.WriteString(value[last:userinfoStart])
|
||||
masked.WriteString(strings.Repeat("•", len([]rune(value[userinfoStart:userinfoEnd]))))
|
||||
last = userinfoEnd
|
||||
for _, span := range spans {
|
||||
masked.WriteString(value[last:span.userinfoStart])
|
||||
masked.WriteString(strings.Repeat("•", len([]rune(value[span.userinfoStart:span.at]))))
|
||||
last = span.at
|
||||
}
|
||||
masked.WriteString(value[last:])
|
||||
return masked.String()
|
||||
|
||||
Reference in New Issue
Block a user