Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ALPN to pg if client sends Postgres Magic Bytes #5

Open
coolaj86 opened this issue May 11, 2024 · 0 comments
Open

Set ALPN to pg if client sends Postgres Magic Bytes #5

coolaj86 opened this issue May 11, 2024 · 0 comments

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented May 11, 2024

If the client sends 00 00 00 08 04 d2 16 2f ("SSLRequest" - similar to StartTLS, but more ancient), then set ALPN to pg and send back N to force a plain connection through the tunnel, or S to tunnel TLS within TLS.

If the client sends 00 00 xx xx 00 03 00 00 (message length, v3), then it's a plain postgres connection. The whole first 4 bytes may be the Little-Endian encoded length (supports > 4k query params), but for the purpose of auto-detection we can assume the first two (or even 3) will be 0.

See also:

Postgres Magic Bytes

nc -l 54321 | hexyl
PGCONNECT_TIMEOUT=1 psql 'postgres://u:p@localhost:54321/d?sslmode=verify-full'
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 00 00 00 08 04 d2 16 2f ┊                         │⋄⋄⋄••×•/┊        │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
PGCONNECT_TIMEOUT=1 psql 'postgres://u:p@localhost:54321/d?sslmode=disable'
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 00 00 00 46 00 03 00 00 ┊ 75 73 65 72 00 75 00 64 │⋄⋄⋄F⋄•⋄⋄┊user⋄u⋄d│
│00000010│ 61 74 61 62 61 73 65 00 ┊ 64 00 61 70 70 6c 69 63 │atabase⋄┊d⋄applic│
│00000020│ 61 74 69 6f 6e 5f 6e 61 ┊ 6d 65 00 70 73 71 6c 00 │ation_na┊me⋄psql⋄│
│00000030│ 63 6c 69 65 6e 74 5f 65 ┊ 6e 63 6f 64 69 6e 67 00 │client_e┊ncoding⋄│
│00000040│ 55 54 46 38 00 00       ┊                         │UTF8⋄⋄  ┊        │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant