Reject IP-literal origins and check errors in store tests
BASE_ORIGIN now refuses any https IP address, since assertion verifiers reject IP origins; only http on loopback stays allowed for local dev. Store tests check errors before asserting refusal, emptiness or absence.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
3 files changed,
+27
−8
internal/config/config.go+3 −0internal/config/config_test.go+15 −0internal/store/store_test.go+9 −8
diff --git a/internal/config/config.go b/internal/config/config.go| index b99a311..592b545 100644 |
| --- a/internal/config/config.go |
| +++ b/internal/config/config.go |
| @@ -106,6 +106,9 @@ func originIsLocal(origin string) (bool, error) { |
| host := strings.ToLower(u.Hostname()) |
| ip := net.ParseIP(host) |
| local := host == "localhost" || (ip != nil && ip.IsLoopback()) |
| + if ip != nil && u.Scheme == "https" { |
| + return false, fmt.Errorf("BASE_ORIGIN %q must use a hostname, not an IP address", origin) |
| + } |
| if u.Scheme != "https" && !(u.Scheme == "http" && local) { |
| return false, fmt.Errorf("BASE_ORIGIN %q must be https (http only on loopback)", origin) |
| } |
diff --git a/internal/config/config_test.go b/internal/config/config_test.go| index c0952f2..08c6743 100644 |
| --- a/internal/config/config_test.go |
| +++ b/internal/config/config_test.go |
| @@ -67,3 +67,18 @@ func TestSlugMakesLegalHosts(t *testing.T) { |
| } |
| } |
| } |
| + |
| +func TestLoadRefusesIPOrigins(t *testing.T) { |
| + for _, origin := range []string{"https://203.0.113.1", "https://127.0.0.1", "https://[::1]:8443", "http://203.0.113.1"} { |
| + m := valid() |
| + m["BASE_ORIGIN"] = origin |
| + if _, err := Load(env(m)); err == nil { |
| + t.Errorf("origin %q was accepted", origin) |
| + } |
| + } |
| + m := valid() |
| + m["BASE_ORIGIN"] = "http://127.0.0.1:8830" |
| + if _, err := Load(env(m)); err != nil { |
| + t.Errorf("loopback http refused: %v", err) |
| + } |
| +} |
diff --git a/internal/store/store_test.go b/internal/store/store_test.go| index a68b927..c87d8f7 100644 |
| --- a/internal/store/store_test.go |
| +++ b/internal/store/store_test.go |
| @@ -53,8 +53,8 @@ func TestClaimInstallOnlyOnce(t *testing.T) { |
| func TestClaimInstallRetriesFailed(t *testing.T) { |
| ctx, db := context.Background(), open(t) |
| - if ok, _ := ClaimInstall(ctx, db, "a", "h"); !ok { |
| - t.Fatal("first claim refused") |
| + if ok, err := ClaimInstall(ctx, db, "a", "h"); err != nil || !ok { |
| + t.Fatalf("first claim = %v, %v", ok, err) |
| } |
| if err := SetInstallState(ctx, db, "a", "failed", "boom"); err != nil { |
| t.Fatal(err) |
| @@ -65,8 +65,8 @@ func TestClaimInstallRetriesFailed(t *testing.T) { |
| if err := SetInstallState(ctx, db, "a", "installed", ""); err != nil { |
| t.Fatal(err) |
| } |
| - if ok, _ := ClaimInstall(ctx, db, "a", "h"); ok { |
| - t.Fatal("claimed an installed app") |
| + if ok, err := ClaimInstall(ctx, db, "a", "h"); err != nil || ok { |
| + t.Fatalf("claim of installed app = %v, %v; want false, nil", ok, err) |
| } |
| is, err := Installs(ctx, db) |
| if err != nil || len(is) != 1 || is[0].State != "installed" { |
| @@ -75,8 +75,9 @@ func TestClaimInstallRetriesFailed(t *testing.T) { |
| if err := DeleteInstall(ctx, db, "a"); err != nil { |
| t.Fatal(err) |
| } |
| - if is, _ := Installs(ctx, db); len(is) != 0 { |
| - t.Fatalf("installs after delete = %+v", is) |
| + is, err = Installs(ctx, db) |
| + if err != nil || len(is) != 0 { |
| + t.Fatalf("installs after delete = %+v, %v", is, err) |
| } |
| } |
| @@ -97,7 +98,7 @@ func TestInviteAndJoin(t *testing.T) { |
| if err != nil || !ok || got.JoinedAt == nil { |
| t.Fatalf("member = %+v %v %v", got, ok, err) |
| } |
| - if _, ok, _ := MemberByEmail(ctx, db, "nobody@example.com"); ok { |
| - t.Error("found a stranger") |
| + if _, ok, err := MemberByEmail(ctx, db, "nobody@example.com"); err != nil || ok { |
| + t.Errorf("stranger lookup = %v, %v; want false, nil", ok, err) |
| } |
| } |