mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-05 01:33:30 -05:00
* fastcgi: derive SCRIPT_FILENAME from the normalized request path
Path matchers evaluate a normalized request path — MatchPath applies
normalizeWindowsPath and then CleanPath before comparing — but the FastCGI
transport split r.URL.Path verbatim. Caddy does not rewrite r.URL.Path itself,
so the two disagreed whenever the path contained dot segments after the split
token: /admin.php/../../other.php was matched as /other.php while
SCRIPT_FILENAME resolved to <root>/admin.php, and on Windows the same held for
/admin.php\..\other.php. A route gated by a path matcher could therefore execute
a script that matcher never evaluated.
php_fastcgi generates a rewrite route that normally normalizes the path first,
but that route can fail to match, so the transport establishes the invariant
itself rather than inheriting it. The reason it can fail to match is addressed
in the following commit; this change does not depend on it.
caddyhttp.NormalizedPath is added so a consumer acting on a path a matcher has
already judged derives it exactly as the matcher did, rather than reassembling
the two steps and risking drift.
Scope — two divergences of the same class are NOT addressed here:
* SCRIPT_NAME remains a prefix of the request path, so a matcher naming a
script exactly (path /admin.php) is still reachable via PATH_INFO at
/admin.php/anything. Matchers protecting a script need a trailing wildcard
regardless; that is inherent to CGI, not to this bug.
* On Windows, NTFS 8.3 short names are not rejected here. fileserver's
ServeHTTP rejects them (and ':' for ADS) precisely because matchers cannot
see through them, so GET /ADMIN~1.PHP is judged as /admin~1.php by a
path matcher while NTFS resolves SCRIPT_FILENAME to admin.php. Closing that
means sharing fileserver's hasWindowsShortName across packages, which is a
larger change than this fix; it is reported alongside this one and a patch
is available on request.
Side effects: PATH_INFO and DOCUMENT_URI are now derived from the normalized
path, so repeated slashes are merged and "." segments removed (/index.php//a//b
yields PATH_INFO=/a/b rather than //a//b), and on Windows per-component trailing
dots and spaces are folded. An application routing on a raw PATH_INFO will see
this. An empty URL.Path stays empty rather than becoming ".". REQUEST_URI is
unchanged and still carries the original target.
Tests: a buildEnv regression over literal and percent-encoded dot segments,
covering that legitimate PATH_INFO is preserved and an empty path stays empty; a
Windows-only counterpart whose cases each fail without the Windows
normalization; and an end-to-end test of a basic_auth-protected php_fastcgi
route against a FastCGI responder reporting the SCRIPT_FILENAME it received. The
end-to-end test uses `index off` so no rewrite route exists, ensuring it
exercises the transport rather than the rewrite.
* fileserver: make first_exist_fallback fall back when the file is absent
The try_policy first_exist_fallback is documented to match unconditionally,
falling back to the last try_files entry when no earlier candidate exists. It
did not: candidates are built with fs.Glob, which returns only paths that exist,
so an absent final entry left the list empty and the fallback branch — nested
inside the loop over that list — was unreachable.
php_fastcgi hard-codes this policy so its generated rewrite route always runs;
when the fallback failed, that rewrite was silently skipped.
Keep the pattern itself as a candidate when nothing globbed. This also aligns
Linux with Windows, where globResults is populated without an existence check
and the fallback already fired.
Behaviour change worth calling out: on a php_fastcgi site whose root has no
index.php (or no file matching the configured index), the rewrite now always
runs, so a request for a path that exists neither on the filesystem nor as a PHP
script is proxied to the backend as <root>/index.php instead of falling through
to file_server and returning 404. That is what the documented fallback promises,
but for a site of standalone .php scripts plus static assets it is a visible
change: unmatched requests now reach PHP-FPM.
The other try policies still test existence explicitly, so a candidate that does
not exist is skipped — but note this applies to ANY pattern that expands to
nothing, not only a malformed glob: a file whose literal name contains glob
metacharacters can now match under first_exist, largest_size and smallest_size
where previously globbing found it only via expansion.
* fileserver: synthesize the literal candidate only for the fallback entry
Address review: the previous commit appended the pattern itself as a
candidate inside makeCandidates whenever globbing found nothing. Since
makeCandidates is shared by every try_policy, first_exist, largest_size,
smallest_size and most_recently_modified could then stat the literal
pattern, so a file whose name contains glob metacharacters (for example
"lit[1].txt") matched where it did not before.
Restore makeCandidates to its original behaviour and build the literal
candidate only for the final entry of first_exist_fallback, the one
entry that is documented to match unconditionally. Placeholders in that
entry are expanded without glob escaping, since nothing is globbed, so
a request for /a[b].txt falls back to /a[b].txt rather than /a\[b].txt.
The path is still cleaned and joined with SanitizedPathJoin, so it
cannot escape the root.
This supersedes the last paragraph of the previous commit message: the
other policies now behave exactly as they did before that commit. The
php_fastcgi behaviour change described there still applies.
Add a regression test that creates a file literally named "lit[1].txt"
and checks that only the fallback entry of first_exist_fallback matches
it by its literal name, including the split_path remainder.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fastcgi: add license headers to new test files
Address review: phpfastcgi_test.go and fastcgi_windows_test.go were
added without the Apache license header used across the codebase. On
the Windows test, the header goes above the build constraint, matching
replacer_fuzz.go.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fastcgi: avoid hardcoded Unix paths in dot-segment tests
Address review: the new tests compared SCRIPT_FILENAME against strings
with forward slashes ("/srv/other.php", a "/admin.php" suffix), but the
transport makes the root absolute and joins it with filepath.Join, so
on Windows the value is C:\srv\other.php and the assertions would fail.
Use a temporary directory as the root and build the expected paths with
filepath instead.
Also lower the bcrypt cost of the integration test's basic_auth hash to
the minimum. At cost 14 the authenticated request exceeded the 5s test
client timeout under -race.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
769 lines
23 KiB
Go
769 lines
23 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 fileserver
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"cel.dev/cel-go/cel"
|
|
"cel.dev/cel-go/common"
|
|
"cel.dev/cel-go/common/ast"
|
|
"cel.dev/cel-go/common/operators"
|
|
"cel.dev/cel-go/common/types"
|
|
"cel.dev/cel-go/common/types/ref"
|
|
"cel.dev/cel-go/parser"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(MatchFile{})
|
|
}
|
|
|
|
// MatchFile is an HTTP request matcher that can match
|
|
// requests based upon file existence.
|
|
//
|
|
// Upon matching, three new placeholders will be made
|
|
// available:
|
|
//
|
|
// - `{http.matchers.file.relative}` The root-relative
|
|
// path of the file. This is often useful when rewriting
|
|
// requests.
|
|
// - `{http.matchers.file.absolute}` The absolute path
|
|
// of the matched file.
|
|
// - `{http.matchers.file.type}` Set to "directory" if
|
|
// the matched file is a directory, "file" otherwise.
|
|
// - `{http.matchers.file.remainder}` Set to the remainder
|
|
// of the path if the path was split by `split_path`.
|
|
//
|
|
// Even though file matching may depend on the OS path
|
|
// separator, the placeholder values always use /.
|
|
type MatchFile struct {
|
|
// The file system implementation to use. By default, the
|
|
// local disk file system will be used.
|
|
FileSystem string `json:"fs,omitempty"`
|
|
|
|
// The root directory, used for creating absolute
|
|
// file paths, and required when working with
|
|
// relative paths; if not specified, `{http.vars.root}`
|
|
// will be used, if set; otherwise, the current
|
|
// directory is assumed. Accepts placeholders.
|
|
Root string `json:"root,omitempty"`
|
|
|
|
// The list of files to try. Each path here is
|
|
// considered related to Root. If nil, the request
|
|
// URL's path will be assumed. Files and
|
|
// directories are treated distinctly, so to match
|
|
// a directory, the filepath MUST end in a forward
|
|
// slash `/`. To match a regular file, there must
|
|
// be no trailing slash. Accepts placeholders. If
|
|
// the policy is "first_exist", then an error may
|
|
// be triggered as a fallback by configuring "="
|
|
// followed by a status code number,
|
|
// for example "=404".
|
|
TryFiles []string `json:"try_files,omitempty"`
|
|
|
|
// How to choose a file in TryFiles. Can be:
|
|
//
|
|
// - first_exist
|
|
// - first_exist_fallback
|
|
// - smallest_size
|
|
// - largest_size
|
|
// - most_recently_modified
|
|
//
|
|
// Default is first_exist.
|
|
TryPolicy string `json:"try_policy,omitempty"`
|
|
|
|
// A list of delimiters to use to split the path in two
|
|
// when trying files. If empty, no splitting will
|
|
// occur, and the path will be tried as-is. For each
|
|
// split value, the left-hand side of the split,
|
|
// including the split value, will be the path tried.
|
|
// For example, the path `/remote.php/dav/` using the
|
|
// split value `.php` would try the file `/remote.php`.
|
|
// Each delimiter must appear at the end of a URI path
|
|
// component in order to be used as a split delimiter.
|
|
SplitPath []string `json:"split_path,omitempty"`
|
|
|
|
fsmap caddy.FileSystems
|
|
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
func (MatchFile) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "http.matchers.file",
|
|
New: func() caddy.Module { return new(MatchFile) },
|
|
}
|
|
}
|
|
|
|
// UnmarshalCaddyfile sets up the matcher from Caddyfile tokens. Syntax:
|
|
//
|
|
// file <files...> {
|
|
// root <path>
|
|
// try_files <files...>
|
|
// try_policy first_exist|smallest_size|largest_size|most_recently_modified
|
|
// }
|
|
func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
// iterate to merge multiple matchers into one
|
|
for d.Next() {
|
|
m.TryFiles = append(m.TryFiles, d.RemainingArgs()...)
|
|
for d.NextBlock(0) {
|
|
switch d.Val() {
|
|
case "root":
|
|
if !d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
m.Root = d.Val()
|
|
case "try_files":
|
|
m.TryFiles = append(m.TryFiles, d.RemainingArgs()...)
|
|
if len(m.TryFiles) == 0 {
|
|
return d.ArgErr()
|
|
}
|
|
case "try_policy":
|
|
if !d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
m.TryPolicy = d.Val()
|
|
case "split_path":
|
|
m.SplitPath = d.RemainingArgs()
|
|
if len(m.SplitPath) == 0 {
|
|
return d.ArgErr()
|
|
}
|
|
default:
|
|
return d.Errf("unrecognized subdirective: %s", d.Val())
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CELLibrary produces options that expose this matcher for use in CEL
|
|
// expression matchers.
|
|
//
|
|
// Example:
|
|
//
|
|
// expression file()
|
|
// expression file({http.request.uri.path}, '/index.php')
|
|
// expression file({'root': '/srv', 'try_files': [{http.request.uri.path}, '/index.php'], 'try_policy': 'first_exist', 'split_path': ['.php']})
|
|
func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
|
|
requestType := cel.ObjectType("http.Request")
|
|
|
|
matcherFactory := func(data ref.Val) (caddyhttp.RequestMatcherWithError, error) {
|
|
values, err := caddyhttp.CELValueToMapStrList(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var root string
|
|
if len(values["root"]) > 0 {
|
|
root = values["root"][0]
|
|
}
|
|
|
|
var fsName string
|
|
if len(values["fs"]) > 0 {
|
|
fsName = values["fs"][0]
|
|
}
|
|
|
|
var try_policy string
|
|
if len(values["try_policy"]) > 0 {
|
|
try_policy = values["try_policy"][0]
|
|
}
|
|
|
|
m := MatchFile{
|
|
Root: root,
|
|
TryFiles: values["try_files"],
|
|
TryPolicy: try_policy,
|
|
SplitPath: values["split_path"],
|
|
FileSystem: fsName,
|
|
}
|
|
|
|
err = m.Provision(ctx)
|
|
return m, err
|
|
}
|
|
|
|
envOptions := []cel.EnvOption{
|
|
cel.Macros(parser.NewGlobalVarArgMacro("file", celFileMatcherMacroExpander())),
|
|
cel.Function("file", cel.Overload("file_request_map", []*cel.Type{requestType, caddyhttp.CELTypeJSON}, cel.BoolType)),
|
|
cel.Function("file_request_map",
|
|
cel.Overload("file_request_map", []*cel.Type{requestType, caddyhttp.CELTypeJSON}, cel.BoolType),
|
|
cel.SingletonBinaryBinding(caddyhttp.CELMatcherRuntimeFunction("file_request_map", matcherFactory))),
|
|
}
|
|
|
|
programOptions := []cel.ProgramOption{
|
|
cel.CustomDecorator(caddyhttp.CELMatcherDecorator("file_request_map", matcherFactory)),
|
|
}
|
|
|
|
return caddyhttp.NewMatcherCELLibrary(envOptions, programOptions), nil
|
|
}
|
|
|
|
func celFileMatcherMacroExpander() parser.MacroExpander {
|
|
return func(eh parser.ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
|
|
if len(args) == 0 {
|
|
return eh.NewCall("file",
|
|
eh.NewIdent(caddyhttp.CELRequestVarName),
|
|
eh.NewMap(),
|
|
), nil
|
|
}
|
|
if len(args) == 1 {
|
|
arg := args[0]
|
|
if isCELStringLiteral(arg) || isCELCaddyPlaceholderCall(arg) {
|
|
return eh.NewCall("file",
|
|
eh.NewIdent(caddyhttp.CELRequestVarName),
|
|
eh.NewMap(eh.NewMapEntry(
|
|
eh.NewLiteral(types.String("try_files")),
|
|
eh.NewList(arg),
|
|
false,
|
|
)),
|
|
), nil
|
|
}
|
|
if isCELTryFilesLiteral(arg) {
|
|
return eh.NewCall("file", eh.NewIdent(caddyhttp.CELRequestVarName), arg), nil
|
|
}
|
|
return nil, &common.Error{
|
|
Location: eh.OffsetLocation(arg.ID()),
|
|
Message: "matcher requires either a map or string literal argument",
|
|
}
|
|
}
|
|
|
|
for _, arg := range args {
|
|
if !isCELStringLiteral(arg) && !isCELCaddyPlaceholderCall(arg) {
|
|
return nil, &common.Error{
|
|
Location: eh.OffsetLocation(arg.ID()),
|
|
Message: "matcher only supports repeated string literal arguments",
|
|
}
|
|
}
|
|
}
|
|
return eh.NewCall("file",
|
|
eh.NewIdent(caddyhttp.CELRequestVarName),
|
|
eh.NewMap(eh.NewMapEntry(
|
|
eh.NewLiteral(types.String("try_files")),
|
|
eh.NewList(args...),
|
|
false,
|
|
)),
|
|
), nil
|
|
}
|
|
}
|
|
|
|
// Provision sets up m's defaults.
|
|
func (m *MatchFile) Provision(ctx caddy.Context) error {
|
|
m.logger = ctx.Logger()
|
|
|
|
m.fsmap = ctx.FileSystems()
|
|
|
|
if m.Root == "" {
|
|
m.Root = "{http.vars.root}"
|
|
}
|
|
|
|
if m.FileSystem == "" {
|
|
m.FileSystem = "{http.vars.fs}"
|
|
}
|
|
|
|
// if list of files to try was omitted entirely, assume URL path
|
|
// (use placeholder instead of r.URL.Path; see issue #4146)
|
|
if m.TryFiles == nil {
|
|
m.TryFiles = []string{"{http.request.uri.path}"}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Validate ensures m has a valid configuration.
|
|
func (m MatchFile) Validate() error {
|
|
switch m.TryPolicy {
|
|
case "",
|
|
tryPolicyFirstExist,
|
|
tryPolicyFirstExistFallback,
|
|
tryPolicyLargestSize,
|
|
tryPolicySmallestSize,
|
|
tryPolicyMostRecentlyMod:
|
|
default:
|
|
return fmt.Errorf("unknown try policy %s", m.TryPolicy)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Match returns true if r matches m. Returns true
|
|
// if a file was matched. If so, four placeholders
|
|
// will be available:
|
|
// - http.matchers.file.relative: Path to file relative to site root
|
|
// - http.matchers.file.absolute: Path to file including site root
|
|
// - http.matchers.file.type: file or directory
|
|
// - http.matchers.file.remainder: Portion remaining after splitting file path (if configured)
|
|
func (m MatchFile) Match(r *http.Request) bool {
|
|
match, err := m.selectFile(r)
|
|
if err != nil {
|
|
// nolint:staticcheck
|
|
caddyhttp.SetVar(r.Context(), caddyhttp.MatcherErrorVarKey, err)
|
|
}
|
|
return match
|
|
}
|
|
|
|
// MatchWithError returns true if r matches m.
|
|
func (m MatchFile) MatchWithError(r *http.Request) (bool, error) {
|
|
return m.selectFile(r)
|
|
}
|
|
|
|
// selectFile chooses a file according to m.TryPolicy by appending
|
|
// the paths in m.TryFiles to m.Root, with placeholder replacements.
|
|
func (m MatchFile) selectFile(r *http.Request) (bool, error) {
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
|
|
|
root := filepath.Clean(repl.ReplaceAll(m.Root, "."))
|
|
|
|
fsName := repl.ReplaceAll(m.FileSystem, "")
|
|
|
|
fileSystem, ok := m.fsmap.Get(fsName)
|
|
if !ok {
|
|
if c := m.logger.Check(zapcore.ErrorLevel, "use of unregistered filesystem"); c != nil {
|
|
c.Write(zap.String("fs", fsName))
|
|
}
|
|
return false, nil
|
|
}
|
|
type matchCandidate struct {
|
|
fullpath, relative, splitRemainder string
|
|
}
|
|
|
|
// makeCandidates evaluates placeholders in file and expands any glob expressions
|
|
// to build a list of file candidates. Special glob characters are escaped in
|
|
// placeholder replacements so globs cannot be expanded from placeholders, and
|
|
// globs are not evaluated on Windows because of its path separator character:
|
|
// escaping is not supported so we can't safely glob on Windows, or we can't
|
|
// support placeholders on Windows (pick one). (Actually, evaluating untrusted
|
|
// globs is not the end of the world since the file server will still hide any
|
|
// hidden files, it just might lead to unexpected behavior.)
|
|
makeCandidates := func(file string) []matchCandidate {
|
|
// first, evaluate placeholders in the file pattern
|
|
expandedFile, err := repl.ReplaceFunc(file, func(variable string, val any) (any, error) {
|
|
if runtime.GOOS == "windows" {
|
|
return val, nil
|
|
}
|
|
switch v := val.(type) {
|
|
case string:
|
|
return globSafeRepl.Replace(v), nil
|
|
case fmt.Stringer:
|
|
return globSafeRepl.Replace(v.String()), nil
|
|
}
|
|
return val, nil
|
|
})
|
|
if err != nil {
|
|
if c := m.logger.Check(zapcore.ErrorLevel, "evaluating placeholders"); c != nil {
|
|
c.Write(zap.Error(err))
|
|
}
|
|
|
|
expandedFile = file // "oh well," I guess?
|
|
}
|
|
|
|
// clean the path and split, if configured -- we must split before
|
|
// globbing so that the file system doesn't include the remainder
|
|
// ("afterSplit") in the filename; be sure to restore trailing slash
|
|
beforeSplit, afterSplit := m.firstSplit(path.Clean(expandedFile))
|
|
if strings.HasSuffix(file, "/") {
|
|
beforeSplit += "/"
|
|
}
|
|
|
|
// create the full path to the file by prepending the site root
|
|
fullPattern := caddyhttp.SanitizedPathJoin(root, beforeSplit)
|
|
|
|
// expand glob expressions, but not on Windows because Glob() doesn't
|
|
// support escaping on Windows due to path separator)
|
|
var globResults []string
|
|
if runtime.GOOS == "windows" {
|
|
globResults = []string{fullPattern} // precious Windows
|
|
} else {
|
|
globResults, err = fs.Glob(fileSystem, fullPattern)
|
|
if err != nil {
|
|
if c := m.logger.Check(zapcore.ErrorLevel, "expanding glob"); c != nil {
|
|
c.Write(zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
// for each glob result, combine all the forms of the path
|
|
candidates := make([]matchCandidate, 0, len(globResults))
|
|
for _, result := range globResults {
|
|
candidates = append(candidates, matchCandidate{
|
|
fullpath: result,
|
|
relative: strings.TrimPrefix(result, root),
|
|
splitRemainder: afterSplit,
|
|
})
|
|
}
|
|
|
|
return candidates
|
|
}
|
|
|
|
// makeLiteralCandidate builds a candidate from file as a literal path,
|
|
// without expanding globs or checking that the file exists. It is used only
|
|
// for the final entry of first_exist_fallback, which matches unconditionally
|
|
// even when globbing found nothing because the file is absent.
|
|
makeLiteralCandidate := func(file string) matchCandidate {
|
|
// placeholders are not glob-escaped here, since nothing is globbed;
|
|
// errors were already logged by makeCandidates for the same pattern
|
|
expandedFile, err := repl.ReplaceFunc(file, func(_ string, val any) (any, error) {
|
|
return val, nil
|
|
})
|
|
if err != nil {
|
|
expandedFile = file
|
|
}
|
|
beforeSplit, afterSplit := m.firstSplit(path.Clean(expandedFile))
|
|
if strings.HasSuffix(file, "/") {
|
|
beforeSplit += "/"
|
|
}
|
|
fullPath := caddyhttp.SanitizedPathJoin(root, beforeSplit)
|
|
return matchCandidate{
|
|
fullpath: fullPath,
|
|
relative: strings.TrimPrefix(fullPath, root),
|
|
splitRemainder: afterSplit,
|
|
}
|
|
}
|
|
|
|
// setPlaceholders creates the placeholders for the matched file
|
|
setPlaceholders := func(candidate matchCandidate, isDir bool) {
|
|
repl.Set("http.matchers.file.relative", filepath.ToSlash(candidate.relative))
|
|
repl.Set("http.matchers.file.absolute", filepath.ToSlash(candidate.fullpath))
|
|
repl.Set("http.matchers.file.remainder", filepath.ToSlash(candidate.splitRemainder))
|
|
|
|
fileType := "file"
|
|
if isDir {
|
|
fileType = "directory"
|
|
}
|
|
repl.Set("http.matchers.file.type", fileType)
|
|
}
|
|
|
|
// match file according to the configured policy
|
|
switch m.TryPolicy {
|
|
case "", tryPolicyFirstExist, tryPolicyFirstExistFallback:
|
|
maxI := -1
|
|
if m.TryPolicy == tryPolicyFirstExistFallback {
|
|
maxI = len(m.TryFiles) - 1
|
|
}
|
|
|
|
for i, pattern := range m.TryFiles {
|
|
// If the pattern is a status code, emit an error,
|
|
// which short-circuits the middleware pipeline and
|
|
// writes an HTTP error response.
|
|
if err := parseErrorCode(pattern); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
candidates := makeCandidates(pattern)
|
|
// globbing finds only files that exist, but the fallback entry matches anyway
|
|
if i == maxI && len(candidates) == 0 {
|
|
candidates = []matchCandidate{makeLiteralCandidate(pattern)}
|
|
}
|
|
for _, c := range candidates {
|
|
// Skip the IO if using fallback policy and it's the latest item
|
|
if i == maxI {
|
|
setPlaceholders(c, false)
|
|
|
|
return true, nil
|
|
}
|
|
|
|
if info, exists := m.strictFileExists(fileSystem, c.fullpath); exists {
|
|
setPlaceholders(c, info.IsDir())
|
|
return true, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
case tryPolicyLargestSize:
|
|
var largestSize int64
|
|
var largest matchCandidate
|
|
var largestInfo os.FileInfo
|
|
for _, pattern := range m.TryFiles {
|
|
candidates := makeCandidates(pattern)
|
|
for _, c := range candidates {
|
|
info, err := fs.Stat(fileSystem, c.fullpath)
|
|
if err == nil && info.Size() > largestSize {
|
|
largestSize = info.Size()
|
|
largest = c
|
|
largestInfo = info
|
|
}
|
|
}
|
|
}
|
|
if largestInfo == nil {
|
|
return false, nil
|
|
}
|
|
setPlaceholders(largest, largestInfo.IsDir())
|
|
return true, nil
|
|
|
|
case tryPolicySmallestSize:
|
|
var smallestSize int64
|
|
var smallest matchCandidate
|
|
var smallestInfo os.FileInfo
|
|
for _, pattern := range m.TryFiles {
|
|
candidates := makeCandidates(pattern)
|
|
for _, c := range candidates {
|
|
info, err := fs.Stat(fileSystem, c.fullpath)
|
|
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
|
smallestSize = info.Size()
|
|
smallest = c
|
|
smallestInfo = info
|
|
}
|
|
}
|
|
}
|
|
if smallestInfo == nil {
|
|
return false, nil
|
|
}
|
|
setPlaceholders(smallest, smallestInfo.IsDir())
|
|
return true, nil
|
|
|
|
case tryPolicyMostRecentlyMod:
|
|
var recent matchCandidate
|
|
var recentInfo os.FileInfo
|
|
for _, pattern := range m.TryFiles {
|
|
candidates := makeCandidates(pattern)
|
|
for _, c := range candidates {
|
|
info, err := fs.Stat(fileSystem, c.fullpath)
|
|
if err == nil &&
|
|
(recentInfo == nil || info.ModTime().After(recentInfo.ModTime())) {
|
|
recent = c
|
|
recentInfo = info
|
|
}
|
|
}
|
|
}
|
|
if recentInfo == nil {
|
|
return false, nil
|
|
}
|
|
setPlaceholders(recent, recentInfo.IsDir())
|
|
return true, nil
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// parseErrorCode checks if the input is a status
|
|
// code number, prefixed by "=", and returns an
|
|
// error if so.
|
|
func parseErrorCode(input string) error {
|
|
if len(input) > 1 && input[0] == '=' {
|
|
code, err := strconv.Atoi(input[1:])
|
|
if err != nil || code < 100 || code > 999 {
|
|
return nil
|
|
}
|
|
return caddyhttp.Error(code, fmt.Errorf("%s", input[1:]))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// strictFileExists returns true if file exists
|
|
// and matches the convention of the given file
|
|
// path. If the path ends in a forward slash,
|
|
// the file must also be a directory; if it does
|
|
// NOT end in a forward slash, the file must NOT
|
|
// be a directory.
|
|
func (m MatchFile) strictFileExists(fileSystem fs.FS, file string) (os.FileInfo, bool) {
|
|
info, err := fs.Stat(fileSystem, file)
|
|
if err != nil {
|
|
// in reality, this can be any error
|
|
// such as permission or even obscure
|
|
// ones like "is not a directory" (when
|
|
// trying to stat a file within a file);
|
|
// in those cases we can't be sure if
|
|
// the file exists, so we just treat any
|
|
// error as if it does not exist; see
|
|
// https://stackoverflow.com/a/12518877/1048862
|
|
return nil, false
|
|
}
|
|
if strings.HasSuffix(file, separator) {
|
|
// by convention, file paths ending
|
|
// in a path separator must be a directory
|
|
return info, info.IsDir()
|
|
}
|
|
// by convention, file paths NOT ending
|
|
// in a path separator must NOT be a directory
|
|
return info, !info.IsDir()
|
|
}
|
|
|
|
// firstSplit returns the first result where the path
|
|
// can be split in two by a value in m.SplitPath. The
|
|
// return values are the first piece of the path that
|
|
// ends with the split substring and the remainder.
|
|
// If the path cannot be split, the path is returned
|
|
// as-is (with no remainder).
|
|
func (m MatchFile) firstSplit(path string) (splitPart, remainder string) {
|
|
for _, split := range m.SplitPath {
|
|
if idx := indexFold(path, split); idx > -1 {
|
|
pos := idx + len(split)
|
|
// skip the split if it's not the final part of the filename
|
|
if pos != len(path) && !strings.HasPrefix(path[pos:], "/") {
|
|
continue
|
|
}
|
|
return path[:pos], path[pos:]
|
|
}
|
|
}
|
|
return path, ""
|
|
}
|
|
|
|
// There is no strings.IndexFold() function like there is strings.EqualFold(),
|
|
// but we can use strings.EqualFold() to build our own case-insensitive
|
|
// substring search (as of Go 1.14).
|
|
func indexFold(haystack, needle string) int {
|
|
nlen := len(needle)
|
|
for i := 0; i+nlen < len(haystack); i++ {
|
|
if strings.EqualFold(haystack[i:i+nlen], needle) {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// isCELTryFilesLiteral returns whether the expression resolves to a map literal containing
|
|
// only string keys with or a placeholder call.
|
|
func isCELTryFilesLiteral(e ast.Expr) bool {
|
|
switch e.Kind() {
|
|
case ast.MapKind:
|
|
mapExpr := e.AsMap()
|
|
for _, entry := range mapExpr.Entries() {
|
|
mapKey := entry.AsMapEntry().Key()
|
|
mapVal := entry.AsMapEntry().Value()
|
|
if !isCELStringLiteral(mapKey) {
|
|
return false
|
|
}
|
|
mapKeyStr := mapKey.AsLiteral().ConvertToType(types.StringType).Value()
|
|
switch mapKeyStr {
|
|
case "try_files", "split_path":
|
|
if !isCELStringListLiteral(mapVal) {
|
|
return false
|
|
}
|
|
case "try_policy", "root":
|
|
if !isCELStringExpr(mapVal) {
|
|
return false
|
|
}
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
|
|
case ast.UnspecifiedExprKind, ast.CallKind, ast.ComprehensionKind, ast.IdentKind, ast.ListKind, ast.LiteralKind, ast.SelectKind, ast.StructKind:
|
|
// appeasing the linter :)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isCELStringExpr indicates whether the expression is a supported string expression
|
|
func isCELStringExpr(e ast.Expr) bool {
|
|
return isCELStringLiteral(e) || isCELCaddyPlaceholderCall(e) || isCELConcatCall(e)
|
|
}
|
|
|
|
// isCELStringLiteral returns whether the expression is a CEL string literal.
|
|
func isCELStringLiteral(e ast.Expr) bool {
|
|
switch e.Kind() {
|
|
case ast.LiteralKind:
|
|
constant := e.AsLiteral()
|
|
switch constant.Type() {
|
|
case types.StringType:
|
|
return true
|
|
}
|
|
case ast.UnspecifiedExprKind, ast.CallKind, ast.ComprehensionKind, ast.IdentKind, ast.ListKind, ast.MapKind, ast.SelectKind, ast.StructKind:
|
|
// appeasing the linter :)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isCELCaddyPlaceholderCall returns whether the expression is a caddy placeholder call.
|
|
func isCELCaddyPlaceholderCall(e ast.Expr) bool {
|
|
switch e.Kind() {
|
|
case ast.CallKind:
|
|
call := e.AsCall()
|
|
if call.FunctionName() == caddyhttp.CELPlaceholderFuncName {
|
|
return true
|
|
}
|
|
case ast.UnspecifiedExprKind, ast.ComprehensionKind, ast.IdentKind, ast.ListKind, ast.LiteralKind, ast.MapKind, ast.SelectKind, ast.StructKind:
|
|
// appeasing the linter :)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isCELConcatCall tests whether the expression is a concat function (+) with string, placeholder, or
|
|
// other concat call arguments.
|
|
func isCELConcatCall(e ast.Expr) bool {
|
|
switch e.Kind() {
|
|
case ast.CallKind:
|
|
call := e.AsCall()
|
|
if call.Target().Kind() != ast.UnspecifiedExprKind {
|
|
return false
|
|
}
|
|
if call.FunctionName() != operators.Add {
|
|
return false
|
|
}
|
|
for _, arg := range call.Args() {
|
|
if !isCELStringExpr(arg) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case ast.UnspecifiedExprKind, ast.ComprehensionKind, ast.IdentKind, ast.ListKind, ast.LiteralKind, ast.MapKind, ast.SelectKind, ast.StructKind:
|
|
// appeasing the linter :)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isCELStringListLiteral returns whether the expression resolves to a list literal
|
|
// containing only string constants or a placeholder call.
|
|
func isCELStringListLiteral(e ast.Expr) bool {
|
|
switch e.Kind() {
|
|
case ast.ListKind:
|
|
list := e.AsList()
|
|
for _, elem := range list.Elements() {
|
|
if !isCELStringExpr(elem) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case ast.UnspecifiedExprKind, ast.CallKind, ast.ComprehensionKind, ast.IdentKind, ast.LiteralKind, ast.MapKind, ast.SelectKind, ast.StructKind:
|
|
// appeasing the linter :)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// globSafeRepl replaces special glob characters with escaped
|
|
// equivalents. Note that the filepath godoc states that
|
|
// escaping is not done on Windows because of the separator.
|
|
var globSafeRepl = strings.NewReplacer(
|
|
"*", "\\*",
|
|
"[", "\\[",
|
|
"?", "\\?",
|
|
"\\", "\\\\",
|
|
)
|
|
|
|
const (
|
|
tryPolicyFirstExist = "first_exist"
|
|
tryPolicyFirstExistFallback = "first_exist_fallback"
|
|
tryPolicyLargestSize = "largest_size"
|
|
tryPolicySmallestSize = "smallest_size"
|
|
tryPolicyMostRecentlyMod = "most_recently_modified"
|
|
)
|
|
|
|
// Interface guards
|
|
var (
|
|
_ caddy.Validator = (*MatchFile)(nil)
|
|
_ caddyhttp.RequestMatcherWithError = (*MatchFile)(nil)
|
|
_ caddyhttp.CELLibraryProducer = (*MatchFile)(nil)
|
|
)
|