diff options
Diffstat (limited to 'internal/server/auth_test.go')
| -rw-r--r-- | internal/server/auth_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/server/auth_test.go b/internal/server/auth_test.go new file mode 100644 index 0000000..ecd3c08 --- /dev/null +++ b/internal/server/auth_test.go @@ -0,0 +1,50 @@ +package server + +import ( + "crypto/subtle" + "testing" +) + +// TestConstantTimePasswordCompare verifies that password comparisons use +// constant-time comparison to prevent timing side-channel attacks. +// This is a regression test for the bug where `!=` was used directly, +// leaking timing information to an attacker who can measure response latency. +func TestConstantTimePasswordCompare(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + a string + b string + wantSame bool + }{ + {"equal passwords", "secret123", "secret123", true}, + {"different passwords", "secret123", "wrongpass", false}, + {"empty vs non-empty", "", "secret", false}, + {"both empty", "", "", true}, + {"prefix match only", "secret", "secretXYZ", false}, + {"suffix match only", "XYZsecret", "secret", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + // secretsEqual is the function under test — it must use + // crypto/subtle.ConstantTimeCompare, not plain ==. + got := secretsEqual(tt.a, tt.b) + if got != tt.wantSame { + t.Errorf("secretsEqual(%q, %q) = %v, want %v", + tt.a, tt.b, got, tt.wantSame) + } + + // Cross-check with the reference implementation to ensure our + // helper is semantically correct, not just timing-safe. + reference := subtle.ConstantTimeCompare([]byte(tt.a), []byte(tt.b)) == 1 + if got != reference { + t.Errorf("secretsEqual(%q, %q) = %v but crypto/subtle gives %v", + tt.a, tt.b, got, reference) + } + }) + } +} |
