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
This commit is contained in:
1 parent
1b9ce995ba
commit
6e0ec29888
20 files changed
+427
-53
No files matched your search
@@ -90,17 +90,23 @@ func TestAuthRequiredAndLoginFlow(t *testing.T) {
|
||||
t.Fatalf("login errado: %s", resp.Status)
|
||||
}
|
||||
|
||||
// Login correto: cookie HttpOnly emitido.
|
||||
// Login correto: cookie HttpOnly com ID DE SESSÃO opaco (nunca o token mestre).
|
||||
resp = do(t, srv, "POST", "/api/login", `{"token":"`+testToken+`"}`, "", "")
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("login: %s", resp.Status)
|
||||
}
|
||||
sc := resp.Header.Get("Set-Cookie")
|
||||
if !strings.Contains(sc, "notas_token="+testToken) || !strings.Contains(sc, "HttpOnly") ||
|
||||
if !strings.HasPrefix(sc, "notas_token=") || !strings.Contains(sc, "HttpOnly") ||
|
||||
strings.Contains(sc, "Max-Age=0") {
|
||||
t.Errorf("Set-Cookie inadequado: %q", sc)
|
||||
}
|
||||
cookieValue := strings.SplitN(strings.TrimPrefix(sc, "notas_token="), ";", 2)[0]
|
||||
if cookieValue == testToken || strings.Contains(sc, testToken) {
|
||||
t.Errorf("cookie não deveria carregar o token mestre: %q", sc)
|
||||
}
|
||||
if len(cookieValue) != 64 {
|
||||
t.Errorf("id de sessão deveria ter 64 hex chars (32 bytes), tem %d", len(cookieValue))
|
||||
}
|
||||
|
||||
// /api/me com o cookie.
|
||||
resp = do(t, srv, "GET", "/api/me", "", "", "notas_token="+cookieValue)
|
||||
@@ -472,3 +478,72 @@ func TestInvalidPayloads(t *testing.T) {
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
func TestLoginRateLimitPerIP(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
for i := 0; i < 5; i++ {
|
||||
resp := do(t, srv, "POST", "/api/login", `{"token":"errada"}`, "", "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("tentativa %d: esperava 401, obtive %s", i+1, resp.Status)
|
||||
}
|
||||
}
|
||||
// 6ª tentativa errada: bloqueada por IP
|
||||
resp := do(t, srv, "POST", "/api/login", `{"token":"errada"}`, "", "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusTooManyRequests {
|
||||
t.Fatalf("esperava 429 após 5 falhas, obtive %s", resp.Status)
|
||||
}
|
||||
// bloqueio vale até para o token CORRETO (o IP está penalizado)
|
||||
resp = do(t, srv, "POST", "/api/login", `{"token":"`+testToken+`"}`, "", "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusTooManyRequests {
|
||||
t.Fatalf("esperava 429 mesmo com token correto, obtive %s", resp.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionRevokedOnLogoutAndBadSessionRejected(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
resp := do(t, srv, "POST", "/api/login", `{"token":"`+testToken+`"}`, "", "")
|
||||
sc := resp.Header.Get("Set-Cookie")
|
||||
resp.Body.Close()
|
||||
sid := strings.SplitN(strings.TrimPrefix(sc, "notas_token="), ";", 2)[0]
|
||||
|
||||
// sessão válida autentica
|
||||
resp = do(t, srv, "GET", "/api/me", "", "", "notas_token="+sid)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("sessão válida: %s", resp.Status)
|
||||
}
|
||||
// token mestre cru NO cookie não vale mais (cookie = só id de sessão)
|
||||
resp = do(t, srv, "GET", "/api/me", "", "", "notas_token="+testToken)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("token mestre no cookie deveria ser rejeitado, obtive %s", resp.Status)
|
||||
}
|
||||
// token mestre via Bearer continua valendo (scripts/CLI)
|
||||
resp = do(t, srv, "GET", "/api/me", "", testToken, "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("bearer com token mestre: %s", resp.Status)
|
||||
}
|
||||
// logout revoga a sessão
|
||||
resp = do(t, srv, "POST", "/api/logout", "", "", "notas_token="+sid)
|
||||
resp.Body.Close()
|
||||
resp = do(t, srv, "GET", "/api/me", "", "", "notas_token="+sid)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("sessão após logout deveria estar revogada, obtive %s", resp.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderRejectsOversizeInput(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
big := strings.Repeat("x", 512<<10+1)
|
||||
body := `{"content":"` + big + `","format":"md"}`
|
||||
resp := do(t, srv, "POST", "/api/render", body, testToken, "")
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("esperava 413 em render >512KiB, obtive %s", resp.Status)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user