Files
notas-online/internal/api/revisions.go
T
lgcosta 6e0ec29888 fix(security): corrige achados da varredura — render, brute-force, upload, WYSIWYG, cookie, sessões
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
2026-11-05 18:58:10 -03:00

115 lines
2.8 KiB
Go

package api
import (
"net/http"
"regexp"
"slices"
"github.com/lgcosta/notas-online/internal/diffx"
"github.com/lgcosta/notas-online/internal/store"
)
var hashRe = regexp.MustCompile(`^[0-9a-f]{64}$`)
func (s *Server) handleListRevisions(w http.ResponseWriter, r *http.Request) {
revs, err := s.st.History(r.PathValue("id"))
if err != nil {
mapStoreError(w, err)
return
}
// Mais recente primeiro: ordem natural para um painel de histórico.
slices.Reverse(revs)
if revs == nil {
revs = []store.Revision{}
}
writeJSON(w, http.StatusOK, revs)
}
func (s *Server) handleGetRevision(w http.ResponseWriter, r *http.Request) {
hash := r.PathValue("hash")
if !hashRe.MatchString(hash) {
writeError(w, http.StatusNotFound, "revisão não encontrada")
return
}
rc, err := s.st.RevisionWithContent(r.PathValue("id"), hash)
if err != nil {
mapStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, rc)
}
type diffView struct {
From string `json:"from"`
To string `json:"to"`
Author string `json:"author"`
When string `json:"when"`
Message string `json:"message"`
Diff string `json:"diff"`
Added int `json:"added"`
Removed int `json:"removed"`
}
func (s *Server) handleRevisionDiff(w http.ResponseWriter, r *http.Request) {
hash := r.PathValue("hash")
if !hashRe.MatchString(hash) {
writeError(w, http.StatusNotFound, "revisão não encontrada")
return
}
before, after, rev, err := s.st.DiffBetween(r.PathValue("id"), hash)
if err != nil {
mapStoreError(w, err)
return
}
from := "criação"
if rev.Parent != "" {
from = short(rev.Parent)
}
patch := diffx.Unified(from, short(hash), before, after)
added, removed := diffx.Stats(patch)
writeJSON(w, http.StatusOK, diffView{
From: from, To: short(hash), Author: rev.Author,
When: rev.When.Format("2006-01-02 15:04:05 MST"), Message: rev.Message,
Diff: patch, Added: added, Removed: removed,
})
}
type restoreInput struct {
Author string `json:"author"`
Session string `json:"session"`
}
func (s *Server) handleRestore(w http.ResponseWriter, r *http.Request) {
hash := r.PathValue("hash")
if !hashRe.MatchString(hash) {
writeError(w, http.StatusNotFound, "revisão não encontrada")
return
}
var in restoreInput
if r.ContentLength > 0 {
if err := decodeJSON(w, r, &in, 1<<16); err != nil {
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
return
}
}
n, err := s.st.Restore(r.PathValue("id"), hash, in.Author)
if err != nil {
mapStoreError(w, err)
return
}
content, _, err := s.st.Content(n.ID)
if err != nil {
mapStoreError(w, err)
return
}
s.publishSaved(n, in.Author, in.Session, false)
writeJSON(w, http.StatusOK, newNoteView(n, content))
}
func short(hash string) string {
if len(hash) > 8 {
return hash[:8]
}
return hash
}