Keep site jobs across restarts, meter Claude spend, harden admin routes
- internal/jobs replaces explain.Jobs: one registry for every kind of background job, with records in DATA_DIR/jobs. A job's draft is written before its commit, so a failed commit can be saved again (POST /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without paying Claude twice. - Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for running ones while still serving the pages that poll them. - internal/usage records every Claude call's tokens and estimated cost in DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a month reaches it. The stats page shows the admin the spend. - auth.SameOrigin refuses cross-origin writes to admin routes. - The LeetCode importer runs the clone's script without the server's secrets in its environment. - A sync that leaves HEAD where it was skips the re-index. - docs_test.go checks README's route table against the routes, and the CLI page against the root README's commands. - The repo is going private: Gitea links on the site are admin-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SameOrigin refuses a state-changing request that a browser made from
|
||||
// another origin: cross-site request forgery.
|
||||
//
|
||||
// The admin is recognized by cookies alone, and the site's cookies are
|
||||
// SameSite=Lax, which keeps them off another site's POST. That is the main
|
||||
// defence; this is the second. Lax still sends them from any other origin of
|
||||
// the same site (another subdomain of adam-french.co.uk), and the JSON
|
||||
// handlers would decode a text/plain form body too, so the browser's own
|
||||
// word on where a request came from is checked as well.
|
||||
//
|
||||
// Sec-Fetch-Site is set by every current browser and cannot be set by a
|
||||
// page. An older browser sends Origin on a cross-origin POST instead, which
|
||||
// must name this host (nginx passes the browser's Host through). A request
|
||||
// with neither is not from a browser (curl, the tests), and so carries no
|
||||
// one's cookies but its sender's.
|
||||
//
|
||||
// Same-origin pages are not told apart: Gitea, at /gitea/ on the same host,
|
||||
// is trusted like this site's own pages.
|
||||
func SameOrigin() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
r := ctx.Request
|
||||
switch r.Method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
if site := r.Header.Get("Sec-Fetch-Site"); site != "" {
|
||||
// "none" is a request the user made themselves (the address
|
||||
// bar), which a page cannot forge.
|
||||
if site != "same-origin" && site != "none" {
|
||||
refuse(ctx)
|
||||
return
|
||||
}
|
||||
} else if origin := r.Header.Get("Origin"); origin != "" {
|
||||
if u, err := url.Parse(origin); err != nil || u.Host != r.Host {
|
||||
refuse(ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// refuse is a 403, not the 401 that sends the page to the login: logging in
|
||||
// would not help.
|
||||
func refuse(ctx *gin.Context) {
|
||||
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "cross-origin request refused"})
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestSameOrigin(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(SameOrigin())
|
||||
r.Any("/x", func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
|
||||
|
||||
cases := []struct {
|
||||
name, method string
|
||||
headers map[string]string
|
||||
want int
|
||||
}{
|
||||
{"same-origin fetch", "POST", map[string]string{"Sec-Fetch-Site": "same-origin"}, 200},
|
||||
{"typed in", "POST", map[string]string{"Sec-Fetch-Site": "none"}, 200},
|
||||
{"sibling subdomain", "POST", map[string]string{"Sec-Fetch-Site": "same-site"}, 403},
|
||||
{"another site", "PUT", map[string]string{"Sec-Fetch-Site": "cross-site", "Origin": "https://example.com"}, 403},
|
||||
{"cross-site GET", "GET", map[string]string{"Sec-Fetch-Site": "cross-site"}, 200},
|
||||
{"old browser, same host", "POST", map[string]string{"Origin": "https://adam-french.co.uk"}, 200},
|
||||
{"old browser, other host", "DELETE", map[string]string{"Origin": "https://evil.adam-french.co.uk"}, 403},
|
||||
{"not a browser", "POST", nil, 200},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
req := httptest.NewRequest(tc.method, "/x", nil)
|
||||
req.Host = "adam-french.co.uk"
|
||||
for k, v := range tc.headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
if rec.Code != tc.want {
|
||||
t.Errorf("%s: %d, want %d", tc.name, rec.Code, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user