check / check (push) Canceled after 0s
seal.DeriveKeys derives the session hash and block keys, the encrypted URL key and the CSRF key from the signing key, with their salts declared beside it, and returns them as one seal.Keys. internal/providers calls it once at startup and provides the value to the fx graph. The session manager, the encrypted URL generator and the CSRF protection each take their own key. session.NewManager, encurl.NewGenerator, newCSRFProtect and handlers.New return no error, as nothing in them can fail now. Each key is derived as before, with seal.DeriveKey and the same salt. The signing key itself stays the HMAC key of signed URLs and the login password. README.md says under signing_key what it protects. Model: opus-5-5
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/pixa/internal/config"
|
|
"sneak.berlin/go/pixa/internal/seal"
|
|
"sneak.berlin/go/pixa/internal/session"
|
|
)
|
|
|
|
// TestLoginLogLeavesOutSubmittedKey verifies that the log lines for a
|
|
// failed and for a successful login do not contain the submitted key.
|
|
func TestLoginLogLeavesOutSubmittedKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const wrongKey = "wrong-signing-key-fedcba9876543210"
|
|
|
|
var buf bytes.Buffer
|
|
|
|
keys, err := seal.DeriveKeys(testSigningKey)
|
|
if err != nil {
|
|
t.Fatalf("seal.DeriveKeys() error = %v", err)
|
|
}
|
|
|
|
h := &Handlers{
|
|
log: slog.New(slog.NewJSONHandler(&buf, nil)),
|
|
config: &config.Config{SigningKey: testSigningKey},
|
|
sessMgr: session.NewManager(keys.SessionHashKey, keys.SessionBlockKey),
|
|
}
|
|
|
|
submittedKeys := []string{wrongKey, testSigningKey}
|
|
|
|
for _, key := range submittedKeys {
|
|
form := url.Values{loginKeyField: {key}}
|
|
req := httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodPost, "/",
|
|
strings.NewReader(form.Encode()))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
h.handleLoginPost(httptest.NewRecorder(), req)
|
|
}
|
|
|
|
for _, msg := range []string{"failed login attempt", "successful login"} {
|
|
if !strings.Contains(buf.String(), msg) {
|
|
t.Fatalf("log missing %q; got %q", msg, buf.String())
|
|
}
|
|
}
|
|
|
|
for _, key := range submittedKeys {
|
|
if strings.Contains(buf.String(), key) {
|
|
t.Errorf("log contains submitted key %q; got %q", key, buf.String())
|
|
}
|
|
}
|
|
}
|