mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[feature] Set Content-Security-Policy header (#2095)
This adds the CSP header with a policy of only loading from the same domain. We don't make use of external media, CSS, JS, fonts, so we don't ever need external data loaded in our context. When building a DEBUG build, the policy gets extended to include localhost:*, i.e localhost on any port. This keeps the live-reloading flow for JS development working. localhost and 127.0.0.1 are considered to be the same so mixing and matching those doesn't result in a CSP violation.
This commit is contained in:
@ -17,10 +17,17 @@
|
||||
|
||||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"codeberg.org/gruf/go-debug"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ExtraHeaders returns a new gin middleware which adds various extra headers to the response.
|
||||
func ExtraHeaders() gin.HandlerFunc {
|
||||
policy := "default-src 'self'"
|
||||
if debug.DEBUG {
|
||||
policy += " localhost:*"
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
// Inform all callers which server implementation this is.
|
||||
c.Header("Server", "gotosocial")
|
||||
@ -32,5 +39,7 @@ func ExtraHeaders() gin.HandlerFunc {
|
||||
//
|
||||
// See: https://github.com/patcg-individual-drafts/topics
|
||||
c.Header("Permissions-Policy", "browsing-topics=()")
|
||||
// Inform the browser we only load CSS/JS/media from the same domain
|
||||
c.Header("Content-Security-Policy", policy)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user