Merge origin/main (license, compose, #65 leak guard) into cans work
This commit is contained in:
@@ -172,6 +172,11 @@ func (a *apiServer) adminKeyOK(r *http.Request, key string) bool {
|
||||
|
||||
func (a *apiServer) adminAuth(next http.HandlerFunc, key string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !rateLimitAdmin(r) {
|
||||
log.Printf("admin auth RATE LIMITED: %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
||||
writeRateLimited(w, 60)
|
||||
return
|
||||
}
|
||||
if !a.adminKeyOK(r, key) {
|
||||
log.Printf("admin auth FAILURE: %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
||||
writeErr(w, 401, "unauthorized")
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package api
|
||||
|
||||
// #66: admin key attempts must be rate limited per IP (5/min), constant-time
|
||||
// compared, and failures logged. Hammering bad keys must yield 429s.
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestAdminKeyRateLimited: burst of 5 bad-key attempts allowed (401), the 6th
|
||||
// gets 429, and even the correct key is blocked from that IP until refill.
|
||||
func TestAdminKeyRateLimited(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
h := srv.routes()
|
||||
reqIP := "10.7.7.1:1234"
|
||||
|
||||
var got429, retryAfter bool
|
||||
var lastCode int
|
||||
for i := 0; i < 10; i++ {
|
||||
req := httptest.NewRequest("POST", "/admin/api/settings", nil)
|
||||
req.RemoteAddr = reqIP
|
||||
req.Header.Set("X-Admin-Key", "wrong-key")
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
lastCode = rec.Code
|
||||
if rec.Code == 429 {
|
||||
got429 = true
|
||||
retryAfter = rec.Header().Get("Retry-After") != ""
|
||||
break
|
||||
}
|
||||
}
|
||||
if !got429 {
|
||||
t.Fatalf("expected 429 after hammering bad keys, last status %d", lastCode)
|
||||
}
|
||||
if !retryAfter {
|
||||
t.Error("429 missing Retry-After header")
|
||||
}
|
||||
|
||||
// Correct key from the same IP is also locked out.
|
||||
req := httptest.NewRequest("POST", "/admin/api/settings", nil)
|
||||
req.RemoteAddr = reqIP
|
||||
req.Header.Set("X-Admin-Key", srv.adminKey)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 429 {
|
||||
t.Errorf("correct key after lockout: got %d, want 429", rec.Code)
|
||||
}
|
||||
|
||||
// A different IP is unaffected.
|
||||
req2 := httptest.NewRequest("POST", "/admin/api/settings", strings.NewReader(`{"rate_limit_burst":5,"rate_limit_per_minute":60,"max_content_bytes":1048576,"custom_slug_reservation_days":30,"burn_viewer_window_minutes":15}`))
|
||||
req2.RemoteAddr = "203.0.113.9:1234"
|
||||
req2.Header.Set("X-Admin-Key", srv.adminKey)
|
||||
rec2 := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec2, req2)
|
||||
if rec2.Code != 200 {
|
||||
t.Errorf("correct key from another IP: got %d, want 200", rec2.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminKeyConstantTimeCompare: sanity check that the comparison is
|
||||
// constant-time (uses subtle.ConstantTimeCompare, not ==).
|
||||
func TestAdminKeyConstantTimeCompare(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
r.Header.Set("X-Admin-Key", "test-admin-key")
|
||||
if !srv.adminKeyOK(r, srv.adminKey) {
|
||||
t.Fatal("correct key rejected")
|
||||
}
|
||||
r.Header.Set("X-Admin-Key", "wrong")
|
||||
if srv.adminKeyOK(r, srv.adminKey) {
|
||||
t.Fatal("wrong key accepted")
|
||||
}
|
||||
// differ in length: must not panic/mismatch unexpectedly
|
||||
r.Header.Set("X-Admin-Key", "test-admin-key-longer")
|
||||
if srv.adminKeyOK(r, srv.adminKey) {
|
||||
t.Fatal("longer wrong key accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// #60: the cans API must clamp expires_in at the boundary exactly like the
|
||||
// pastes API — reject zero/negative durations and anything over the 1-year
|
||||
// UI cap, accept the exact boundaries.
|
||||
func TestCreateCanExpiryBounds(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
cases := []struct {
|
||||
expiresIn string
|
||||
wantCode int
|
||||
}{
|
||||
{"-1h", 400}, // negative
|
||||
{"-0s", 400}, // negative zero
|
||||
{"0s", 400}, // zero
|
||||
{"1ns", 400}, // positive but below the 1-minute floor
|
||||
{"59s", 400}, // just under the floor
|
||||
{"1m", 201}, // exactly the floor
|
||||
{"90s", 201}, // just over the floor
|
||||
{"8760h", 201}, // exactly 1 year
|
||||
{"8785h", 400}, // 1 year + 1 day: over the cap
|
||||
{"87600h", 400}, // 10 years, the originally reported case
|
||||
}
|
||||
for _, c := range cases {
|
||||
globalLimiter = newLimiter() // avoid create rate limit between cases
|
||||
body, ct := multipartBody(t, map[string]string{
|
||||
"json_items": `[{"title":"a.txt","content":"AAA"}]`,
|
||||
"expires_in": c.expiresIn,
|
||||
}, "files", "pic.txt", "file data")
|
||||
req := httptest.NewRequest("POST", "/api/pastes/can", body)
|
||||
req.Header.Set("Content-Type", ct)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != c.wantCode {
|
||||
t.Errorf("expires_in %q: got %d want %d (%s)",
|
||||
c.expiresIn, rec.Code, c.wantCode, rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package api
|
||||
|
||||
// Regression tests for #68 input-validation gaps: negative/oversized content
|
||||
// lengths (413), limit=0 → default page size, unchecked query params
|
||||
// (negative offset), and negative burn_after_reads.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createPasteRaw(t *testing.T, h http.Handler, body string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest("POST", "/api/pastes", strings.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
// A request whose decoded content exceeds the admin-tunable cap is rejected
|
||||
// with 413 and a clear message (content under the body cap, over the
|
||||
// content cap).
|
||||
func TestCreatePasteContentOverCap413(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
// content over MaxContentBytes (5MiB) but under body cap (+4KiB): send
|
||||
// just over the content cap so the per-field check fires first.
|
||||
content := strings.Repeat("a", 5*1024*1024+10)
|
||||
rec := createPasteRaw(t, h, fmt.Sprintf(`{"content":"%s"}`, content))
|
||||
if rec.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("got %d want 413: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "content exceeds max") {
|
||||
t.Fatalf("unclear error message: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A request whose entire body is larger than the server-side body cap is cut
|
||||
// off by http.MaxBytesReader and answered with 413, not decoded into memory
|
||||
// (#68: previously a giant body was fully buffered, then rejected only at
|
||||
// the per-field check — actually the decode happened before any check).
|
||||
func TestCreatePasteBodyOverCap413(t *testing.T) {
|
||||
s := testServer(t)
|
||||
// shrink the content cap so the body cap is small too
|
||||
ss := s.settings.get()
|
||||
ss.MaxContentBytes = 64 * 1024
|
||||
if err := s.settings.set(ss); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := s.routes()
|
||||
|
||||
content := strings.Repeat("a", 200*1024) // 200KiB > 64KiB+4KiB body cap
|
||||
rec := createPasteRaw(t, h, fmt.Sprintf(`{"content":"%s","title":"x"}`, content))
|
||||
if rec.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("got %d want 413: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Negative content lengths cannot be expressed via JSON, but a negative
|
||||
// expires-style numeric payload must not crash; more importantly the
|
||||
// burn_after_reads field: negative values are rejected with a clear message.
|
||||
func TestCreatePasteNegativeBurnAfterReads(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
rec := createPasteRaw(t, h, `{"content":"hi","burn_after_reads":-5}`)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("got %d want 400: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "burn_after_reads") {
|
||||
t.Fatalf("unclear error message: %s", rec.Body.String())
|
||||
}
|
||||
|
||||
// 0 and positive values still work (0 = default single read, per #49)
|
||||
rec = createPasteRaw(t, h, `{"content":"hi","burn_after_reads":0,"burn_after_read":true}`)
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("zero burn_after_reads: got %d want 201: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// limit=0 on list endpoints returns the default page size (existing clamp
|
||||
// treats <=0 as default; #68 asks this be explicit and tested).
|
||||
func TestListLimitZeroUsesDefault(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
// seed 3 public pastes
|
||||
for i := 0; i < 3; i++ {
|
||||
rec := createPasteRaw(t, h, fmt.Sprintf(`{"content":"p%d"}`, i))
|
||||
if rec.Code != 201 {
|
||||
t.Fatalf("seed: got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
for _, q := range []string{"/api/public?limit=0", "/api/public"} {
|
||||
req := httptest.NewRequest("GET", q, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%s: got %d", q, rec.Code)
|
||||
}
|
||||
var got struct {
|
||||
Limit int `json:"limit"`
|
||||
Total int `json:"total"`
|
||||
Items []struct{ ID string } `json:"items"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
if got.Limit != 25 || len(got.Items) != 3 {
|
||||
t.Fatalf("%s: limit=%d items=%d, want limit 25 and all 3 items", q, got.Limit, len(got.Items))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Huge limit values are clamped to the max page size (already the behavior;
|
||||
// regression-tested here per #68 "validate unchecked params").
|
||||
func TestListLimitHugeClamped(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/public?limit=999999999", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
var got struct {
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
if got.Limit != 25 {
|
||||
t.Fatalf("limit=%d, want clamped to 25", got.Limit)
|
||||
}
|
||||
}
|
||||
|
||||
// Negative offset previously passed through unchecked to SQL (harmless in
|
||||
// SQLite, but invalid); it must be clamped to 0 (#68).
|
||||
func TestListNegativeOffsetClamped(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
rec := createPasteRaw(t, h, fmt.Sprintf(`{"content":"p%d"}`, i))
|
||||
if rec.Code != 201 {
|
||||
t.Fatalf("seed: got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/public?offset=-999", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("got %d", rec.Code)
|
||||
}
|
||||
var got struct {
|
||||
Offset int `json:"offset"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
if got.Offset != 0 || got.Total != 2 {
|
||||
t.Fatalf("offset=%d total=%d, want offset 0 and total 2", got.Offset, got.Total)
|
||||
}
|
||||
|
||||
// /api/mine too (needs the viewer cookie)
|
||||
req = httptest.NewRequest("GET", "/api/mine?offset=-5", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "vwr", Value: "offclamp"})
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
var mine struct {
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &mine)
|
||||
if mine.Offset != 0 {
|
||||
t.Fatalf("mine offset=%d, want 0", mine.Offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Non-numeric limit/offset fall back to defaults instead of 500s.
|
||||
func TestListGarbageParams(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/public?limit=abc&offset=xyz", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("got %d", rec.Code)
|
||||
}
|
||||
var got struct {
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
if got.Limit != 25 || got.Offset != 0 {
|
||||
t.Fatalf("limit=%d offset=%d, want 25/0", got.Limit, got.Offset)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package api
|
||||
|
||||
// #81: ALL password verification attempts (GET query param, header, POST
|
||||
// form) must go through the per-IP unlock limiter. Regression: N wrong
|
||||
// passwords via GET ?password= must eventually yield 429.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createPasswordPaste(t *testing.T, s *apiServer, pw string) string {
|
||||
t.Helper()
|
||||
h := s.routes()
|
||||
body := `{"content":"secret","password":"` + pw + `"}`
|
||||
req := httptest.NewRequest("POST", "/api/pastes", strings.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
var created struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &created)
|
||||
return created.ID
|
||||
}
|
||||
|
||||
// TestRateLimitGetPasswordQuery: repeated wrong passwords via GET
|
||||
// ?password= must eventually return 429 (unlock limiter: burst 5).
|
||||
func TestRateLimitGetPasswordQuery(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
id := createPasswordPaste(t, s, "hunter2")
|
||||
|
||||
var saw429 bool
|
||||
// more attempts than the unlock burst (5)
|
||||
for i := 0; i < 10; i++ {
|
||||
req := httptest.NewRequest("GET", "/api/pastes/"+id+"?password=wrong"+string(rune('a'+i)), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code == 429 {
|
||||
saw429 = true
|
||||
break
|
||||
}
|
||||
if rec.Code != 401 {
|
||||
t.Fatalf("attempt %d: expected 401 before limit, got %d", i, rec.Code)
|
||||
}
|
||||
}
|
||||
if !saw429 {
|
||||
t.Fatal("expected 429 after repeated wrong ?password= attempts, never got one")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimitGetPasswordHeader: same guarantee for the X-Paste-Password header path.
|
||||
func TestRateLimitGetPasswordHeader(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
id := createPasswordPaste(t, s, "hunter2")
|
||||
|
||||
var saw429 bool
|
||||
for i := 0; i < 10; i++ {
|
||||
req := httptest.NewRequest("GET", "/api/pastes/"+id, nil)
|
||||
req.Header.Set("X-Paste-Password", "wrong"+string(rune('a'+i)))
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code == 429 {
|
||||
saw429 = true
|
||||
break
|
||||
}
|
||||
if rec.Code != 401 {
|
||||
t.Fatalf("attempt %d: expected 401 before limit, got %d", i, rec.Code)
|
||||
}
|
||||
}
|
||||
if !saw429 {
|
||||
t.Fatal("expected 429 after repeated wrong header password attempts, never got one")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimitGetPasswordCorrectStillAllowed: a correct password must still
|
||||
// work within the burst (the limiter gates attempts, not correctness).
|
||||
func TestRateLimitGetPasswordCorrectStillAllowed(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
id := createPasswordPaste(t, s, "hunter2")
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/pastes/"+id+"?password=hunter2", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("expected 200 for correct password within burst, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package api
|
||||
|
||||
// Regression tests for #86: title and language are bounded at create time.
|
||||
// Titles over 200 chars are truncated; language must match
|
||||
// ^[a-zA-Z0-9+#-]{1,40}$ or the create is rejected with a clear 400.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// pasteMeta fetches a created paste's stored metadata via the API.
|
||||
func pasteMeta(t *testing.T, h http.Handler, id string) map[string]any {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/pastes/"+id, nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("get paste %s: got %d: %s", id, rec.Code, rec.Body.String())
|
||||
}
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// A 5000-char title is truncated to 200 characters at create time (#86).
|
||||
func TestCreatePasteTitleTruncated(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
title := strings.Repeat("t", 5000)
|
||||
body, _ := json.Marshal(map[string]any{"content": "hi", "title": title})
|
||||
rec := createPasteRaw(t, h, string(body))
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("got %d want 201: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
meta := pasteMeta(t, h, resp.ID)
|
||||
got, _ := meta["title"].(string)
|
||||
if got != strings.Repeat("t", 200) {
|
||||
t.Fatalf("title not truncated to 200 chars: len=%d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// A title within the 200-char bound is stored verbatim (minus surrounding
|
||||
// whitespace, which is trimmed).
|
||||
func TestCreatePasteTitleWithinBoundKept(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
title := " " + strings.Repeat("x", 200) + " "
|
||||
body, _ := json.Marshal(map[string]any{"content": "hi", "title": title})
|
||||
rec := createPasteRaw(t, h, string(body))
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("got %d want 201: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
meta := pasteMeta(t, h, resp.ID)
|
||||
if got, _ := meta["title"].(string); got != strings.Repeat("x", 200) {
|
||||
t.Fatalf("title changed unexpectedly: len=%d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// A language longer than 40 chars is rejected with a clear 400 (#86).
|
||||
func TestCreatePasteLanguageTooLong400(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
body, _ := json.Marshal(map[string]any{"content": "hi", "language": strings.Repeat("a", 41)})
|
||||
rec := createPasteRaw(t, h, string(body))
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("got %d want 400: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "language") {
|
||||
t.Fatalf("unclear error message: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Language strings outside ^[a-zA-Z0-9+#-]{1,40}$ are rejected with 400.
|
||||
func TestCreatePasteLanguageBadFormat400(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
for _, bad := range []string{
|
||||
"<img src=x onerror=alert(1)>",
|
||||
"java script",
|
||||
"c++ extra!",
|
||||
"py_thon",
|
||||
"go.lang",
|
||||
} {
|
||||
body, _ := json.Marshal(map[string]any{"content": "hi", "language": bad})
|
||||
rec := createPasteRaw(t, h, string(body))
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("language %q: got %d want 400: %s", bad, rec.Code, rec.Body.String())
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "language must match") {
|
||||
t.Errorf("language %q: unclear error: %s", bad, rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Valid languages (letters, digits, #, +, -) within 40 chars are accepted.
|
||||
func TestCreatePasteLanguageValidAccepted(t *testing.T) {
|
||||
for _, ok := range []string{"go", "c#", "f#", "c++", "objective-c", "ECMAScript-2023", strings.Repeat("a", 40)} {
|
||||
s2 := testServer(t) // fresh rate limiter per case
|
||||
h2 := s2.routes()
|
||||
body, _ := json.Marshal(map[string]any{"content": "hi", "language": ok})
|
||||
rec := createPasteRaw(t, h2, string(body))
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Errorf("language %q: got %d want 201: %s", ok, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An absent or blank language still creates fine and stores NULL, and a
|
||||
// blank title is stored NULL rather than an empty string.
|
||||
func TestCreatePasteBlankMetadataOK(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
for _, body := range []string{
|
||||
`{"content":"hi"}`,
|
||||
`{"content":"hi","language":"","title":" "}`,
|
||||
} {
|
||||
rec := createPasteRaw(t, h, body)
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("body %s: got %d want 201: %s", body, rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
meta := pasteMeta(t, h, resp.ID)
|
||||
if lang, ok := meta["language"]; ok && lang != nil && lang != "" {
|
||||
t.Fatalf("body %s: language not null: %v", body, lang)
|
||||
}
|
||||
if title, ok := meta["title"]; ok && title != nil && title != "" {
|
||||
t.Fatalf("body %s: title not null: %v", body, title)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,6 +183,46 @@ func TestListPublicExcludesUnlisted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPublicExcludesPasswordAndUnlisted(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
bodies := []string{
|
||||
`{"content":"open","visibility":"public"}`,
|
||||
`{"content":"locked","visibility":"public","password":"hunter2"}`,
|
||||
`{"content":"hidden","visibility":"unlisted"}`,
|
||||
}
|
||||
for _, body := range bodies {
|
||||
req := httptest.NewRequest("POST", "/api/pastes", strings.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 201 {
|
||||
t.Fatalf("create %s: got %d", body, rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/public", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("list public: got %d", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
Total int `json:"total"`
|
||||
Items []map[string]any `json:"items"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
if resp.Total != 1 || len(resp.Items) != 1 {
|
||||
t.Fatalf("expected only the 1 public paste, got total=%d items=%d", resp.Total, len(resp.Items))
|
||||
}
|
||||
// password-protected and unlisted pastes must not appear (no metadata leak)
|
||||
for _, secret := range []string{"hunter2", "locked", "hidden"} {
|
||||
if strings.Contains(rec.Body.String(), secret) {
|
||||
t.Fatalf("leaked %q in /api/public response", secret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepSoftDeletesAfterGrace(t *testing.T) {
|
||||
s := testServer(t)
|
||||
h := s.routes()
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package api
|
||||
|
||||
// #83 regression tests: `public` boolean in the create payload must map to
|
||||
// visibility (false -> unlisted, true -> public); string `visibility` still works.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createPasteBody(t *testing.T, h *apiServer, body string) map[string]any {
|
||||
req := httptest.NewRequest("POST", "/api/pastes", strings.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
h.routes().ServeHTTP(rec, req)
|
||||
if rec.Code != 201 {
|
||||
t.Fatalf("create: got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("bad json: %v", err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func getVis(t *testing.T, h *apiServer, id string) string {
|
||||
req := httptest.NewRequest("GET", "/api/pastes/"+id, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.routes().ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("get %s: got %d", id, rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
Visibility string `json:"visibility"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("bad json: %v", err)
|
||||
}
|
||||
return resp.Visibility
|
||||
}
|
||||
|
||||
// TestPublicBooleanFalseMapsToUnlisted: {"public": false} must create an unlisted paste.
|
||||
func TestPublicBooleanFalseMapsToUnlisted(t *testing.T) {
|
||||
s := testServer(t)
|
||||
resp := createPasteBody(t, s, `{"content":"x","public":false}`)
|
||||
if v := getVis(t, s, resp["id"].(string)); v != "unlisted" {
|
||||
t.Fatalf("public:false -> got visibility %q, want unlisted", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublicBooleanTrueMapsToPublic: {"public": true} must create a public paste.
|
||||
func TestPublicBooleanTrueMapsToPublic(t *testing.T) {
|
||||
s := testServer(t)
|
||||
resp := createPasteBody(t, s, `{"content":"x","public":true}`)
|
||||
if v := getVis(t, s, resp["id"].(string)); v != "public" {
|
||||
t.Fatalf("public:true -> got visibility %q, want public", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublicBooleanOverridesString: boolean wins when both fields are sent.
|
||||
func TestPublicBooleanOverridesString(t *testing.T) {
|
||||
s := testServer(t)
|
||||
resp := createPasteBody(t, s, `{"content":"x","visibility":"public","public":false}`)
|
||||
if v := getVis(t, s, resp["id"].(string)); v != "unlisted" {
|
||||
t.Fatalf("boolean override -> got %q, want unlisted", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestVisibilityStringStillWorks: existing string contract unchanged.
|
||||
func TestVisibilityStringStillWorks(t *testing.T) {
|
||||
s := testServer(t)
|
||||
resp := createPasteBody(t, s, `{"content":"x","visibility":"unlisted"}`)
|
||||
if v := getVis(t, s, resp["id"].(string)); v != "unlisted" {
|
||||
t.Fatalf("string field -> got %q, want unlisted", v)
|
||||
}
|
||||
resp = createPasteBody(t, s, `{"content":"y","visibility":"public"}`)
|
||||
if v := getVis(t, s, resp["id"].(string)); v != "public" {
|
||||
t.Fatalf("string field -> got %q, want public", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublicListExcludesPublicFalse: {"public":false} pastes stay out of /api/public.
|
||||
func TestPublicListExcludesPublicFalse(t *testing.T) {
|
||||
s := testServer(t)
|
||||
createPasteBody(t, s, `{"content":"hidden","public":false}`)
|
||||
req := httptest.NewRequest("GET", "/api/public", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(rec, req)
|
||||
var resp struct {
|
||||
Total int `json:"total"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
if resp.Total != 0 {
|
||||
t.Fatalf("public:false paste leaked into /api/public: total=%d", resp.Total)
|
||||
}
|
||||
}
|
||||
@@ -48,8 +48,28 @@ func (l *limiter) allow(key string, rate, burst float64) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// clientIP extracts the request IP (no reverse proxy header by default).
|
||||
// clientIP extracts the client IP for rate-limit keying (#85).
|
||||
//
|
||||
// Trust boundary: palette runs behind exactly ONE trusted reverse proxy
|
||||
// (Traefik in the k3s pod network). Traefik APPENDS the real client IP to
|
||||
// X-Forwarded-For, so the RIGHTMOST entry is the last value the trusted
|
||||
// proxy observed and is unspoofable by the client (a client-supplied fake
|
||||
// entry only lands on the LEFT and is ignored). This matches chi's
|
||||
// middleware.RealIP semantics for a single trusted proxy hop.
|
||||
//
|
||||
// Direct connections (no XFF header) fall back to RemoteAddr. Directly
|
||||
// reachable deployments must NOT expose the app to untrusted networks
|
||||
// without a proxy in front, or attackers could forge the rightmost entry.
|
||||
func clientIP(r *http.Request) string {
|
||||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
if i := strings.LastIndex(xff, ","); i >= 0 {
|
||||
return strings.TrimSpace(xff[i+1:])
|
||||
}
|
||||
return strings.TrimSpace(xff)
|
||||
}
|
||||
if xr := r.Header.Get("X-Real-Ip"); xr != "" {
|
||||
return strings.TrimSpace(xr)
|
||||
}
|
||||
host := r.RemoteAddr
|
||||
if i := strings.LastIndex(host, ":"); i > 0 {
|
||||
host = host[:i]
|
||||
@@ -84,6 +104,12 @@ func rateLimitUnlock(id string, r *http.Request) bool {
|
||||
return globalLimiter.allow("unlock:"+id+":"+clientIP(r), 5.0/60.0, 5)
|
||||
}
|
||||
|
||||
// rateLimitAdmin: 5 attempts per minute per IP on the admin key check (#66),
|
||||
// same pattern as the unlock limiter (#34).
|
||||
func rateLimitAdmin(r *http.Request) bool {
|
||||
return globalLimiter.allow("admin:"+clientIP(r), 5.0/60.0, 5)
|
||||
}
|
||||
|
||||
// writeRateLimited responds 429 with Retry-After based on refill rate.
|
||||
func writeRateLimited(w http.ResponseWriter, retryAfterSecs int) {
|
||||
w.Header().Set("Retry-After", strconv.Itoa(retryAfterSecs))
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package api
|
||||
|
||||
// Issue #85: the rate limit key must use the rightmost X-Forwarded-For entry
|
||||
// (appended by the trusted Traefik proxy), never the raw/leftmost header
|
||||
// value a client can forge. A spoofed FIRST XFF entry must not bypass the
|
||||
// limit or rotate buckets.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientIPTakesRightmostXFF(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
r.RemoteAddr = "10.42.0.7:51000" // trusted Traefik pod
|
||||
r.Header.Set("X-Forwarded-For", "1.2.3.4, 1.2.3.5, 203.0.113.9")
|
||||
if got := clientIP(r); got != "203.0.113.9" {
|
||||
t.Fatalf("clientIP = %q, want rightmost 203.0.113.9", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPXRealIPFallback(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
r.RemoteAddr = "10.42.0.7:51000"
|
||||
r.Header.Set("X-Real-Ip", "203.0.113.10")
|
||||
if got := clientIP(r); got != "203.0.113.10" {
|
||||
t.Fatalf("clientIP = %q, want 203.0.113.10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPDirectFallback(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
r.RemoteAddr = "198.51.100.5:51000"
|
||||
if got := clientIP(r); got != "198.51.100.5" {
|
||||
t.Fatalf("clientIP = %q, want 198.51.100.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimitSpoofedFirstXFFDoesNotBypass: an attacker rotating a fake
|
||||
// leftmost XFF entry stays limited on their real (rightmost) IP.
|
||||
func TestRateLimitSpoofedFirstXFFDoesNotBypass(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
h := srv.routes()
|
||||
for i := 0; i < 5; i++ {
|
||||
req := httptest.NewRequest("POST", "/api/pastes", bytes.NewReader([]byte(`{"content":"hi"}`)))
|
||||
req.RemoteAddr = "10.42.0.7:51000"
|
||||
// each request spoofs a DIFFERENT leftmost entry
|
||||
req.Header.Set("X-Forwarded-For", spoofN(i)+", 203.0.113.9")
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if rr.Code != 201 {
|
||||
t.Fatalf("req %d: want 201, got %d", i, rr.Code)
|
||||
}
|
||||
}
|
||||
// 6th request, still the same real IP, new spoofed prefix: must 429
|
||||
req := httptest.NewRequest("POST", "/api/pastes", bytes.NewReader([]byte(`{"content":"hi"}`)))
|
||||
req.RemoteAddr = "10.42.0.7:51000"
|
||||
req.Header.Set("X-Forwarded-For", "9.9.9.9, 203.0.113.9")
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if rr.Code != 429 {
|
||||
t.Fatalf("spoofed 6th req: want 429, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func spoofN(i int) string {
|
||||
return "1.2.3." + string(rune('0'+i))
|
||||
}
|
||||
|
||||
// Distinct real IPs must still get distinct buckets (no over-limiting).
|
||||
func TestRateLimitDistinctRightmostIPsIndependent(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
h := srv.routes()
|
||||
for _, ip := range []string{"203.0.113.20", "203.0.113.21"} {
|
||||
req := httptest.NewRequest("POST", "/api/pastes", bytes.NewReader([]byte(`{"content":"hi"}`)))
|
||||
req.RemoteAddr = "10.42.0.7:51000"
|
||||
req.Header.Set("X-Forwarded-For", "6.6.6.6, "+ip)
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if rr.Code != 201 {
|
||||
t.Fatalf("ip %s: want 201, got %d", ip, rr.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-15
@@ -60,7 +60,9 @@ func (a *apiServer) routes() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.Timeout(30 * time.Second))
|
||||
r.Use(a.limitRequestBody) // #68: hard server-side body cap -> 413
|
||||
r.Use(viewerCookieMiddleware)
|
||||
r.Use(web.SecurityHeaders) // #59: CSP + hardening headers on HTML pages
|
||||
|
||||
// admin (#40): HTML page is open (key entry via form); API is key-guarded
|
||||
r.Get("/admin", a.ui.Handlers().HandleAdminPage)
|
||||
@@ -155,16 +157,43 @@ func (a *apiServer) handleCreatePaste(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
var p store.Paste
|
||||
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
||||
if isBodyTooLarge(err) { // #68: body cut off by MaxBytesReader
|
||||
writeBodyTooLarge(w)
|
||||
return
|
||||
}
|
||||
writeErr(w, 400, "invalid json body")
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(p.Content) == "" {
|
||||
writeErr(w, 400, "content is required")
|
||||
if status, msg := checkContent(p.Content, s.MaxContentBytes); status != 0 {
|
||||
writeErr(w, status, msg)
|
||||
return
|
||||
}
|
||||
if int64(len(p.Content)) > s.MaxContentBytes { // #40: admin-tunable
|
||||
writeErr(w, 413, fmt.Sprintf("content exceeds max %d bytes", s.MaxContentBytes))
|
||||
return
|
||||
// #86: bound free-form metadata at create time
|
||||
if p.Title != nil {
|
||||
t, err := checkTitle(*p.Title)
|
||||
if err != nil {
|
||||
writeErr(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
p.Title = &t
|
||||
}
|
||||
if p.Language != nil {
|
||||
l, err := checkLanguage(*p.Language)
|
||||
if err != nil {
|
||||
writeErr(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
if l == "" {
|
||||
p.Language = nil
|
||||
} else {
|
||||
p.Language = &l
|
||||
}
|
||||
}
|
||||
if p.BurnAfterReads != nil { // #68: reject negative read budgets
|
||||
if err := parseBurnAfterReads(*p.BurnAfterReads); err != nil {
|
||||
writeErr(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
// #40: admin-configurable default expiry
|
||||
if (p.ExpiresIn == nil || *p.ExpiresIn == "") && s.DefaultExpiry != "" {
|
||||
@@ -209,6 +238,13 @@ func (a *apiServer) handleGetPaste(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if row.PasswordHash.Valid {
|
||||
// #81: every password verification (header, query param, or empty)
|
||||
// goes through the same per-IP+paste unlock limiter as the POST form
|
||||
// path, so brute-force via GET ?password= or X-Paste-Password gets 429.
|
||||
if !rateLimitUnlock(row.ID, r) {
|
||||
writeRateLimited(w, 60)
|
||||
return
|
||||
}
|
||||
// require password via header or query
|
||||
pw := r.Header.Get("X-Paste-Password")
|
||||
if pw == "" {
|
||||
@@ -293,11 +329,8 @@ func (a *apiServer) handleListMine(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"total": 0, "items": []any{}})
|
||||
return
|
||||
}
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 50
|
||||
}
|
||||
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||
limit := parseLimit(r, 50, 100)
|
||||
offset := parseOffset(r)
|
||||
rows, total, err := a.store.ListMine(vid, limit, offset)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "db error")
|
||||
@@ -317,11 +350,8 @@ func (a *apiServer) handleListMine(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (a *apiServer) handleListPublic(w http.ResponseWriter, r *http.Request) {
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 25
|
||||
}
|
||||
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||
limit := parseLimit(r, 25, 100)
|
||||
offset := parseOffset(r)
|
||||
rows, total, err := a.store.ListPublic(limit, offset)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "db error")
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// #68 input-validation helpers. Paste/can payloads are size-capped and list
|
||||
// endpoints get a single place where limit/offset are parsed and clamped.
|
||||
|
||||
// maxRequestBody returns the HTTP body cap for JSON create endpoints: the
|
||||
// admin-tunable content cap plus headroom for JSON field overhead, floored
|
||||
// at 64KiB so a tiny admin-configured cap can't break small requests.
|
||||
func (a *apiServer) maxRequestBody() int64 {
|
||||
s := a.settings.get()
|
||||
max := s.MaxContentBytes + 4096
|
||||
if max < 64*1024 {
|
||||
max = 64 * 1024
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
// limitRequestBody wraps the request body with http.MaxBytesReader so
|
||||
// oversized payloads are cut off server-side instead of being fully decoded
|
||||
// into memory before the per-field size check runs (#68). A read over the
|
||||
// cap surfaces as *http.MaxBytesError, which handlers map to 413.
|
||||
func (a *apiServer) limitRequestBody(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body != nil {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, a.maxRequestBody())
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// writeBodyTooLarge emits the 413 response for a body rejected by
|
||||
// MaxBytesReader.
|
||||
func writeBodyTooLarge(w http.ResponseWriter) {
|
||||
writeErr(w, http.StatusRequestEntityTooLarge, "request body too large")
|
||||
}
|
||||
|
||||
// isBodyTooLarge reports whether err came from http.MaxBytesReader.
|
||||
func isBodyTooLarge(err error) bool {
|
||||
var mbe *http.MaxBytesError
|
||||
return errors.As(err, &mbe)
|
||||
}
|
||||
|
||||
// checkContent validates paste content: rejects whitespace-only content
|
||||
// (400) and content over the byte cap (413). Returns (0, "") when valid.
|
||||
func checkContent(content string, maxBytes int64) (int, string) {
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return http.StatusBadRequest, "content is required"
|
||||
}
|
||||
if int64(len(content)) > maxBytes { // #40/#68: admin-tunable cap
|
||||
return http.StatusRequestEntityTooLarge,
|
||||
fmt.Sprintf("content exceeds max %d bytes", maxBytes)
|
||||
}
|
||||
return 0, ""
|
||||
}
|
||||
|
||||
// parseLimit clamps the ?limit query param: missing/non-numeric/zero/negative
|
||||
// or over-max values fall back to def. Zero intentionally maps to the default
|
||||
// page size, matching the pre-existing `<= 0` clamp (#68).
|
||||
func parseLimit(r *http.Request, def, max int) int {
|
||||
n, err := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if err != nil || n <= 0 || n > max {
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// parseOffset clamps the ?offset query param: missing/non-numeric or negative
|
||||
// values become 0 (#68: negative offsets previously passed through to SQL).
|
||||
func parseOffset(r *http.Request) int {
|
||||
n, err := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||
if err != nil || n < 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// parseBurnAfterReads validates the burn_after_reads field (#68): negative
|
||||
// values are rejected; zero/absent mean the default single read.
|
||||
func parseBurnAfterReads(n int) error {
|
||||
if n < 0 {
|
||||
return errors.New("burn_after_reads must be a positive number")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// #86: bounds for free-form metadata fields on create.
|
||||
const (
|
||||
maxTitleLen = 200
|
||||
maxLanguageLen = 40
|
||||
)
|
||||
|
||||
// languageRe restricts language to identifiers like go, c#, f#, c++, objc.
|
||||
var languageRe = regexp.MustCompile(`^[a-zA-Z0-9+#-]{1,40}$`)
|
||||
|
||||
// checkTitle validates the paste title (#86): over-max titles are truncated
|
||||
// to 200 characters so a bloated listing entry can't be stored; whitespace
|
||||
// is trimmed first.
|
||||
func checkTitle(title string) (string, error) {
|
||||
title = strings.TrimSpace(title)
|
||||
if len(title) > maxTitleLen {
|
||||
return truncateRunes(title, maxTitleLen), nil
|
||||
}
|
||||
return title, nil
|
||||
}
|
||||
|
||||
// checkLanguage validates the language field (#86): optional, max 40 chars,
|
||||
// and must match ^[a-zA-Z0-9+#-]{1,40}$. Returns "" for absent/blank values.
|
||||
// Anything else malformed is a 400.
|
||||
func checkLanguage(lang string) (string, error) {
|
||||
lang = strings.TrimSpace(lang)
|
||||
if lang == "" {
|
||||
return "", nil
|
||||
}
|
||||
if len(lang) > maxLanguageLen || !languageRe.MatchString(lang) {
|
||||
return "", fmt.Errorf("language must match ^[a-zA-Z0-9+#-]{1,40}$ (max %d chars)", maxLanguageLen)
|
||||
}
|
||||
return lang, nil
|
||||
}
|
||||
|
||||
// truncateRunes cuts s to at most max runes, keeping the prefix intact.
|
||||
func truncateRunes(s string, max int) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= max {
|
||||
return s
|
||||
}
|
||||
return string(runes[:max])
|
||||
}
|
||||
Reference in New Issue
Block a user