1. /api/render: cap de 512 KiB na entrada (amplificação ~2× medida); decodeJSON agora amarra http.MaxBytesReader ao ResponseWriter (413 real ao cliente em todos os endpoints JSON) 2. Login com rate-limit por IP em memória (5 falhas/15 min → 429, acerto reseta, RemoteAddr puro — X-F-F não é confiável) + log de falha com IP 3. Upload: MaxBytesReader no corpo inteiro (64 MiB + 64 KiB) antes do parse do multipart; quota global de 2 GiB (MaxFilesTotalBytes) em AddFile 4. sanitizeRich neutraliza href/src com javascript:/vbscript:/data: (case/whitespace-insensível); link legítimo preservado 5. Cookie Secure passa a ser configuração explícita (NOTES_SECURE_COOKIE) — X-Forwarded-Proto é spoofável; documentado em .env.example e compose 6. Cookie agora carrega id de sessão opaco (crypto/rand 32B, TTL 30d, revogação no logout e no restart); token mestre só vale via Bearer — furo pego pelo próprio teste novo (token no cookie era aceito) Testes: rate-limit, revogação, canais separados, render 413, upload cap sw v16
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
type categoryInput struct {
|
|
Name string `json:"name"`
|
|
Parent *string `json:"parent"` // ausente = não move; "" = raiz
|
|
}
|
|
|
|
func (s *Server) handleCreateCategory(w http.ResponseWriter, r *http.Request) {
|
|
var in categoryInput
|
|
if err := decodeJSON(w, r, &in, 1<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
parent := ""
|
|
if in.Parent != nil {
|
|
parent = *in.Parent
|
|
}
|
|
c, err := s.st.CreateCategory(in.Name, parent)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, c)
|
|
}
|
|
|
|
func (s *Server) handleUpdateCategory(w http.ResponseWriter, r *http.Request) {
|
|
var in categoryInput
|
|
if err := decodeJSON(w, r, &in, 1<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
c, err := s.st.UpdateCategory(r.PathValue("id"), in.Name, in.Parent)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, c)
|
|
}
|
|
|
|
func (s *Server) handleDeleteCategory(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.st.DeleteCategory(r.PathValue("id")); err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|