[PR #6642] [MERGED] fix(amneziawg): bound S1-S3 by the receive buffer, reject overlapping H #6393

Closed
opened 2026-11-02 04:08:26 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6642
Author: @rudenko-ks
Created: 9/24/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: fix/amneziawg-obfuscation-bounds


📝 Commits (4)

  • 44d9c1c fix(amneziawg): bound S1-S3 by the receive buffer, reject overlapping H
  • 24147de Merge branch 'main' into fix/amneziawg-obfuscation-bounds
  • a5bce06 Merge remote-tracking branch 'origin/main' into pr-6642
  • 7f38f51 fix(amneziawg): bound only inbound padding by the iOS receive buffer

📊 Changes

12 files changed (+189 additions, -35 deletions)

View changed files

📝 docs/content/docs/en/config/amneziawg.mdx (+3 -3)
📝 frontend/src/pages/inbounds/form/protocols/amneziawg.tsx (+3 -3)
📝 frontend/src/pages/xray/outbounds/protocols/amneziawg.tsx (+3 -3)
📝 frontend/src/schemas/protocols/inbound/amneziawg.ts (+3 -3)
📝 frontend/src/schemas/protocols/outbound/amneziawg.ts (+4 -3)
📝 frontend/src/test/amneziawg-schema-cleared.test.ts (+22 -9)
📝 internal/amneziawg/outbound_test.go (+22 -0)
📝 internal/amneziawg/params.go (+53 -6)
📝 internal/amneziawg/params_test.go (+63 -4)
📝 internal/amneziawgnet/device_test.go (+11 -0)
📝 internal/web/service/inbound_amneziawg.go (+1 -1)
📝 internal/web/service/inbound_amneziawg_test.go (+1 -0)

📄 Description

Summary

The native AmneziaWG validator (internal/amneziawg/params.go), both Zod schemas, both forms and the docs
disagreed with amneziawg-go in three places. This PR makes them follow what the engine and its clients
actually enforce.

  1. S3 was capped at 64 with no engine reason. Real configs could not be saved. Example: Amnezia's own
    Amnezia Premium ships s3=1045 (s1=284 s2=659 s4=12, read from a live tunnel over UAPI).
  2. S1/S2 allowed up to 65535. An iOS peer cannot receive a handshake that large.
  3. Overlapping H1–H4 ranges were accepted. amneziawg-go and the kernel module then refuse the whole
    device, so the inbound saved fine and never came up.

The docs also stated two things the engine does not do. They said H 1–4 "must not be used". They said
"amneziawg-go rejects" S1 + 56 = S2.

1–2. S1–S3: the real bound is the smallest receive buffer

  • Handshake messages are sent as padding + size, where the sizes are MessageInitiationSize = 148,
    MessageResponseSize = 92 and MessageCookieReplySize = 64:
    device/send.go,
    device/noise-protocol.go.
  • The receiver reads every datagram into a [MaxMessageSize]byte buffer, where MaxMessageSize = MaxSegmentSize:
    device/pools.go,
    device/constants.go.
  • MaxSegmentSize differs by platform:
  • For every client to receive every handshake message:
    • S1 ≤ 1552
    • S2 ≤ 1608
    • S3 ≤ 1636
  • RandomTrailers does not change these bounds. It only tops a packet up to DefaultUdpWindow = 500 bytes
    (randomTrailer() in send.go), so it never grows a message that is already larger.
  • Where 64 came from: #6105 ported the validator from coinman-dev/3ax-ui in the kernel-module era, and
    gives no reason for it. S4 ≤ 32 does have a reason (transport packets ride at full MTU) and is unchanged.
  • Behaviour change: a saved inbound with S1 > 1552 or S2 > 1608 now fails validation on the next edit.
    Such a config never worked for iOS clients. Linux/Android peers could still receive it.
  • Side effect worth knowing: 1552 + 56 = 1608, so the two maxima together trip the panel's own
    S1 + 56 ≠ S2 rule. Each maximum is valid on its own.

3. H1–H4: reject overlap, allow 1–4

  • amneziawg-go: mergeWithDevice returns headers must not overlap
    (device/uapi.go).
  • Kernel module: H1 must not overlap with others, and the same for H2–H4
    (src/netlink.c).
  • The panel never sends a blank H (writeOptionalLine in internal/amneziawgnet/device.go), so the engine
    keeps its default: WireGuard's own type n (device.go, rang.FromUint32(MessageInitiationType, …)).
    The new check treats blank fields as that default. So H1 = 3 with a blank H3 is correctly rejected; the
    engine would refuse it.
  • 1–4 are valid. The amneziawg-go README lists no such restriction, and they are the engine's own
    default. With a HeaderProtectionKey (AWG 3+), the type field is encrypted on the wire (README, "Header
    protection"). Amnezia Premium uses exactly H1–H4 = 1/2/3/4 together with a key. The docs now say that
    1–4 are valid, but that without a HeaderProtectionKey they leave the type field looking like plain WireGuard.

Docs corrections

  • The S1/S2 row said "amneziawg-go rejects S1 + 56 = S2". It does not: I checked amneziawg-go
    (uapi.go, device.go), amneziawg-tools (config.c) and the kernel module (netlink.c), and a real
    IpcSet with S1=30, S2=86 returns nil. The panel keeps the rule as a fingerprint guard (both packets
    would have the same size on the wire), and the docs now say so.
  • The S1/S2/S3 rows state the new ranges and the reason. The H1–H4 row: see above.

Tests

  • internal/amneziawg/params_test.go covers:
    • S1=1553 and S2=1609 are rejected; {1552, 30}, {20, 1608} and {284, 659} are accepted;
    • S3=1637 is rejected; 1045 and 1636 are accepted;
    • overlap cases are rejected: 100-200 vs 150-300, 7 vs 7, and 3 vs blank-H3;
    • 1/2/3/4, all-blank and disjoint ranges are accepted.
      All of the new cases fail on the old code.
  • internal/amneziawgnet/device_test.go TestValidatedObfuscationAlwaysApplies runs every set the
    validator accepts through a real amneziawg-go IpcSet. New cases: H overlap, H1 equal to the blank-H3
    default, H 1–4, the S1/S3 and S2 bounds, and the full Amnezia Premium set. On the old validator it failed
    twice with IPC error -22: failed to merge with device: headers must not overlap.
  • frontend/src/test/amneziawg-schema-cleared.test.ts: over-limit values are S1 1553, S2 1609, S3 1637;
    at-limit values are 1552, 1608, 1636. It fails with the old .max(65535) / .max(64).
  • Local runs:
    • go test ./internal/amneziawg/... ./internal/amneziawgnet/... ./internal/web/service/ ./internal/sub/ —
      amneziawgnet under Linux; on Windows one of its UDP tests hangs for environment reasons, unrelated to
      this change;
    • golangci-lint (GOOS=linux) on the touched packages: 0 issues;
    • gofumpt, npm run typecheck, npm run lint: clean.
      Both branches were tested.
  • Live, on a lucx.265 build with this commit (API against a running panel):
    • overlapping H, S1=1553 and H1=3 with a blank H3 are refused, each with a clear message;
    • S1 1552 + S3 1636 + H 1–4, and S2 1608, are accepted, and the embedded device starts
      ("started embedded interface");
    • the Amnezia Premium set runs in production-like use, and a Linux amneziawg 3.1 client handshakes and
      passes traffic.

Not in this PR

  • The form does not check H overlap client-side. The server now rejects it with a clear message, so the UI
    duplicate was left out.
  • The generator is unchanged (S3 12–55, S1/S2 15–150, H ≥ 5).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6642 **Author:** [@rudenko-ks](https://github.com/rudenko-ks) **Created:** 9/24/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/amneziawg-obfuscation-bounds` --- ### 📝 Commits (4) - [`44d9c1c`](https://github.com/MHSanaei/3x-ui/commit/44d9c1c15429eb17a41d534f2e83c66cbaefdb6d) fix(amneziawg): bound S1-S3 by the receive buffer, reject overlapping H - [`24147de`](https://github.com/MHSanaei/3x-ui/commit/24147deaf4395d32f1e3657cc5382c2533cde2fe) Merge branch 'main' into fix/amneziawg-obfuscation-bounds - [`a5bce06`](https://github.com/MHSanaei/3x-ui/commit/a5bce06847e84d2b411b86220bb2926aa1f58dc9) Merge remote-tracking branch 'origin/main' into pr-6642 - [`7f38f51`](https://github.com/MHSanaei/3x-ui/commit/7f38f51e7c13a6206a62eed352d04d1a45f8a349) fix(amneziawg): bound only inbound padding by the iOS receive buffer ### 📊 Changes **12 files changed** (+189 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/config/amneziawg.mdx` (+3 -3) 📝 `frontend/src/pages/inbounds/form/protocols/amneziawg.tsx` (+3 -3) 📝 `frontend/src/pages/xray/outbounds/protocols/amneziawg.tsx` (+3 -3) 📝 `frontend/src/schemas/protocols/inbound/amneziawg.ts` (+3 -3) 📝 `frontend/src/schemas/protocols/outbound/amneziawg.ts` (+4 -3) 📝 `frontend/src/test/amneziawg-schema-cleared.test.ts` (+22 -9) 📝 `internal/amneziawg/outbound_test.go` (+22 -0) 📝 `internal/amneziawg/params.go` (+53 -6) 📝 `internal/amneziawg/params_test.go` (+63 -4) 📝 `internal/amneziawgnet/device_test.go` (+11 -0) 📝 `internal/web/service/inbound_amneziawg.go` (+1 -1) 📝 `internal/web/service/inbound_amneziawg_test.go` (+1 -0) </details> ### 📄 Description ## Summary The native AmneziaWG validator (`internal/amneziawg/params.go`), both Zod schemas, both forms and the docs disagreed with amneziawg-go in three places. This PR makes them follow what the engine and its clients actually enforce. 1. **S3 was capped at 64 with no engine reason.** Real configs could not be saved. Example: Amnezia's own **Amnezia Premium** ships `s3=1045` (`s1=284 s2=659 s4=12`, read from a live tunnel over UAPI). 2. **S1/S2 allowed up to 65535.** An iOS peer cannot receive a handshake that large. 3. **Overlapping H1–H4 ranges were accepted.** amneziawg-go and the kernel module then refuse the whole device, so the inbound saved fine and never came up. The docs also stated two things the engine does not do. They said H `1`–`4` "must not be used". They said "amneziawg-go rejects" `S1 + 56 = S2`. ## 1–2. S1–S3: the real bound is the smallest receive buffer - Handshake messages are sent as `padding + size`, where the sizes are `MessageInitiationSize = 148`, `MessageResponseSize = 92` and `MessageCookieReplySize = 64`: [device/send.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/send.go), [device/noise-protocol.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/noise-protocol.go). - The receiver reads every datagram into a `[MaxMessageSize]byte` buffer, where `MaxMessageSize = MaxSegmentSize`: [device/pools.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/pools.go), [device/constants.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/constants.go). - `MaxSegmentSize` differs by platform: - 65535 on Linux and Android ([queueconstants_default.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/queueconstants_default.go), [queueconstants_android.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/queueconstants_android.go)); - 2016 on Windows ([queueconstants_windows.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/queueconstants_windows.go)); - **1700 on iOS** ([queueconstants_ios.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/queueconstants_ios.go)). - For every client to receive every handshake message: - **S1 ≤ 1552** - **S2 ≤ 1608** - **S3 ≤ 1636** - `RandomTrailers` does not change these bounds. It only tops a packet up to `DefaultUdpWindow = 500` bytes (`randomTrailer()` in send.go), so it never grows a message that is already larger. - Where 64 came from: #6105 ported the validator from coinman-dev/3ax-ui in the kernel-module era, and gives no reason for it. S4 ≤ 32 does have a reason (transport packets ride at full MTU) and is unchanged. - **Behaviour change:** a saved inbound with S1 > 1552 or S2 > 1608 now fails validation on the next edit. Such a config never worked for iOS clients. Linux/Android peers could still receive it. - **Side effect worth knowing:** 1552 + 56 = 1608, so the two maxima together trip the panel's own `S1 + 56 ≠ S2` rule. Each maximum is valid on its own. ## 3. H1–H4: reject overlap, allow 1–4 - amneziawg-go: `mergeWithDevice` returns `headers must not overlap` ([device/uapi.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/uapi.go)). - Kernel module: `H1 must not overlap with others`, and the same for H2–H4 ([src/netlink.c](https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/blob/master/src/netlink.c)). - The panel never sends a blank H (`writeOptionalLine` in `internal/amneziawgnet/device.go`), so the engine keeps its default: WireGuard's own type `n` (`device.go`, `rang.FromUint32(MessageInitiationType, …)`). The new check treats blank fields as that default. So `H1 = 3` with a blank H3 is correctly rejected; the engine would refuse it. - **1–4 are valid.** The amneziawg-go README lists no such restriction, and they are the engine's own default. With a HeaderProtectionKey (AWG 3+), the type field is encrypted on the wire (README, "Header protection"). Amnezia Premium uses exactly `H1–H4 = 1/2/3/4` together with a key. The docs now say that 1–4 are valid, but that without a HeaderProtectionKey they leave the type field looking like plain WireGuard. ## Docs corrections - The S1/S2 row said "amneziawg-go rejects `S1 + 56 = S2`". It does not: I checked amneziawg-go (`uapi.go`, `device.go`), amneziawg-tools (`config.c`) and the kernel module (`netlink.c`), and a real `IpcSet` with S1=30, S2=86 returns nil. The panel keeps the rule as a fingerprint guard (both packets would have the same size on the wire), and the docs now say so. - The S1/S2/S3 rows state the new ranges and the reason. The H1–H4 row: see above. ## Tests - `internal/amneziawg/params_test.go` covers: - S1=1553 and S2=1609 are rejected; {1552, 30}, {20, 1608} and {284, 659} are accepted; - S3=1637 is rejected; 1045 and 1636 are accepted; - overlap cases are rejected: `100-200` vs `150-300`, `7` vs `7`, and `3` vs blank-H3; - `1/2/3/4`, all-blank and disjoint ranges are accepted. All of the new cases fail on the old code. - `internal/amneziawgnet/device_test.go` `TestValidatedObfuscationAlwaysApplies` runs every set the validator accepts through a **real amneziawg-go `IpcSet`**. New cases: H overlap, H1 equal to the blank-H3 default, H 1–4, the S1/S3 and S2 bounds, and the full Amnezia Premium set. On the old validator it failed twice with `IPC error -22: failed to merge with device: headers must not overlap`. - `frontend/src/test/amneziawg-schema-cleared.test.ts`: over-limit values are S1 1553, S2 1609, S3 1637; at-limit values are 1552, 1608, 1636. It fails with the old `.max(65535)` / `.max(64)`. - Local runs: - `go test ./internal/amneziawg/... ./internal/amneziawgnet/... ./internal/web/service/ ./internal/sub/` — amneziawgnet under Linux; on Windows one of its UDP tests hangs for environment reasons, unrelated to this change; - `golangci-lint` (GOOS=linux) on the touched packages: 0 issues; - `gofumpt`, `npm run typecheck`, `npm run lint`: clean. Both branches were tested. - Live, on a lucx.265 build with this commit (API against a running panel): - overlapping H, S1=1553 and H1=3 with a blank H3 are refused, each with a clear message; - S1 1552 + S3 1636 + H 1–4, and S2 1608, are accepted, and the embedded device starts ("started embedded interface"); - the Amnezia Premium set runs in production-like use, and a Linux amneziawg 3.1 client handshakes and passes traffic. ## Not in this PR - The form does not check H overlap client-side. The server now rejects it with a clear message, so the UI duplicate was left out. - The generator is unchanged (S3 12–55, S1/S2 15–150, H ≥ 5). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:26 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6393
No description provided.