Files
opencloud/vendor/github.com/unrolled/secure/csp.go
Christian Richter dd57a53310 technical-debt: move unrolled/secure replacement to own org
Signed-off-by: Christian Richter <c.richter@opencloud.eu>
2026-03-12 11:54:26 +01:00

47 lines
1.1 KiB
Go

package secure
import (
"context"
"crypto/rand"
"encoding/base64"
"io"
"net/http"
)
type key int
const cspNonceKey key = iota
// CSPNonce returns the nonce value associated with the present request.
// If no nonce has been generated it returns an empty string.
func CSPNonce(c context.Context) string {
if val, ok := c.Value(cspNonceKey).(string); ok {
return val
}
return ""
}
// WithCSPNonce returns a context derived from ctx containing the given nonce as a value.
//
// This is intended for testing or more advanced use-cases;
// For ordinary HTTP handlers, clients can rely on this package's middleware to populate the CSP nonce in the context.
func WithCSPNonce(ctx context.Context, nonce string) context.Context {
return context.WithValue(ctx, cspNonceKey, nonce)
}
func withCSPNonce(r *http.Request, nonce string) *http.Request {
return r.WithContext(WithCSPNonce(r.Context(), nonce))
}
func cspRandNonce() string {
var buf [cspNonceSize]byte
_, err := io.ReadFull(rand.Reader, buf[:])
if err != nil {
panic("CSP Nonce rand.Reader failed" + err.Error())
}
return base64.RawStdEncoding.EncodeToString(buf[:])
}