Skip to content

caddyauth: only replace known placeholders in basic auth credentials - #8017

Merged
steadytao merged 1 commit into
caddyserver:masterfrom
hktitof:fix/basicauth-replaceknown
Sep 26, 2026
Merged

steadytao merged 1 commit into
caddyserver:masterfrom
hktitof:fix/basicauth-replaceknown

Conversation

@hktitof

@hktitof hktitof commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8016

Summary

the basic auth provider expanded account usernames and passwords with repl.ReplaceAll, which blanks any {...} the replacer does not recognize, so a username like alice-{bogus} was provisioned as alice- and a password like ab{cd}ef was quietly rewritten into a different string that still parses as valid base64, so provisioning accepted decoded bytes that are not a usable bcrypt hash and logins simply failed

this switches both to repl.ReplaceKnown, matching the respond headers fix in #8014 and the header handler fix in #4880 (same class as #4418), so unrecognized braces in credentials are left intact and real placeholders like {env.*} still expand

Steps to reproduce

  1. go test ./modules/caddyhttp/caddyauth/ -run TestBasicAuth -count=1
  2. Expected: unknown {...} in usernames survive as literal data, a braced password is rejected instead of silently rewritten, {env.*} placeholders still expand
  3. Actual (raw output on untouched master 56e3a88):
--- FAIL: TestBasicAuthProvisionKeepsUnknownPlaceholdersInUsername (0.00s)
    basicauth_test.go:43: expected an account for the username as configured, got accounts ["alice-"]
--- FAIL: TestBasicAuthProvisionRejectsBracedPasswordInsteadOfRewritingIt (0.00s)
    basicauth_test.go:98: expected provisioning to fail for a password with literal braces, instead it accepted the rewritten password "i\xb7\x9f"
FAIL
FAIL	github.com/caddyserver/caddy/v2/modules/caddyhttp/caddyauth	0.011s

regression tests in basicauth_test.go pin all three directions: unknown braces survive, a known placeholder still expands, and a braced password fails provisioning loudly instead of being quietly rewritten

Assistance Disclosure

AI used: yes, for drafting test coverage and description formatting

The basic auth provider expanded account usernames and passwords with
repl.ReplaceAll, which blanks any {...} the replacer does not recognize

Credentials are config data, so a username like alice-{bogus} was stored
as alice- and a password like ab{cd}ef was quietly rewritten into
different valid base64, turning a config mistake into a working
password the admin never set

Use ReplaceKnown, matching the respond headers fix in caddyserver#8014 and the
header handler fix in caddyserver#4880, so unknown braces survive and real
placeholders like {env.*} still expand

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please correct the password-impact claim; the example becomes valid base64, not a usable bcrypt hash. It does not demonstrate an unintended working password.

@hktitof

hktitof commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

good catch, thanks, i corrected the wording in the description, the rewritten value is just base64 that decodes but never works as a hash, so the real problem is silent provisioning and not a working password

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@Santoshkumarpuppala

Copy link
Copy Markdown

Separate from this PR (the order predates it), happy to open an issue for this: basicauth.go:105-106 checks hba.Accounts[acct.Username] for duplicates before the username is expanded at :109-110, and :129 stores the expanded name. The order is the same at master df77f8b and in #8021; read, not run, at v2.11.4.

I ran this at 2d38ac5 and df77f8b with X=Y=carol. carol+carol fails with "username is not unique", but {env.X}+{env.Y}, carol+{env.Y} and even {env.X}+{env.X} load as one account, and only the second password works because :129 overwrites the first entry. Control: {env.X}+{env.Z} (Z=dave) gives two working accounts. With a df77f8b binary, caddy validate rejects literal and {$VU1}/{$VU2} duplicates but reports "Valid configuration" for {env.VU1}/{env.VU2}.

Scope: not an auth bypass. One credential silently stops working. Moving the check below the two Replace lines at 2d38ac5 makes all four collision cases above error, the control still passes, and go test ./modules/caddyhttp/caddyauth/ -count=1 is ok. Happy to be told the order is deliberate.

Disclosure: I used AI assistance to trace and test this.

@hktitof

hktitof commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

thanks for testing this so carefully @Santoshkumarpuppala, i checked the order again and you are right, the dup check runs on the raw name before the placeholders expand, so two accounts that only collide after expansion load as one and the second password wins silently, opening an issue for it would be welcome bcs this predates the pr and stays outside its scope

@hktitof

hktitof commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

all checks green on the head, ready whenever you have time @steadytao!

@steadytao
steadytao enabled auto-merge (squash) September 26, 2026 08:52
@steadytao
steadytao merged commit 69ec5df into caddyserver:master Sep 26, 2026
27 checks passed
@hktitof

hktitof commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

thanks for the review and merge @steadytao, happy to contribute to caddy!

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.

basic auth blanks literal braces in usernames and passwords (unknown placeholders)

3 participants