MoonBit native HTTP security header and CSP audit library
HYF-ai2006/moonsec-headersmoon add HYF-ai2006/moonsec-headersimport {
"HYF-ai2006/moonsec-headers" @headers
}let raw = [
"Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'",
"Strict-Transport-Security: max-age=31536000; includeSubDomains",
"X-Content-Type-Options: nosniff",
"Referrer-Policy: strict-origin-when-cross-origin",
"Permissions-Policy: camera=(), microphone=(), geolocation=()",
].join("\n")
let report = @headers.audit_headers(raw)
println(report.to_markdown())let plan = @headers.static_site_header_plan()
println(plan.to_header_block())
let report = plan.audit()
println(report.render(@headers.PlainText))let spa = @headers.spa_header_plan(
"https://api.example.test",
"https://assets.example.test",
)
let assessment = spa.assess_with_profile("spa-app")moon check
moon build
moon test
moon run cmd/main
moon publish --dry-runmoon check
moon build
moon test
moon run cmd/main
moon publish --dry-runfn AuditReport::render(self : AuditReport, format : ReportFormat, options? : RenderOptions) -> Stringpub(all) struct CspAnalysis {
score : Int
policy : CspPolicy
observations : Array[CspObservation]
directives : Array[CspDirectiveSummary]
sources : Array[CspSourceExpression]
} derive(Eq, Debug)pub(all) struct CspBuilder {
report_only : Bool
directives : Array[CspDirectiveSpec]
} derive(Eq, Debug)fn CspBuilder::with_directive(self : CspBuilder, name : StringView, values : Array[String]) -> CspBuilderpub(all) struct CspDirectiveSummary {
name : String
family : CspDirectiveFamily
explicit : Bool
value_count : Int
source_count : Int
has_none : Bool
has_self : Bool
has_wildcard : Bool
has_http : Bool
has_inline : Bool
has_eval : Bool
has_nonce : Bool
has_hash : Bool
notes : Array[String]
} derive(Eq, Debug)fn CspPolicy::explicit_sources_for(self : CspPolicy, directive : StringView) -> Array[CspSourceExpression]pub(all) struct CspSourceExpression {
directive : String
raw : String
normalized : String
kind : CspSourceKind
secure_transport : Bool
broad : Bool
note : String
} derive(Eq, Debug)pub(all) enum CspSourceKind {
CspSourceNone
CspSourceSelf
CspSourceUnsafeInline
CspSourceUnsafeEval
CspSourceStrictDynamic
CspSourceNonce
CspSourceHash
CspSourceHttpsScheme
CspSourceHttpScheme
CspSourceDataScheme
CspSourceBlobScheme
CspSourceFilesystemScheme
CspSourceWildcard
CspSourceHost
CspSourceKeyword
CspSourceUnknown
} derive(Eq, Debug)pub(all) struct HeaderPlan {
kind : HeaderPlanKind
title : String
scenario : String
headers : Array[HeaderPair]
notes : Array[String]
} derive(Eq, Debug)pub(all) struct HeaderRequirement {
name : String
expected : String
mode : RequirementMode
required : Bool
severity : Severity
rationale : String
} derive(Eq, Debug)pub(all) struct ParseIssue {
kind : ParseIssueKind
line : Int
name : String
message : String
} derive(Eq, Debug)pub(all) struct ProfileAssessment {
profile_key : String
score : Int
matched : Int
missing : Int
weak : Int
conflict : Int
optional : Int
findings : Array[ProfileFinding]
} derive(Eq, Debug)pub(all) struct ProfileFinding {
name : String
status : ProfileStatus
actual : String?
expected : String
severity : Severity
rationale : String
} derive(Eq, Debug)pub(all) struct SecurityProfile {
kind : ProfileKind
key : String
title : String
audience : String
summary : String
requirements : Array[HeaderRequirement]
} derive(Eq, Debug)fn SecurityProfile::find_requirement(self : SecurityProfile, name : StringView) -> HeaderRequirement?pub(all) struct ValidationMessage {
level : ValidationLevel
field : String
message : String
} derive(Eq, Debug)fn csp_navigation_directive(name : StringView) -> Boolfn header_plan(kind : HeaderPlanKind, title : StringView, scenario : StringView, headers : Array[HeaderPair], notes : Array[String]) -> HeaderPlanfn profile(kind : ProfileKind, key : StringView, title : StringView, audience : StringView, summary : StringView, requirements : Array[HeaderRequirement]) -> SecurityProfilefn requirement(name : StringView, expected : StringView, mode? : RequirementMode, required? : Bool, severity? : Severity, rationale? : StringView) -> HeaderRequirementMoonBit native HTTP security header and CSP audit library