Skip to content

Basic Authentication fails when .htpasswd uses CRLF line endings #8554

Closed

Description

@weeix

Description:

When configuring Basic Authentication using a Secret containing an .htpasswd file, Envoy Gateway fails to apply the configuration if the file uses CRLF line endings (common in Windows) instead of LF.

The primary issue is a discrepancy between the reported status and the actual state:

  1. SecurityPolicy status shows as Accepted: True with the message "Policy has been accepted."
  2. However, the Envoy Gateway logs reveal that the xDS update was rejected by Envoy with an error: invalid htpasswd format, invalid SHA hash length. This is likely because the carriage return (\r) is being treated as part of the hash string.

Expected Behavior:

  • The xDS translator should be resilient enough to handle both LF and CRLF line endings in .htpasswd files.
  • OR, the SecurityPolicy should be rejected during the validation phase with a clear error message in the Status field, rather than reporting success while the underlying Envoy configuration is failing.

Repro steps:

  1. Follow the official Basic Authentication task.
  2. Instead of a standard .htpasswd, create a Secret using a file with CRLF line endings. You can use the following YAML as a reference (contains a CRLF-encoded htpasswd):
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: basic-auth
data:
  # Contains "foo:{SHA}Ys23Ag/5IOWqZCw9QGaVDdHwH00=" with \r\n
  .htpasswd: Zm9vOntTSEF9WXMyM0FnLzVJT1dxWkN3OVFHYVZEZEh3SDAwPQ0K
  1. Apply a SecurityPolicy targeting an HTTPRoute.
  2. Run kubectl describe securitypolicy/basic-auth-example. It will show:
    • Reason: Accepted
    • Status: True
    • Message: Policy has been accepted.
  3. Check Envoy Gateway logs to see the xDS rejection error.

Environment:

  • Gateway Image: docker.io/envoyproxy/gateway:v1.7.1
  • Envoy Image: docker.io/envoyproxy/envoy:distroless-v1.37.1
  • Kubernetes Version: v1.33

Logs:

kubectl describe securitypolicy/basic-auth-example output:

Name:         basic-auth-example
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  gateway.envoyproxy.io/v1alpha1
Kind:         SecurityPolicy
Metadata:
  Creation Timestamp:  2026-03-19T02:37:29Z
  Generation:          1
  Resource Version:    123408604
  UID:                 6243f724-1783-413c-b275-51a2f63d9063
Spec:
  Basic Auth:
    Users:
      Group:
      Kind:   Secret
      Name:   basic-auth
  Target Refs:
    Group:  gateway.networking.k8s.io
    Kind:   HTTPRoute
    Name:   backend
Status:
  Ancestors:
    Ancestor Ref:
      Group:      gateway.networking.k8s.io
      Kind:       Gateway
      Name:       eg
      Namespace:  envoy-gateway-system
    Conditions:
      Last Transition Time:  2026-03-19T02:37:29Z
      Message:               Policy has been accepted.
      Observed Generation:   1
      Reason:                Accepted
      Status:                True
      Type:                  Accepted
    Controller Name:         gateway.envoyproxy.io/gatewayclass-controller
Events:                      <none>

Envoy Gateway logs (deploy/envoy-gateway):

2026-03-19T02:37:29.644Z        INFO    provider        kubernetes/controller.go:607    reconciled gateways successfully{"runner": "provider", "trace_id": "cade7f3b5bbb2184ce6660241c34c0a0", "span_id": "d14e6945331c1157"}
2026-03-19T02:37:29.644Z        INFO    gateway-api     runner/runner.go:190    received an update      {"runner": "gateway-api", "trace_id": "cade7f3b5bbb2184ce6660241c34c0a0", "span_id": "6a2676c2a02cabf6", "key": "gateway.envoyproxy.io/gatewayclass-controller"}
2026-03-19T02:37:29.645Z        INFO    gateway-api     gatewayapi/route.go:372 setting 503 direct response in routes due to no ready endpoints {"runner": "gateway-api", "trace_id": "cade7f3b5bbb2184ce6660241c34c0a0", "span_id": "6a2676c2a02cabf6", "routes": ["httproute/default/backend/rule/0/match/0"]}
2026-03-19T02:37:29.646Z        INFO    infrastructure  runner/runner.go:112    received an update      {"runner": "infrastructure", "key": "envoy-gateway-system/eg", "delete": false}
2026-03-19T02:37:29.646Z        INFO    provider        kubernetes/status_updater.go:143        received a status update{"runner": "provider", "namespace": "default", "name": "basic-auth-example", "kind": "SecurityPolicy"}
2026-03-19T02:37:29.646Z        INFO    xds     runner/runner.go:274    received an update      {"runner": "xds", "trace_id": "cade7f3b5bbb2184ce6660241c34c0a0", "span_id": "d8af2178ad1e0106"}
2026-03-19T02:37:29.653Z        ERROR   xds     cache/snapshotcache.go:388      Envoy rejected the last update with code 13 and message Error adding/updating listener(s) envoy-gateway-system/eg/http: basic auth: invalid htpasswd format, invalid SHA hash length
envoy-gateway-system/eg/www-example-com-https: basic auth: invalid htpasswd format, invalid SHA hash length

2026-03-19T02:37:29.653Z        INFO    xds     v3/simple.go:583        open delta watch ID:169 for type.googleapis.com/envoy.config.listener.v3.Listener Resources:map[] from nodeID: "envoy-envoy-gateway-system-eg-c3508b54-5577b8478kdhc4",  version "cade7f3b5bbb2184ce6660241c34c0a0"
2026-03-19T02:37:29.654Z        ERROR   xds     cache/snapshotcache.go:388      Envoy rejected the last update with code 13 and message basic auth: invalid htpasswd format, invalid SHA hash length

Activity

  1. stekole commented on Mar 19, 2026

    @stekole
    Contributor

    I was able to replicate this locally and confirmed.

    SecurityPolicy says Accepted: True / "Policy has been accepted."

    kubectl get securitypolicy basic-auth-example -n default -o yaml | grep -A10 conditions
        conditions:
        - lastTransitionTime: "2026-03-19T04:12:47Z"
          message: Policy has been accepted.
          observedGeneration: 1
          reason: Accepted
          status: "True"
          type: Accepted
        controllerName: gateway.envoyproxy.io/gatewayclass-controller
    

    and Envoy logs show: invalid htpasswd format, invalid SHA hash length

    2026-03-19T04:12:47.200Z        ERROR   xds     cache/snapshotcache.go:376      Envoy rejected the last update with code 13 and message basic auth: invalid htpasswd format, invalid SHA hash length
    

    Looks like something in https://github.com/envoyproxy/gateway/blob/main/internal/gatewayapi/securitypolicy.go#L2030 and may require normalize validateHtpasswdFormat and return cleaned bytes

    I could take a look at fixing.

  2. zirain commented on Mar 19, 2026

    @zirain
    Member
  3. zhaohuabing commented on Mar 19, 2026

    @zhaohuabing
    Member

    @stekole It seems that you already have a fix, feel free to open a pr.

  4. stekole commented on Mar 19, 2026

    @stekole
    Contributor

    /assign

  5. added this to the v1.8.0-rc.1 Release milestone on Mar 23, 2026
  6. added
    kind/bug
    on Mar 23, 2026
  7. added a commit that references this issue on Mar 23, 2026
    9cac348
  8. added 2 commits that reference this issue on Apr 15, 2026
    c373304
    174c1d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugtriage

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions