mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-14 14:56:27 +03:00
map: Prevent output destinations overlap with Caddyfile shorthands (#4657)
This commit is contained in:
parent
f6900fcf53
commit
f7be0ee101
2 changed files with 61 additions and 28 deletions
|
@ -88,34 +88,10 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace shorthand placeholders (which are
|
// replace shorthand placeholders (which are convenient
|
||||||
// convenient when writing a Caddyfile) with
|
// when writing a Caddyfile) with their actual placeholder
|
||||||
// their actual placeholder identifiers or
|
// identifiers or variable names
|
||||||
// variable names
|
replacer := strings.NewReplacer(placeholderShorthands()...)
|
||||||
replacer := strings.NewReplacer(
|
|
||||||
"{dir}", "{http.request.uri.path.dir}",
|
|
||||||
"{file}", "{http.request.uri.path.file}",
|
|
||||||
"{host}", "{http.request.host}",
|
|
||||||
"{hostport}", "{http.request.hostport}",
|
|
||||||
"{port}", "{http.request.port}",
|
|
||||||
"{method}", "{http.request.method}",
|
|
||||||
"{path}", "{http.request.uri.path}",
|
|
||||||
"{query}", "{http.request.uri.query}",
|
|
||||||
"{remote}", "{http.request.remote}",
|
|
||||||
"{remote_host}", "{http.request.remote.host}",
|
|
||||||
"{remote_port}", "{http.request.remote.port}",
|
|
||||||
"{scheme}", "{http.request.scheme}",
|
|
||||||
"{uri}", "{http.request.uri}",
|
|
||||||
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
|
||||||
"{tls_version}", "{http.request.tls.version}",
|
|
||||||
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
|
||||||
"{tls_client_issuer}", "{http.request.tls.client.issuer}",
|
|
||||||
"{tls_client_serial}", "{http.request.tls.client.serial}",
|
|
||||||
"{tls_client_subject}", "{http.request.tls.client.subject}",
|
|
||||||
"{tls_client_certificate_pem}", "{http.request.tls.client.certificate_pem}",
|
|
||||||
"{tls_client_certificate_der_base64}", "{http.request.tls.client.certificate_der_base64}",
|
|
||||||
"{upstream_hostport}", "{http.reverse_proxy.upstream.hostport}",
|
|
||||||
)
|
|
||||||
|
|
||||||
// these are placeholders that allow a user-defined final
|
// these are placeholders that allow a user-defined final
|
||||||
// parameters, but we still want to provide a shorthand
|
// parameters, but we still want to provide a shorthand
|
||||||
|
@ -1265,6 +1241,58 @@ func encodeMatcherSet(matchers map[string]caddyhttp.RequestMatcher) (caddy.Modul
|
||||||
return msEncoded, nil
|
return msEncoded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// placeholderShorthands returns a slice of old-new string pairs,
|
||||||
|
// where the left of the pair is a placeholder shorthand that may
|
||||||
|
// be used in the Caddyfile, and the right is the replacement.
|
||||||
|
func placeholderShorthands() []string {
|
||||||
|
return []string{
|
||||||
|
"{dir}", "{http.request.uri.path.dir}",
|
||||||
|
"{file}", "{http.request.uri.path.file}",
|
||||||
|
"{host}", "{http.request.host}",
|
||||||
|
"{hostport}", "{http.request.hostport}",
|
||||||
|
"{port}", "{http.request.port}",
|
||||||
|
"{method}", "{http.request.method}",
|
||||||
|
"{path}", "{http.request.uri.path}",
|
||||||
|
"{query}", "{http.request.uri.query}",
|
||||||
|
"{remote}", "{http.request.remote}",
|
||||||
|
"{remote_host}", "{http.request.remote.host}",
|
||||||
|
"{remote_port}", "{http.request.remote.port}",
|
||||||
|
"{scheme}", "{http.request.scheme}",
|
||||||
|
"{uri}", "{http.request.uri}",
|
||||||
|
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
||||||
|
"{tls_version}", "{http.request.tls.version}",
|
||||||
|
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
||||||
|
"{tls_client_issuer}", "{http.request.tls.client.issuer}",
|
||||||
|
"{tls_client_serial}", "{http.request.tls.client.serial}",
|
||||||
|
"{tls_client_subject}", "{http.request.tls.client.subject}",
|
||||||
|
"{tls_client_certificate_pem}", "{http.request.tls.client.certificate_pem}",
|
||||||
|
"{tls_client_certificate_der_base64}", "{http.request.tls.client.certificate_der_base64}",
|
||||||
|
"{upstream_hostport}", "{http.reverse_proxy.upstream.hostport}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WasReplacedPlaceholderShorthand checks if a token string was
|
||||||
|
// likely a replaced shorthand of the known Caddyfile placeholder
|
||||||
|
// replacement outputs. Useful to prevent some user-defined map
|
||||||
|
// output destinations from overlapping with one of the
|
||||||
|
// predefined shorthands.
|
||||||
|
func WasReplacedPlaceholderShorthand(token string) string {
|
||||||
|
prev := ""
|
||||||
|
for i, item := range placeholderShorthands() {
|
||||||
|
// only look at every 2nd item, which is the replacement
|
||||||
|
if i%2 == 0 {
|
||||||
|
prev = item
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Trim(token, "{}") == strings.Trim(item, "{}") {
|
||||||
|
// we return the original shorthand so it
|
||||||
|
// can be used for an error message
|
||||||
|
return prev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// tryInt tries to convert val to an integer. If it fails,
|
// tryInt tries to convert val to an integer. If it fails,
|
||||||
// it downgrades the error to a warning and returns 0.
|
// it downgrades the error to a warning and returns 0.
|
||||||
func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
|
func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
|
||||||
|
|
|
@ -56,6 +56,11 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
if len(handler.Destinations) == 0 {
|
if len(handler.Destinations) == 0 {
|
||||||
return nil, h.Err("missing destination argument(s)")
|
return nil, h.Err("missing destination argument(s)")
|
||||||
}
|
}
|
||||||
|
for _, dest := range handler.Destinations {
|
||||||
|
if shorthand := httpcaddyfile.WasReplacedPlaceholderShorthand(dest); shorthand != "" {
|
||||||
|
return nil, h.Errf("destination %s conflicts with a Caddyfile placeholder shorthand", shorthand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// mappings
|
// mappings
|
||||||
for h.NextBlock(0) {
|
for h.NextBlock(0) {
|
||||||
|
|
Loading…
Reference in a new issue