mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-20 14:59:36 +00:00
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-latest, 0, 1.26, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
* caddyauth: Support third-party http.authentication.hashes modules basic_auth and hash-password only accepted the built-in bcrypt and argon2id algorithms, even though the http.authentication.hashes namespace is documented as extensible. Look up any other algorithm name among the installed hash modules instead, and list the available ones when the name is not recognized. The built-in algorithms keep their own hash-password flags; other modules are provisioned and must implement Hasher to generate hashes. Closes #6670 * caddyauth: Base64-encode non-MCF hashes from hash-password modules hash-password printed module hashes as raw bytes, but basic_auth base64-decodes any password that isn't in Modular Crypt Format, so the output couldn't be used as-is. Encode non-MCF hashes as base64 and add a round-trip test that provisions HTTPBasicAuth with the command output. * caddyauth: always base64-encode third-party hash output
302 lines
9.5 KiB
Go
302 lines
9.5 KiB
Go
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package caddyauth
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(testHash{})
|
|
caddy.RegisterModule(testCompareOnlyHash{})
|
|
caddy.RegisterModule(testMCFHash{})
|
|
}
|
|
|
|
// testHash is a third-party-style hash module used to
|
|
// test that modules in the http.authentication.hashes
|
|
// namespace can be used by name.
|
|
type testHash struct {
|
|
provisioned bool
|
|
}
|
|
|
|
func (testHash) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "http.authentication.hashes.testhash",
|
|
New: func() caddy.Module { return new(testHash) },
|
|
}
|
|
}
|
|
|
|
func (th *testHash) Provision(caddy.Context) error {
|
|
th.provisioned = true
|
|
return nil
|
|
}
|
|
|
|
func (testHash) Compare(hashed, plaintext []byte) (bool, error) {
|
|
return bytes.Equal(hashed, append([]byte("testhash:"), plaintext...)), nil
|
|
}
|
|
|
|
func (th testHash) Hash(plaintext []byte) ([]byte, error) {
|
|
if !th.provisioned {
|
|
return nil, errors.New("testhash: not provisioned")
|
|
}
|
|
return append([]byte("testhash:"), plaintext...), nil
|
|
}
|
|
|
|
func (testHash) FakeHash() []byte { return []byte("testhash:fake") }
|
|
|
|
// testCompareOnlyHash can compare, but not generate, hashes.
|
|
type testCompareOnlyHash struct{}
|
|
|
|
func (testCompareOnlyHash) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "http.authentication.hashes.testcompareonly",
|
|
New: func() caddy.Module { return new(testCompareOnlyHash) },
|
|
}
|
|
}
|
|
|
|
func (testCompareOnlyHash) Compare(hashed, plaintext []byte) (bool, error) {
|
|
return bytes.Equal(hashed, plaintext), nil
|
|
}
|
|
|
|
// testMCFHash produces hashes that start with "$", like Modular Crypt Format.
|
|
type testMCFHash struct{}
|
|
|
|
func (testMCFHash) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "http.authentication.hashes.testmcf",
|
|
New: func() caddy.Module { return new(testMCFHash) },
|
|
}
|
|
}
|
|
|
|
func (testMCFHash) Compare(hashed, plaintext []byte) (bool, error) {
|
|
return bytes.Equal(hashed, append([]byte("$testmcf$"), plaintext...)), nil
|
|
}
|
|
|
|
func (testMCFHash) Hash(plaintext []byte) ([]byte, error) {
|
|
return append([]byte("$testmcf$"), plaintext...), nil
|
|
}
|
|
|
|
func (testMCFHash) FakeHash() []byte { return []byte("$testmcf$fake") }
|
|
|
|
func adaptBasicAuth(t *testing.T, input string) (string, error) {
|
|
t.Helper()
|
|
adapter := caddyfile.Adapter{ServerType: httpcaddyfile.ServerType{}}
|
|
result, _, err := adapter.Adapt([]byte(input), map[string]any{"filename": "Caddyfile"})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var cfg struct {
|
|
Apps struct {
|
|
HTTP struct {
|
|
Servers map[string]struct {
|
|
Routes []struct {
|
|
Handle []struct {
|
|
Providers struct {
|
|
HTTPBasic struct {
|
|
Hash struct {
|
|
Algorithm string `json:"algorithm"`
|
|
} `json:"hash"`
|
|
} `json:"http_basic"`
|
|
} `json:"providers"`
|
|
} `json:"handle"`
|
|
} `json:"routes"`
|
|
} `json:"servers"`
|
|
} `json:"http"`
|
|
} `json:"apps"`
|
|
}
|
|
if err := json.Unmarshal(result, &cfg); err != nil {
|
|
t.Fatalf("unmarshaling adapted config: %v\n%s", err, result)
|
|
}
|
|
srv, ok := cfg.Apps.HTTP.Servers["srv0"]
|
|
if !ok || len(srv.Routes) != 1 || len(srv.Routes[0].Handle) != 1 {
|
|
t.Fatalf("unexpected adapted config: %s", result)
|
|
}
|
|
return srv.Routes[0].Handle[0].Providers.HTTPBasic.Hash.Algorithm, nil
|
|
}
|
|
|
|
func TestBasicAuthCaddyfileHashModules(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
algorithm string
|
|
expected string
|
|
}{
|
|
{name: "default", algorithm: "", expected: "bcrypt"},
|
|
{name: "bcrypt", algorithm: "bcrypt", expected: "bcrypt"},
|
|
{name: "argon2id", algorithm: "argon2id", expected: "argon2id"},
|
|
{name: "third-party", algorithm: "testhash", expected: "testhash"},
|
|
{name: "third-party compare only", algorithm: "testcompareonly", expected: "testcompareonly"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
input := ":80 {\n\tbasic_auth " + tc.algorithm + " {\n\t\talice dGVzdGhhc2g6c2VjcmV0\n\t}\n}\n"
|
|
algorithm, err := adaptBasicAuth(t, input)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if algorithm != tc.expected {
|
|
t.Errorf("expected hash algorithm %q, got %q", tc.expected, algorithm)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBasicAuthCaddyfileUnknownHash(t *testing.T) {
|
|
_, err := adaptBasicAuth(t, ":80 {\n\tbasic_auth nope {\n\t\talice c2VjcmV0\n\t}\n}\n")
|
|
if err == nil {
|
|
t.Fatal("expected an error for an unknown hash algorithm, got nil")
|
|
}
|
|
for _, want := range []string{"unrecognized hash algorithm: nope", "argon2id", "bcrypt", "testhash"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Errorf("expected error to contain %q, got: %v", want, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBasicAuthProvisionHashModule(t *testing.T) {
|
|
ctx, cancel := caddy.NewContext(caddy.Context{Context: t.Context()})
|
|
defer cancel()
|
|
|
|
hba := HTTPBasicAuth{
|
|
HashRaw: json.RawMessage(`{"algorithm": "testhash"}`),
|
|
AccountList: []Account{{
|
|
Username: "alice",
|
|
Password: "dGVzdGhhc2g6c2VjcmV0", // base64("testhash:secret")
|
|
}},
|
|
HashCache: new(Cache),
|
|
}
|
|
if err := hba.Provision(ctx); err != nil {
|
|
t.Fatalf("provisioning: %v", err)
|
|
}
|
|
if _, ok := hba.Hash.(*testHash); !ok {
|
|
t.Fatalf("expected hash module to be *testHash, got %T", hba.Hash)
|
|
}
|
|
if string(hba.fakePassword) != "testhash:fake" {
|
|
t.Errorf("expected fake password from module, got %q", hba.fakePassword)
|
|
}
|
|
if ok, err := hba.correctPassword(hba.Accounts["alice"], []byte("secret")); err != nil || !ok {
|
|
t.Errorf("expected password to match (err=%v)", err)
|
|
}
|
|
if ok, err := hba.correctPassword(hba.Accounts["alice"], []byte("wrong")); err != nil || ok {
|
|
t.Errorf("expected password not to match (err=%v)", err)
|
|
}
|
|
}
|
|
|
|
func runHashPassword(t *testing.T, algorithm, plaintext string) (string, error) {
|
|
t.Helper()
|
|
|
|
fs := pflag.NewFlagSet("hash-password", pflag.ContinueOnError)
|
|
fs.String("plaintext", plaintext, "")
|
|
fs.String("algorithm", algorithm, "")
|
|
fs.Int("bcrypt-cost", defaultBcryptCost, "")
|
|
fs.Uint32("argon2id-time", defaultArgon2idTime, "")
|
|
fs.Uint32("argon2id-memory", defaultArgon2idMemory, "")
|
|
fs.Uint8("argon2id-threads", defaultArgon2idThreads, "")
|
|
fs.Uint32("argon2id-keylen", defaultArgon2idKeylen, "")
|
|
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stdout := os.Stdout
|
|
os.Stdout = w
|
|
_, cmdErr := cmdHashPassword(caddycmd.Flags{FlagSet: fs})
|
|
os.Stdout = stdout
|
|
_ = w.Close()
|
|
|
|
out, err := io.ReadAll(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return strings.TrimSpace(string(out)), cmdErr
|
|
}
|
|
|
|
func TestHashPasswordCommandHashModule(t *testing.T) {
|
|
out, err := runHashPassword(t, "testhash", "secret")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if out != "dGVzdGhhc2g6c2VjcmV0" { // base64("testhash:secret")
|
|
t.Errorf("expected output %q, got %q", "dGVzdGhhc2g6c2VjcmV0", out)
|
|
}
|
|
|
|
out, err = runHashPassword(t, "testmcf", "secret")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// A leading "$" does not make a third-party hash MCF, so it is
|
|
// base64-encoded like any other module output.
|
|
if out != "JHRlc3RtY2Ykc2VjcmV0" { // base64("$testmcf$secret")
|
|
t.Errorf("expected output %q, got %q", "JHRlc3RtY2Ykc2VjcmV0", out)
|
|
}
|
|
}
|
|
|
|
// TestHashPasswordCommandRoundTrip checks that the output of the
|
|
// hash-password command is accepted by HTTPBasicAuth as an account password.
|
|
func TestHashPasswordCommandRoundTrip(t *testing.T) {
|
|
for _, algorithm := range []string{"testhash", "testmcf", "bcrypt", "argon2id"} {
|
|
t.Run(algorithm, func(t *testing.T) {
|
|
out, err := runHashPassword(t, algorithm, "secret")
|
|
if err != nil {
|
|
t.Fatalf("hash-password: %v", err)
|
|
}
|
|
|
|
ctx, cancel := caddy.NewContext(caddy.Context{Context: t.Context()})
|
|
defer cancel()
|
|
hba := HTTPBasicAuth{
|
|
HashRaw: json.RawMessage(`{"algorithm": "` + algorithm + `"}`),
|
|
AccountList: []Account{{Username: "alice", Password: out}},
|
|
}
|
|
if err := hba.Provision(ctx); err != nil {
|
|
t.Fatalf("provisioning with %q: %v", out, err)
|
|
}
|
|
if ok, err := hba.correctPassword(hba.Accounts["alice"], []byte("secret")); err != nil || !ok {
|
|
t.Errorf("expected password to match (err=%v)", err)
|
|
}
|
|
if ok, err := hba.correctPassword(hba.Accounts["alice"], []byte("wrong")); err != nil || ok {
|
|
t.Errorf("expected wrong password not to match (err=%v)", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHashPasswordCommandErrors(t *testing.T) {
|
|
_, err := runHashPassword(t, "nope", "secret")
|
|
if err == nil {
|
|
t.Fatal("expected an error for an unknown hash algorithm, got nil")
|
|
}
|
|
for _, want := range []string{"unrecognized hash algorithm: nope", "argon2id", "bcrypt", "testhash"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Errorf("expected error to contain %q, got: %v", want, err)
|
|
}
|
|
}
|
|
|
|
_, err = runHashPassword(t, "testcompareonly", "secret")
|
|
if err == nil || !strings.Contains(err.Error(), "does not support generating hashes") {
|
|
t.Errorf("expected an error for a hash module that cannot hash, got: %v", err)
|
|
}
|
|
}
|