csp

package
v0.1.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HashAlgoSha256 = "sha256"
	HashAlgoSha384 = "sha384"
	HashAlgoSha512 = "sha512"
)

哈希算法类型

View Source
const (
	// 获取指令
	DirectiveDefaultSrc    = "default-src"
	DirectiveScriptSrc     = "script-src"
	DirectiveScriptSrcElem = "script-src-elem"
	DirectiveScriptSrcAttr = "script-src-attr"
	DirectiveStyleSrc      = "style-src"
	DirectiveStyleSrcElem  = "style-src-elem"
	DirectiveStyleSrcAttr  = "style-src-attr"
	DirectiveImgSrc        = "img-src"
	DirectiveConnectSrc    = "connect-src"
	DirectiveFontSrc       = "font-src"
	DirectiveObjectSrc     = "object-src"
	DirectiveMediaSrc      = "media-src"
	DirectiveFrameSrc      = "frame-src"
	DirectiveWorkerSrc     = "worker-src"
	DirectiveManifestSrc   = "manifest-src"
	DirectiveChildSrc      = "child-src"
	DirectivePrefetchSrc   = "prefetch-src"

	// 文档指令
	DirectiveBaseURI        = "base-uri"
	DirectiveSandbox        = "sandbox"
	DirectiveFormAction     = "form-action"
	DirectiveFrameAncestors = "frame-ancestors"
	DirectiveNavigateTo     = "navigate-to"

	// 报告指令
	DirectiveReportURI = "report-uri"
	DirectiveReportTo  = "report-to"

	// 其他指令
	DirectiveRequireSriFor           = "require-sri-for"
	DirectiveUpgradeInsecureRequests = "upgrade-insecure-requests"
	DirectiveBlockAllMixedContent    = "block-all-mixed-content"
	DirectiveTrustedTypes            = "trusted-types"
	DirectiveTreatAsPublicAddress    = "treat-as-public-address"
)

CSP指令类型

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

CSPBuilder 用于构建内容安全策略的生成器

func CSPBasic

func CSPBasic(nonce string) *Builder

CSPBasic 返回基本的CSP策略

func CSPModern

func CSPModern(nonce string) *Builder

CSPModern 返回适合现代Web应用的CSP策略

func CSPStrict

func CSPStrict(nonce string) *Builder

CSPStrict 返回严格的CSP策略

func NewBuilder

func NewBuilder() *Builder

NewBuilder 创建新的CSP生成器

func (*Builder) Add

func (b *Builder) Add(directive string, values ...string) *Builder

Add 添加内容安全策略指令

func (*Builder) AddHash

func (b *Builder) AddHash(directive, content, algorithm string) *Builder

AddHash 添加内容哈希

func (*Builder) AddNonce

func (b *Builder) AddNonce(directive string) *Builder

AddNonce 添加nonce到指定指令

func (*Builder) AddNonceToScriptAndStyle

func (b *Builder) AddNonceToScriptAndStyle() *Builder

AddNonceToScriptAndStyle 添加nonce到脚本和样式指令

func (*Builder) AddReporting

func (b *Builder) AddReporting(endpoint string) *Builder

AddReporting 添加报告配置

func (*Builder) AddStrictDynamic

func (b *Builder) AddStrictDynamic() *Builder

AddStrictDynamic 添加strict-dynamic到脚本指令

func (*Builder) AllowUnsafeEval

func (b *Builder) AllowUnsafeEval(directive string) *Builder

AllowUnsafeEval 添加unsafe-eval到指令

func (*Builder) AllowUnsafeInline

func (b *Builder) AllowUnsafeInline(directive string) *Builder

AllowUnsafeInline 添加unsafe-inline到指令

func (*Builder) BlockAllMixedContent

func (b *Builder) BlockAllMixedContent() *Builder

BlockAllMixedContent 阻止所有混合内容

func (*Builder) EnableUpgradeInsecureRequests

func (b *Builder) EnableUpgradeInsecureRequests() *Builder

EnableUpgradeInsecureRequests 启用升级不安全请求

func (*Builder) RequireSRI

func (b *Builder) RequireSRI(directive string, require bool) *Builder

RequireSRI 为特定指令要求使用SRI

func (*Builder) SetNonce

func (b *Builder) SetNonce(nonce string) *Builder

SetNonce 设置全局nonce值

func (*Builder) SetReportEndpoint

func (b *Builder) SetReportEndpoint(endpoint string) *Builder

SetReportEndpoint 设置违规报告端点

func (*Builder) SetReportOnly

func (b *Builder) SetReportOnly(reportOnly bool) *Builder

SetReportOnly 设置CSP为仅报告模式

func (*Builder) String

func (b *Builder) String() string

String 生成内容安全策略字符串

func (*Builder) ToHeader

func (b *Builder) ToHeader() (string, string)

ToHeader 返回CSP头名称和值

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL