Skip to content

add SHA support to htpasswd - #413

Merged
mattfarina merged 1 commit into
Masterminds:masterfrom
vixns:master
Oct 28, 2024
Merged

mattfarina merged 1 commit into
Masterminds:masterfrom
vixns:master

Conversation

@vixns

@vixns vixns commented Oct 10, 2024 •

Copy link
Copy Markdown
Contributor

My use case is to address hashicorp vault > external-password > k8s password > envoy gateway > envoy
envoy only supports sha encoding, external-password uses sprig, sprig encodes to bcrypt

This PR will allow to simplify this chain and partially fix #32.

I've opened a counterpart issue in envoy repo to also add bcrypt decoding support envoyproxy/envoy#36278

I hope this will be fixed both ways, allowing better security with a smooth transition from sha to bcrypt on existing apps

@phoenix-bjoern

phoenix-bjoern commented Dec 17, 2025 •

Copy link
Copy Markdown

Security Concerns with the Current SHA Implementation

While I understand this PR was created to address Envoy compatibility (which currently only supports {SHA}), I'd like to suggest adding secure alternatives for other use cases.

Current Situation

Envoy Limitation:
Envoy's Basic Auth filter currently only supports {SHA} (unsalted SHA-1):

"Note that only SHA format is currently supported. Other formats may be added in the future."
— Envoy Basic Auth Documentation

The Security Issue:
Plain {SHA} has no salt, making it vulnerable to rainbow table attacks. While the current implementation is necessary for Envoy compatibility, users deploying to NGINX (which supports better algorithms) have no secure alternative.

Proposed Solution: Add Secure Alternatives

Instead of changing the existing "sha" behavior (which would break Envoy compatibility), I propose adding new algorithm options:

Suggested Implementation

// Default: bcrypt (most secure)
{{ htpasswd "user" "pass" }}
// Output: user:$2y$05$... (random, non-deterministic)

{{ htpasswd "user" "pass" "bcrypt" }}
// Output: user:$2y$05$... (same as default)

{{ htpasswd "user" "pass" "sha" }}
// Output: user:{SHA}... (unchanged - for Envoy)

{{ htpasswd "user" "pass" "ssha" }}
// Output: user:{SSHA}... (NEW - secure with auto-salt)

{{ htpasswd "user" "pass" "ssha" "fixedsalt" }}
// Output: user:{SSHA}... (NEW - deterministic for testing)

{{ htpasswd "user" "pass" "sha512" }}
// Output: user:$6$... (NEW - for NGINX)

{{ htpasswd "user" "pass" "sha512" "fixedsalt" }}
// Output: user:$6$fixedsalt$... (NEW - deterministic)

{{ htpasswd "user" "pass" "sha256" }}
// Output: user:$5$... (NEW - for NGINX)

{{ htpasswd "user" "pass" "sha256" "fixedsalt" }}
// Output: user:$5$fixedsalt$... (NEW - deterministic)

Algorithm Comparison

Algorithm Command Envoy NGINX Security Deterministic Use Case
bcrypt (default) htpasswd "user" "pass" ❌ No ⚠️ System-dep ⭐⭐⭐⭐⭐ Best ❌ No NGINX (recommended)
sha512 (NEW) htpasswd "user" "pass" "sha512" ❌ No ✅ Yes ⭐⭐⭐⭐ Very Good ✅ With salt NGINX (good alternative)
sha256 (NEW) htpasswd "user" "pass" "sha256" ❌ No ✅ Yes ⭐⭐⭐ Good ✅ With salt NGINX
ssha (NEW) htpasswd "user" "pass" "ssha" ❌ No ✅ Yes ⭐⭐ Better ✅ With salt NGINX, legacy apps
sha htpasswd "user" "pass" "sha" ✅ Yes ⚠️ Deprecated ❌ Insecure ✅ Always Envoy only

GitOps Example (reproducible deployments)

# Terraform/Helm values - always produces same output
apiVersion: v1
kind: Secret
metadata:
  name: app-auth
stringData:
  auth: |
    {{ htpasswd "deploy-user" "deploy-pass" "sha512" "deploysalt" }}
    {{ htpasswd "admin" "admin-pass" "sha256" "adminsalt" }}

Mixed Environments Example

# Support both Envoy and NGINX from same secret
apiVersion: v1
kind: Secret
metadata:
  name: multi-auth
stringData:
  # For Envoy
  envoy-auth: {{ htpasswd "envoy-user" "pass" "sha" }}
  
  # For NGINX (secure)
  nginx-auth: |
    {{ htpasswd "nginx-user" "pass" }}
    {{ htpasswd "nginx-admin" "secret" "sha512" }}

Benefits of This Approach

  1. ✅ Backward compatible - Existing "sha" behavior unchanged
  2. ✅ Secure default - bcrypt remains the default (most secure)
  3. ✅ Envoy compatible - Plain SHA still available when needed
  4. ✅ Secure options - SSHA, SHA-512, SHA-256 for NGINX
  5. ✅ Flexible - Optional salt parameter for deterministic behavior (except bcrypt)
  6. ✅ Clear intent - Users explicitly choose their algorithm
  7. ✅ No breaking changes - Pure addition of new features
  8. ✅ Testing friendly - Deterministic options for unit tests and GitOps

Related Issues

@vixns @mattfarina Would you be open to a follow-up PR that adds these secure alternatives while keeping the existing SHA implementation for Envoy compatibility?


Summary:

  • Keep default → bcrypt (most secure, non-deterministic)
  • Keep "sha" → {SHA} for Envoy (existing behavior)
  • Add "ssha" → {SSHA} for NGINX (secure alternative with optional salt)
  • Add "sha512" → $6$ for NGINX (recommended, with optional salt)
  • Add "sha256" → $5$ for NGINX (good option, with optional salt)
  • All new algorithms except bcrypt support optional salt parameter for deterministic behavior

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

Successfully merging this pull request may close these issues.

Add support for generating htpasswd hash

3 participants