Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor: single-source url redaction behind urls.Redact with an explicit cap

Luigi Agosti89e2ac136f34
diff --git a/internal/activity/url_sanitize.go b/internal/activity/url_sanitize.go
index 234840a5..2fe33d95 100644
--- a/internal/activity/url_sanitize.go
+++ b/internal/activity/url_sanitize.go
@@ -1,49 +1,11 @@
package activity
-import (
- "net"
- "net/url"
- "strings"
-
- "github.com/pinchtab/pinchtab/internal/sanitize"
- internalurls "github.com/pinchtab/pinchtab/internal/urls"
-)
+import internalurls "github.com/pinchtab/pinchtab/internal/urls"
const maxActivityURLBytes = 2048
+// sanitizeActivityURL redacts raw for the activity feed. The feed keeps a
+// larger cap than logs; every other rule is shared, so it is not restated here.
func sanitizeActivityURL(raw string) string {
- raw = strings.TrimSpace(raw)
- if raw == "" {
- return ""
- }
-
- normalized, err := internalurls.Sanitize(raw)
- if err != nil {
- parsed, parseErr := url.Parse(raw)
- if parseErr != nil || (parsed.Scheme == "" && parsed.Host == "" && parsed.Opaque == "") {
- return ""
- }
- normalized = raw
- }
-
- parsed, err := url.Parse(normalized)
- if err != nil {
- return ""
- }
-
- parsed.User = nil
- parsed.RawQuery = ""
- parsed.Fragment = ""
- parsed.ForceQuery = false
-
- if parsed.Host != "" {
- host := strings.ToLower(parsed.Hostname())
- if port := parsed.Port(); port != "" {
- parsed.Host = net.JoinHostPort(host, port)
- } else {
- parsed.Host = host
- }
- }
-
- return sanitize.TruncateUTF8BytesWithEllipsis(parsed.String(), maxActivityURLBytes)
+ return internalurls.Redact(raw, maxActivityURLBytes)
}
diff --git a/internal/activity/url_sanitize_test.go b/internal/activity/url_sanitize_test.go
new file mode 100644
index 00000000..3ae84070
--- /dev/null
+++ b/internal/activity/url_sanitize_test.go
@@ -0,0 +1,97 @@
+package activity
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/sanitize"
+ internalurls "github.com/pinchtab/pinchtab/internal/urls"
+)
+
+// logCapBytes reads the log cap off the function instead of restating it: an
+// input longer than any cap comes back exactly cap-length, so this cannot go
+// stale if internal/urls changes its constant.
+func logCapBytes() int {
+ return len(internalurls.RedactForLog("https://example.com/" + strings.Repeat("a", 8192)))
+}
+
+// The two redactors are one control with two caps. This is the guard on that
+// claim: for every input, the log form must be exactly the activity form put
+// through the smaller cap — so a future edit to either caller that changes
+// anything other than the cap reds here.
+func TestBothRedactorsAgreeExceptWhereTheCapBites(t *testing.T) {
+ inputs := []string{
+ "/path://x",
+ "https://u:[email protected]:8443/a?token=secret#frag",
+ "javascript:alert(1)",
+ "foo/bar",
+ "://foo",
+ "mailto://a@b?x=1",
+ "./rel://x",
+ "../up://x",
+ "about:blank#frag",
+ "pinchtab.com/reset?token=secret",
+ "ftp://u:[email protected]:21/x?y#z",
+ "",
+ " ",
+ // Longer than the log cap, shorter than the activity cap, so the cap is
+ // the only thing that can make the two disagree on it.
+ "https://example.com/" + strings.Repeat("a", 700),
+ }
+ if len(inputs) == 0 {
+ t.Fatal("the shared input set is empty, so this parity guard proves nothing")
+ }
+
+ logCap := logCapBytes()
+ if logCap <= 0 {
+ t.Fatal("could not determine the log cap; the parity comparison below would be meaningless")
+ }
+
+ capBit := 0
+ for _, raw := range inputs {
+ logged := internalurls.RedactForLog(raw)
+ recorded := sanitizeActivityURL(raw)
+
+ want := sanitize.TruncateUTF8BytesWithEllipsis(recorded, logCap)
+ if logged != want {
+ t.Errorf("the two redactors disagree on %q beyond the byte cap:\n log = %q\n activity = %q\nOne security control implemented twice has drifted again.", raw, logged, recorded)
+ continue
+ }
+ if logged != recorded {
+ capBit++
+ }
+ }
+
+ if capBit == 0 {
+ t.Fatal("no input was long enough for the caps to differ, so this guard never proved the cap is the ONLY difference; add a case longer than the log cap")
+ }
+}
+
+func TestActivityFeedDropsRelativePathsThatMerelyContainASchemeSeparator(t *testing.T) {
+ for _, raw := range []string{"/path://x", "./rel://x", "../up://x"} {
+ if got := sanitizeActivityURL(raw); got != "" {
+ t.Errorf("sanitizeActivityURL(%q) = %q, want %q; these used to be recorded verbatim because the validity guard was skipped on the Sanitize success path", raw, got, "")
+ }
+ }
+}
+
+func TestActivityFeedStillRecordsRealURLs(t *testing.T) {
+ for _, tc := range []struct{ raw, want string }{
+ {"https://u:[email protected]:8443/a?token=secret#frag", "https://example.com:8443/a"},
+ {"pinchtab.com/reset?token=secret", "https://pinchtab.com/reset"},
+ {"about:blank#frag", "about:blank"},
+ {"path://x", "path://x"},
+ } {
+ if got := sanitizeActivityURL(tc.raw); got != tc.want {
+ t.Errorf("sanitizeActivityURL(%q) = %q, want %q; the stricter guard must not drop real URLs", tc.raw, got, tc.want)
+ }
+ }
+}
+
+func TestActivityCapIsLargerThanTheLogCap(t *testing.T) {
+ raw := "https://example.com/" + strings.Repeat("a", 700)
+ logCap := logCapBytes()
+ if got := len(sanitizeActivityURL(raw)); got <= logCap {
+ t.Errorf("activity kept %d bytes, want more than the log cap of %d", got, logCap)
+ }
+}
diff --git a/internal/urls/log_redact.go b/internal/urls/log_redact.go
index 163c76a9..df287e85 100644
--- a/internal/urls/log_redact.go
+++ b/internal/urls/log_redact.go
@@ -10,11 +10,12 @@ import (
const maxLogURLBytes = 512
-// RedactForLog normalizes a URL for logs and strips sensitive components.
-// It removes userinfo, query, and fragment and caps the final string length.
-// Invalid inputs return an empty string rather than echoing raw potentially
-// sensitive data back into logs.
-func RedactForLog(raw string) string {
+// Redact is the single implementation of the URL redaction control: it strips
+// the components that must never reach a persisted record — userinfo, query and
+// fragment — lowercases the host, and caps the result at maxBytes. Inputs that
+// are not URLs return an empty string rather than echoing possibly-sensitive
+// text through. Callers differ only in the cap they pass.
+func Redact(raw string, maxBytes int) string {
raw = strings.TrimSpace(raw)
if raw == "" {
return ""
@@ -29,6 +30,9 @@ func RedactForLog(raw string) string {
if err != nil {
return ""
}
+ // Sanitize returns any string containing "://" verbatim, so this guard has
+ // to run on its success path too: a relative path such as "/path://x"
+ // arrives here parsed as neither scheme, host nor opaque.
if parsed.Scheme == "" && parsed.Host == "" && parsed.Opaque == "" {
return ""
}
@@ -47,5 +51,10 @@ func RedactForLog(raw string) string {
}
}
- return sanitize.TruncateUTF8BytesWithEllipsis(parsed.String(), maxLogURLBytes)
+ return sanitize.TruncateUTF8BytesWithEllipsis(parsed.String(), maxBytes)
+}
+
+// RedactForLog redacts raw for log output.
+func RedactForLog(raw string) string {
+ return Redact(raw, maxLogURLBytes)
}
diff --git a/internal/urls/log_redact_test.go b/internal/urls/log_redact_test.go
new file mode 100644
index 00000000..c18e0e29
--- /dev/null
+++ b/internal/urls/log_redact_test.go
@@ -0,0 +1,62 @@
+package urls
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestRedactStripsEveryCredentialBearingComponent(t *testing.T) {
+ const raw = "https://u:[email protected]:8443/a?token=secret#frag"
+ got := Redact(raw, maxLogURLBytes)
+
+ if got != "https://example.com:8443/a" {
+ t.Fatalf("Redact(%q) = %q, want %q", raw, got, "https://example.com:8443/a")
+ }
+ for _, leaked := range []string{"u:p@", "token", "secret", "frag", "?", "#", "Example.COM"} {
+ if strings.Contains(got, leaked) {
+ t.Errorf("redacted URL still carries %q: %q", leaked, got)
+ }
+ }
+}
+
+func TestRedactRejectsInputsThatAreNotURLs(t *testing.T) {
+ // Sanitize passes anything containing "://" through untouched, so these
+ // reach the parse step looking valid and are caught only by the guard.
+ for _, raw := range []string{"/path://x", "./rel://x", "../up://x", "://foo", "", " "} {
+ if got := Redact(raw, maxLogURLBytes); got != "" {
+ t.Errorf("Redact(%q) = %q, want %q; a non-URL must not be echoed into a persisted record", raw, got, "")
+ }
+ }
+}
+
+func TestRedactKeepsInputsThatAreURLs(t *testing.T) {
+ for _, tc := range []struct{ raw, want string }{
+ {"path://x", "path://x"},
+ {"x://y", "x://y"},
+ {"foo/bar", "https://foo/bar"},
+ {"about:blank#frag", "about:blank"},
+ {"mailto://a@b?x=1", "mailto://b"},
+ {"javascript:alert(1)", "javascript:alert(1)"},
+ } {
+ if got := Redact(tc.raw, maxLogURLBytes); got != tc.want {
+ t.Errorf("Redact(%q) = %q, want %q", tc.raw, got, tc.want)
+ }
+ }
+}
+
+func TestRedactAppliesTheCapItIsGiven(t *testing.T) {
+ raw := "https://example.com/" + strings.Repeat("a", 700)
+
+ short := Redact(raw, 512)
+ long := Redact(raw, 2048)
+
+ if len(short) != 512 {
+ t.Errorf("len(Redact(raw, 512)) = %d, want 512", len(short))
+ }
+ if len(long) <= 512 {
+ t.Errorf("len(Redact(raw, 2048)) = %d, want the larger cap to keep more of the URL", len(long))
+ }
+ if !strings.HasPrefix(long, "https://example.com/") {
+ t.Errorf("the redacted URL lost its origin: %q", long[:40])
+ }
+}