Exclude password-protected pastes from public listings (#65)
ListPublic and its COUNT query now filter password_hash IS NULL, so /api/public (and any page backed by it) no longer leaks metadata (title, slug, existence) of password-protected pastes. Unlisted pastes were already excluded. Adds regression test covering both.
This commit is contained in:
@@ -175,6 +175,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()
|
||||
|
||||
Reference in New Issue
Block a user