-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CSP and other security related headers in the oCIS proxy se…
…rvice
- Loading branch information
1 parent
a4cc165
commit 9ae23ee
Showing
20 changed files
with
1,489 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Enhancement: Add CSP and other security related headers to oCIS | ||
|
||
General hardening of oCIS | ||
|
||
https://github.com/owncloud/ocis/pull/8777 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package config | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
// CSP defines CSP header directives | ||
type CSP struct { | ||
Directives map[string][]string `yaml:"directives"` | ||
} | ||
|
||
//go:embed csp.yaml | ||
var DefaultCSPConfig string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
directives: | ||
child-src: | ||
- '''self''' | ||
connect-src: | ||
- '''self''' | ||
default-src: | ||
- '''none''' | ||
font-src: | ||
- '''self''' | ||
frame-src: | ||
- '''self''' | ||
- 'https://embed.diagrams.net/' | ||
img-src: | ||
- '''self''' | ||
- 'data:' | ||
- 'blob:' | ||
manifest-src: | ||
- '''self''' | ||
media-src: | ||
- '''self''' | ||
object-src: | ||
- '''self''' | ||
- 'blob:' | ||
script-src: | ||
- '''self''' | ||
- '''unsafe-inline''' | ||
style-src: | ||
- '''self''' | ||
- '''unsafe-inline''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/owncloud/ocis/v2/services/proxy/pkg/config" | ||
"github.com/unrolled/secure" | ||
"github.com/unrolled/secure/cspbuilder" | ||
"gopkg.in/yaml.v2" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// LoadCSPConfig loads CSP header configuration from a yaml file. | ||
func LoadCSPConfig(proxyCfg *config.Config) (*config.CSP, error) { | ||
yamlContent, err := loadCSPYaml(proxyCfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cspConfig := config.CSP{} | ||
err = yaml.Unmarshal(yamlContent, &cspConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cspConfig, nil | ||
} | ||
|
||
func loadCSPYaml(proxyCfg *config.Config) ([]byte, error) { | ||
if proxyCfg.CSPConfigFileLocation == "" { | ||
return []byte(config.DefaultCSPConfig), nil | ||
} | ||
return os.ReadFile(proxyCfg.CSPConfigFileLocation) | ||
} | ||
|
||
// Security is a middleware to apply security relevant http headers like CSP. | ||
func Security(cspConfig *config.CSP) func(h http.Handler) http.Handler { | ||
cspBuilder := cspbuilder.Builder{ | ||
Directives: cspConfig.Directives, | ||
} | ||
|
||
secureMiddleware := secure.New(secure.Options{ | ||
BrowserXssFilter: true, | ||
ContentSecurityPolicy: cspBuilder.MustBuild(), | ||
ContentTypeNosniff: true, | ||
CustomFrameOptionsValue: "SAMEORIGIN", | ||
ReferrerPolicy: "strict-origin-when-cross-origin", | ||
STSSeconds: 315360000, | ||
STSPreload: true, | ||
}) | ||
return func(next http.Handler) http.Handler { | ||
return secureMiddleware.Handler(next) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.