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:
@@ -14,6 +14,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -110,6 +111,7 @@ func (i *Importer) Import(ctx context.Context, root, slug string) error {
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
cmd := exec.CommandContext(ctx, python, "-I", filepath.Join(root, "lib", "leetcode.py"), root, slug)
|
||||
cmd.Env = scriptEnv()
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
for _, line := range strings.Split(stderr.String(), "\n") {
|
||||
@@ -121,3 +123,22 @@ func (i *Importer) Import(ctx context.Context, root, slug string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// scriptEnv is the environment the importer runs with: what python3 needs
|
||||
// to reach LeetCode over HTTPS, and nothing else. The script comes from the
|
||||
// clone, so whoever can push to the repo writes it, while this process holds
|
||||
// the Claude, Gitea and S3 secrets in its environment. Those must not be
|
||||
// inherited.
|
||||
func scriptEnv() []string {
|
||||
env := []string{"HOME=" + os.TempDir(), "LANG=C.UTF-8"}
|
||||
if _, ok := os.LookupEnv("PATH"); !ok {
|
||||
env = append(env, "PATH=/usr/local/bin:/usr/bin:/bin")
|
||||
}
|
||||
for _, k := range []string{"PATH", "TMPDIR", "SSL_CERT_FILE", "SSL_CERT_DIR",
|
||||
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "no_proxy"} {
|
||||
if v, ok := os.LookupEnv(k); ok {
|
||||
env = append(env, k+"="+v)
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
@@ -72,3 +72,19 @@ func TestImportError(t *testing.T) {
|
||||
t.Fatalf("Import = %v, want the script's message", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The script is the clone's, so it must not see the server's secrets.
|
||||
func TestImportHidesSecrets(t *testing.T) {
|
||||
if _, err := exec.LookPath("python3"); err != nil {
|
||||
t.Skip("no python3")
|
||||
}
|
||||
t.Setenv("CLAUDE_API_KEY", "sk-secret")
|
||||
t.Setenv("GIT_TOKEN", "gitea-secret")
|
||||
root := t.TempDir()
|
||||
os.MkdirAll(filepath.Join(root, "lib"), 0o755)
|
||||
script := "import os, sys\nleaked = [k for k in ('CLAUDE_API_KEY', 'GIT_TOKEN') if k in os.environ]\nprint('Error: leaked ' + ','.join(leaked) if leaked else 'Error: clean', file=sys.stderr)\nsys.exit(1)\n"
|
||||
os.WriteFile(filepath.Join(root, "lib", "leetcode.py"), []byte(script), 0o644)
|
||||
if err := (&Importer{}).Import(context.Background(), root, "x"); err == nil || err.Error() != "clean" {
|
||||
t.Fatalf("Import = %v, want the script to see no secrets", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user