fix: normalize CRLF line endings in .htpasswd to prevent xDS rejection#8567
fix: normalize CRLF line endings in .htpasswd to prevent xDS rejection#8567Teja079 wants to merge 1 commit intoenvoyproxy:mainfrom
Conversation
✅ Deploy Preview for cerulean-figolla-1f9435 canceled.
|
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent Envoy xDS rejection when BasicAuth .htpasswd secrets are created on Windows by normalizing CRLF line endings before the data is handed to Envoy.
Changes:
- Normalize
\r\nto\nfor BasicAuth users secret data during translation. - Add a unit test case covering CRLF line endings for
validateHtpasswdFormat.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/gatewayapi/securitypolicy.go | Normalizes htpasswd line endings prior to validation/IR construction. |
| internal/gatewayapi/securitypolicy_test.go | Adds a CRLF test case for htpasswd format validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Normalize CRLF to LF to handle .htpasswd files generated on Windows. | ||
| // Without this, the \r character gets included in the hash string, causing | ||
| // Envoy to reject the xDS config with "invalid SHA hash length" errors. | ||
| usersSecretBytes = []byte(strings.ReplaceAll(string(usersSecretBytes), "\r\n", "\n")) | ||
|
|
There was a problem hiding this comment.
usersSecretBytes normalization currently converts []byte -> string -> []byte, which always makes at least two full copies even when there are no CRLFs. Consider using bytes.ReplaceAll (or a bytes.Contains guard) to avoid unconditional allocations/copies for large secrets.
| // Normalize CRLF to LF to handle .htpasswd files generated on Windows. | ||
| // Without this, the \r character gets included in the hash string, causing | ||
| // Envoy to reject the xDS config with "invalid SHA hash length" errors. | ||
| usersSecretBytes = []byte(strings.ReplaceAll(string(usersSecretBytes), "\r\n", "\n")) |
There was a problem hiding this comment.
This normalization only replaces CRLF (\r\n). If the secret contains stray \r characters (e.g., mixed line endings or a final line ending with \r only), they will still be passed through to Envoy and can trigger the same rejection. Consider also stripping standalone \r (e.g., replace all \r with empty) or normalizing more generally.
| // Normalize CRLF to LF to handle .htpasswd files generated on Windows. | |
| // Without this, the \r character gets included in the hash string, causing | |
| // Envoy to reject the xDS config with "invalid SHA hash length" errors. | |
| usersSecretBytes = []byte(strings.ReplaceAll(string(usersSecretBytes), "\r\n", "\n")) | |
| // Strip carriage returns to handle .htpasswd files generated on Windows. | |
| // Without this, the \r character gets included in the hash string, causing | |
| // Envoy to reject the xDS config with "invalid SHA hash length" errors. | |
| usersSecretBytes = []byte(strings.ReplaceAll(string(usersSecretBytes), "\r", "")) |
| { | ||
| name: "valid htpasswd with CRLF line endings (Windows-style)", | ||
| htpasswd: "user1:{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g=\r\nuser2:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=\r\n", | ||
| wantError: false, | ||
| }, |
There was a problem hiding this comment.
The new CRLF test case doesn’t actually validate the PR’s behavior change. validateHtpasswdFormat already calls strings.TrimSpace(line), which trims a trailing \r, so this test would pass even without the CRLF normalization in buildBasicAuth. Consider adding a unit test that exercises buildBasicAuth (or the IR/xDS generation) and asserts the emitted Users data contains no \r characters.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8567 +/- ##
==========================================
+ Coverage 74.15% 74.17% +0.01%
==========================================
Files 242 242
Lines 37749 37750 +1
==========================================
+ Hits 27992 28000 +8
+ Misses 7804 7797 -7
Partials 1953 1953 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@Teja079 can you fix the conflicts and sign your commits? |
What type of PR is this?
What this PR does / why we need it:
Which issue(s) this PR fixes:
Fixes #
Release Notes: Yes/No