device_actions_internal_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // device_actions_internal_test.go — unitarias de los helpers de sanitización
  2. // uci de device_actions.go (#693): escapado de comillas simples y validación
  3. // de hostname DNS.
  4. package httpapi
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. func TestUciQuoteEscapesSingleQuote(t *testing.T) {
  10. cases := []struct{ in, want string }{
  11. {"tv-salon", "tv-salon"},
  12. {"", ""},
  13. {"a'b", `a'\''b`},
  14. {`x'; reboot; echo '`, `x'\''; reboot; echo '\''`},
  15. {"it''s", `it'\'''\''s`},
  16. }
  17. for _, c := range cases {
  18. if got := uciQuote(c.in); got != c.want {
  19. t.Errorf("uciQuote(%q) = %q, want %q", c.in, got, c.want)
  20. }
  21. }
  22. }
  23. func TestUciSetCmdQuotesValue(t *testing.T) {
  24. got := uciSetCmd("dhcp", "np_host_x", "name", "a'b")
  25. want := `uci set dhcp.np_host_x.name='a'\''b'`
  26. if got != want {
  27. t.Errorf("uciSetCmd = %q, want %q", got, want)
  28. }
  29. // El payload del issue queda inerte: cada comilla simple del valor viaja
  30. // como secuencia de escape, nunca como cierre del quoting.
  31. raw := uciSetCmd("firewall", "np_block_x", "name", `x'; id; echo '`)
  32. wantRaw := `uci set firewall.np_block_x.name='x'\''; id; echo '\'''`
  33. if raw != wantRaw {
  34. t.Errorf("uciSetCmd con payload hostil = %q, want %q", raw, wantRaw)
  35. }
  36. }
  37. func TestValidDHCPHostname(t *testing.T) {
  38. valid := []string{
  39. "", // vacío = no tocar el name
  40. "tv-salon", // clásico
  41. "a", // mínimo
  42. "nas.lan", // multi-etiqueta
  43. "n-a-s.l-a-n", // etiquetas con guiones internos
  44. strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 61), // 253 justo
  45. }
  46. for _, h := range valid {
  47. if !validDHCPHostname(h) {
  48. t.Errorf("validDHCPHostname(%q) = false, want true", h)
  49. }
  50. }
  51. invalid := []string{
  52. "x'; touch /tmp/poc; echo '", // PoC del issue #693
  53. "tv salon", // espacio
  54. "x$(id)", // sustitución
  55. "x`id`", // backticks
  56. "pc_1", // underscore (no es DNS)
  57. "-lead", // guion inicial
  58. "trail-", // guion final
  59. "a..b", // etiqueta vacía
  60. ".a", // empieza en punto
  61. "télé", // acento
  62. strings.Repeat("a", 254), // demasiado largo
  63. }
  64. for _, h := range invalid {
  65. if validDHCPHostname(h) {
  66. t.Errorf("validDHCPHostname(%q) = true, want false", h)
  67. }
  68. }
  69. }