firmware_upload_internal_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package httpapi
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/gnacho/netpulse/server-go/internal/db"
  9. )
  10. func TestValidFirmwareID(t *testing.T) {
  11. valid := []string{"rt1", "router-1", "a.b", "file_1"}
  12. for _, id := range valid {
  13. if !validFirmwareID(id) {
  14. t.Errorf("validFirmwareID(%q) = false", id)
  15. }
  16. }
  17. invalid := []string{"", ".", "..", "../x", `..\x`, "a/b", `a\b`, "v1..2", "/abs"}
  18. for _, id := range invalid {
  19. if validFirmwareID(id) {
  20. t.Errorf("validFirmwareID(%q) = true", id)
  21. }
  22. }
  23. }
  24. func TestFirmwareUploadPathStaysInsideDir(t *testing.T) {
  25. dir := t.TempDir()
  26. path, ok := firmwareUploadPath(dir, "rt1")
  27. if !ok || path != filepath.Join(dir, "rt1.bin") {
  28. t.Fatalf("firmwareUploadPath valid = (%q, %v)", path, ok)
  29. }
  30. for _, id := range []string{"../escape", `..\escape`, "..", "/etc/passwd"} {
  31. if path, ok := firmwareUploadPath(dir, id); ok {
  32. t.Errorf("firmwareUploadPath(%q) = (%q, true)", id, path)
  33. }
  34. }
  35. }
  36. func newExecutorTokenServer(t *testing.T) (*server, string) {
  37. t.Helper()
  38. d, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
  39. if err != nil {
  40. t.Fatalf("db.Open: %v", err)
  41. }
  42. t.Cleanup(func() { _ = d.Close() })
  43. token := "agent-secret"
  44. if _, err := d.Exec(
  45. `INSERT INTO kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value`,
  46. agentTokenKey("rt1"), hashAgentToken(token),
  47. ); err != nil {
  48. t.Fatalf("seed agent token: %v", err)
  49. }
  50. return &server{db: d}, token
  51. }
  52. func executorTokenRequest(token, body string) *http.Request {
  53. r := httptest.NewRequest(http.MethodPost, "/api/agents/executor-token", strings.NewReader(body))
  54. r.Header.Set("X-Router-ID", "rt1")
  55. r.Header.Set("Authorization", "Bearer "+token)
  56. return r
  57. }
  58. func TestHandleAgentExecutorTokenLimits(t *testing.T) {
  59. s, agentToken := newExecutorTokenServer(t)
  60. for _, tc := range []struct {
  61. name string
  62. body string
  63. want int
  64. }{
  65. {"valid", `{"token":"exec-token"}`, http.StatusOK},
  66. {"empty", `{"token":""}`, http.StatusBadRequest},
  67. {"too long", `{"token":"` + strings.Repeat("a", 513) + `"}`, http.StatusBadRequest},
  68. {"body too large", `{"token":"` + strings.Repeat("a", maxBodyBytes+1) + `"}`, http.StatusRequestEntityTooLarge},
  69. } {
  70. t.Run(tc.name, func(t *testing.T) {
  71. rec := httptest.NewRecorder()
  72. s.handleAgentExecutorToken(rec, executorTokenRequest(agentToken, tc.body))
  73. if rec.Code != tc.want {
  74. t.Fatalf("got %d want %d (body %s)", rec.Code, tc.want, rec.Body.String())
  75. }
  76. })
  77. }
  78. var stored string
  79. if err := s.db.QueryRow(`SELECT value FROM kv WHERE key = ?`, "netgrip.executor_token.rt1").Scan(&stored); err != nil {
  80. t.Fatalf("read stored token: %v", err)
  81. }
  82. if stored != "exec-token" {
  83. t.Fatalf("stored token = %q", stored)
  84. }
  85. }
  86. func TestHandleAgentExecutorTokenRejectsBadAuth(t *testing.T) {
  87. s, _ := newExecutorTokenServer(t)
  88. rec := httptest.NewRecorder()
  89. s.handleAgentExecutorToken(rec, executorTokenRequest("wrong", `{"token":"exec-token"}`))
  90. if rec.Code != http.StatusUnauthorized {
  91. t.Fatalf("got %d want 401", rec.Code)
  92. }
  93. }