package main // Tests for the F9 remediation of the any-port escape hatch on URL uploads: // the 80/443 allowlist fails safe — only exactly "1" (either env var) lifts // it, and careless truthy values leave it enforced. import ( "net/url" "testing" ) func mustURL(t *testing.T, raw string) *url.URL { t.Helper() u, err := url.Parse(raw) if err != nil { t.Fatalf("url.Parse(%q): %v", raw, err) } return u } func TestUploadURLPortAllowedDefault(t *testing.T) { for _, raw := range []string{ "https://example.com/a", "https://example.com:443/a", "http://example.com/a", "http://example.com:80/a", } { if !uploadURLPortAllowed(mustURL(t, raw)) { t.Errorf("expected allowed: %s", raw) } } for _, raw := range []string{ "https://example.com:8081/a", "http://example.com:3000/a", } { if uploadURLPortAllowed(mustURL(t, raw)) { t.Errorf("expected blocked by default: %s", raw) } } } func TestUploadURLPortAllowedEscapeHatch(t *testing.T) { const anyPort = "https://example.com:8081/a" t.Setenv("REMOTE_FETCH_ALLOW_ANY_PORT", "1") if !uploadURLPortAllowed(mustURL(t, anyPort)) { t.Error("REMOTE_FETCH_ALLOW_ANY_PORT=1 should lift the port allowlist") } t.Setenv("REMOTE_FETCH_ALLOW_ANY_PORT", "") t.Setenv("ADPANEL_NODE_FETCH_ALLOW_ANY_PORT", "1") if !uploadURLPortAllowed(mustURL(t, anyPort)) { t.Error("ADPANEL_NODE_FETCH_ALLOW_ANY_PORT=1 should lift the port allowlist") } } func TestUploadURLPortAllowedCarelessValuesFailClosed(t *testing.T) { const anyPort = "https://example.com:8081/a" for _, val := range []string{"true", "yes", "on"} { t.Setenv("REMOTE_FETCH_ALLOW_ANY_PORT", val) if uploadURLPortAllowed(mustURL(t, anyPort)) { t.Errorf("value %q must not lift the port allowlist (only \"1\")", val) } } // Surrounding whitespace is formatting noise, not a different value — // trimmed the same way in panel (JS) and node (Go). for _, val := range []string{" 1", "1 "} { t.Setenv("REMOTE_FETCH_ALLOW_ANY_PORT", val) if !uploadURLPortAllowed(mustURL(t, anyPort)) { t.Errorf("value %q should be treated as \"1\"", val) } } }