Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Set some security related headers #3065

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/middleware/contentsecuritypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func BuildContentSecurityPolicy(extraURIs ...string) string {
objectSrc = "object-src"
imgSrc = "img-src"
mediaSrc = "media-src"
frames = "frame-ancestors"

self = "'self'"
none = "'none'"
Expand Down Expand Up @@ -102,6 +103,14 @@ func BuildContentSecurityPolicy(extraURIs ...string) string {
extraURIs...,
)

/*
frame-ancestors
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
*/

// Don't allow embedding us in an iframe
values[frames] = []string{none}

/*
Assemble policy directives.
*/
Expand Down
9 changes: 9 additions & 0 deletions internal/middleware/extraheaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func ExtraHeaders() gin.HandlerFunc {
// Inform all callers which server implementation this is.
c.Header("Server", "gotosocial")

// Equivalent to CSP frame-ancestors for older browsers
c.Header("X-Frame-Options", "DENY")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caps here don't matter but so many examples go full caps on it. So. DENYed.


// Don't do MIME type sniffing
c.Header("X-Content-Type-Options", "nosniff")

// Only send Referer header for URLs matching our protocol, hostname and port
c.Header("Referrer-Policy", "same-origin")

// Prevent google chrome cohort tracking. Originally this was referred
// to as FlocBlock. Floc was replaced by Topics in 2022 and the spec says
// that interest-cohort will also block Topics (as of 2022-Nov).
Expand Down