Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(auth): add CSRF state param, scheme validation, and .envrc to gitignore

yushenb9a4fa1a6dc8
diff --git a/.gitignore b/.gitignore
index d9c40de0a5..7d7617b7e0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@ node_modules
dist
*.log
.DS_Store
+.envrc
# build outputs
.next
diff --git a/apps/web/app/(auth)/login/page.tsx b/apps/web/app/(auth)/login/page.tsx
index ee64ee30b4..4f8409d677 100644
--- a/apps/web/app/(auth)/login/page.tsx
+++ b/apps/web/app/(auth)/login/page.tsx
@@ -73,9 +73,14 @@ function LoginPageContent() {
const cliCallback = searchParams.get("cli_callback");
if (cliCallback) {
// CLI browser login: verify code, get JWT, redirect to CLI callback.
- // Only allow localhost callbacks to prevent open redirect / JWT theft.
+ // Only allow http://localhost callbacks to prevent open redirect / JWT theft.
try {
const cbUrl = new URL(cliCallback);
+ if (cbUrl.protocol !== "http:") {
+ setError("Invalid callback URL");
+ setSubmitting(false);
+ return;
+ }
if (cbUrl.hostname !== "localhost" && cbUrl.hostname !== "127.0.0.1") {
setError("Invalid callback URL");
setSubmitting(false);
@@ -87,8 +92,9 @@ function LoginPageContent() {
return;
}
const { token } = await api.verifyCode(email, value);
+ const cliState = searchParams.get("cli_state") || "";
const separator = cliCallback.includes("?") ? "&" : "?";
- window.location.href = `${cliCallback}${separator}token=${encodeURIComponent(token)}`;
+ window.location.href = `${cliCallback}${separator}token=${encodeURIComponent(token)}&state=${encodeURIComponent(cliState)}`;
return;
}
diff --git a/server/cmd/multica/cmd_auth.go b/server/cmd/multica/cmd_auth.go
index 3f8d98efcc..d2d406f2f9 100644
--- a/server/cmd/multica/cmd_auth.go
+++ b/server/cmd/multica/cmd_auth.go
@@ -3,6 +3,8 @@ package main
import (
"bufio"
"context"
+ "crypto/rand"
+ "encoding/hex"
"fmt"
"net"
"net/http"
@@ -103,7 +105,15 @@ func runAuthLoginBrowser(cmd *cobra.Command) error {
port := listener.Addr().(*net.TCPAddr).Port
callbackURL := fmt.Sprintf("http://localhost:%d/callback", port)
- loginURL := fmt.Sprintf("%s/login?cli_callback=%s", appURL, url.QueryEscape(callbackURL))
+
+ // Generate a random state parameter for CSRF protection.
+ stateBytes := make([]byte, 16)
+ if _, err := rand.Read(stateBytes); err != nil {
+ return fmt.Errorf("failed to generate state: %w", err)
+ }
+ state := hex.EncodeToString(stateBytes)
+
+ loginURL := fmt.Sprintf("%s/login?cli_callback=%s&cli_state=%s", appURL, url.QueryEscape(callbackURL), url.QueryEscape(state))
// Channel to receive the JWT from the browser callback.
jwtCh := make(chan string, 1)
@@ -116,6 +126,11 @@ func runAuthLoginBrowser(cmd *cobra.Command) error {
http.Error(w, "missing token", http.StatusBadRequest)
return
}
+ returnedState := r.URL.Query().Get("state")
+ if returnedState != state {
+ http.Error(w, "invalid state parameter", http.StatusBadRequest)
+ return
+ }
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!DOCTYPE html><html><body><h2>Authentication successful!</h2><p>You can close this tab and return to the terminal.</p><script>window.close()</script></body></html>`))
jwtCh <- token