mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
|
"github.com/hashicorp/vault/vault"
|
|
)
|
|
|
|
var allowedMethods = []string{
|
|
http.MethodDelete,
|
|
http.MethodGet,
|
|
http.MethodOptions,
|
|
http.MethodPost,
|
|
http.MethodPut,
|
|
"LIST", // LIST is not an official HTTP method, but Vault supports it.
|
|
}
|
|
|
|
func wrapCORSHandler(h http.Handler, core *vault.Core) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
corsConf := core.CORSConfig()
|
|
|
|
// If CORS is not enabled or if no Origin header is present (i.e. the request
|
|
// is from the Vault CLI. A browser will always send an Origin header), then
|
|
// just return a 204.
|
|
if !corsConf.IsEnabled() {
|
|
h.ServeHTTP(w, req)
|
|
return
|
|
}
|
|
|
|
origin := req.Header.Get("Origin")
|
|
requestMethod := req.Header.Get("Access-Control-Request-Method")
|
|
|
|
if origin == "" {
|
|
h.ServeHTTP(w, req)
|
|
return
|
|
}
|
|
|
|
// Return a 403 if the origin is not allowed to make cross-origin requests.
|
|
if !corsConf.IsValidOrigin(origin) {
|
|
respondError(w, http.StatusForbidden, fmt.Errorf("origin not allowed"))
|
|
return
|
|
}
|
|
|
|
if req.Method == http.MethodOptions && !strutil.StrListContains(allowedMethods, requestMethod) {
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
w.Header().Set("Vary", "Origin")
|
|
|
|
// apply headers for preflight requests
|
|
if req.Method == http.MethodOptions {
|
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allowedMethods, ","))
|
|
w.Header().Set("Access-Control-Allow-Headers", strings.Join(corsConf.AllowedHeaders, ","))
|
|
w.Header().Set("Access-Control-Max-Age", "300")
|
|
|
|
return
|
|
}
|
|
|
|
h.ServeHTTP(w, req)
|
|
return
|
|
})
|
|
}
|