agent_paths_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package auth
  2. import (
  3. "net/http/httptest"
  4. "testing"
  5. )
  6. // Regresión #1462: el agente sube el backup de config a
  7. // /api/agents/{slug}/firmware-backup con su Bearer. Si la ruta no está en la
  8. // allowlist de rutas de agente, RequireAuth la rechaza con 401 antes de que
  9. // el handler valide el token (el upload del flujo de firmware #1414 fallaba
  10. // en silencio por esto).
  11. func TestIsAgentReportPathFirmwareBackup(t *testing.T) {
  12. cases := []string{
  13. "/api/agents/rt4/firmware-backup",
  14. "/api/agents/rt4/firmware-progress",
  15. "/api/agents/rt4/firmware-result",
  16. "/api/agents/rt4/upgrade-progress",
  17. "/api/agents/rt4/binary",
  18. }
  19. for _, p := range cases {
  20. if !isAgentReportPath(p) {
  21. t.Errorf("%s debería ser ruta de agente", p)
  22. }
  23. if !IsAgentRequest(httptest.NewRequest("POST", p, nil)) {
  24. t.Errorf("%s debería pasar el gate IsAgentRequest", p)
  25. }
  26. }
  27. // Rutas de admin con el mismo prefijo NO deben colarse como agente.
  28. if isAgentReportPath("/api/agents") {
  29. t.Error("/api/agents no es una ruta de reporte")
  30. }
  31. }
  32. // Regresión #1751: el instalador se sirve desde el propio servidor y lo baja
  33. // curl en el router antes de tener sesión. Si la ruta no está en la allowlist,
  34. // RequireAuth responde 401 y el alta de agente no se puede completar.
  35. func TestInstallAgentScriptIsAnonymous(t *testing.T) {
  36. p := "/api/agents/install-agent.sh"
  37. if !isAnonymousPath(p) {
  38. t.Errorf("%s debería ser anónima", p)
  39. }
  40. if !IsAgentRequest(httptest.NewRequest("GET", p, nil)) {
  41. t.Errorf("%s debería pasar el gate IsAgentRequest", p)
  42. }
  43. }