Skip to content

Commit

Permalink
docs: refine secure.go configuration handling
Browse files Browse the repository at this point in the history
- Add comments to the `Config` struct in `secure.go`
- Insert a new line in `secure_test.go` without additional content
- Remove an empty line from `secure_test.go`

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed May 5, 2024
1 parent e944b63 commit f6e7c81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *policy) loadConfig(config Config) {
if config.STSIncludeSubdomains {
stsSub = "; includeSubdomains"
}
if config.STSPreload {
if config.STSPreload {
stsSub = "; preload"
}
// TODO
Expand Down
32 changes: 17 additions & 15 deletions secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/gin-gonic/gin"
// Config is a struct for specifying configuration options for the secure.
type Config struct {
// AllowedHosts is a list of fully qualified domain names that are allowed.
//Default is empty list, which allows any and all host names.
// Default is empty list, which allows any and all host names.
AllowedHosts []string
// If SSLRedirect is set to true, then only allow https requests.
// Default is false.
Expand All @@ -22,11 +22,11 @@ type Config struct {
// If STSIncludeSubdomains is set to true, the `includeSubdomains` will
// be appended to the Strict-Transport-Security header. Default is false.
STSIncludeSubdomains bool
// If STSPreload is set to true, the `; preload` will be appended to the
// Strict-Transport-Security header. Default is false.
// Note that removal is non-trivial and enabling this means you need to
// support https long-term. See https://hstspreload.org/ for more info.
STSPreload bool
// If STSPreload is set to true, the `; preload` will be appended to the
// Strict-Transport-Security header. Default is false.
// Note that removal is non-trivial and enabling this means you need to
// support https long-term. See https://hstspreload.org/ for more info.
STSPreload bool
// If FrameDeny is set to true, adds the X-Frame-Options header with
// the value of `DENY`. Default is false.
FrameDeny bool
Expand Down Expand Up @@ -64,15 +64,17 @@ type Config struct {

// DefaultConfig returns a Configuration with strict security settings.
// ```
// SSLRedirect: true
// IsDevelopment: false
// STSSeconds: 315360000
// STSIncludeSubdomains: true
// FrameDeny: true
// ContentTypeNosniff: true
// BrowserXssFilter: true
// ContentSecurityPolicy: "default-src 'self'"
// SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
//
// SSLRedirect: true
// IsDevelopment: false
// STSSeconds: 315360000
// STSIncludeSubdomains: true
// FrameDeny: true
// ContentTypeNosniff: true
// BrowserXssFilter: true
// ContentSecurityPolicy: "default-src 'self'"
// SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
//
// ```
func DefaultConfig() Config {
return Config{
Expand Down
4 changes: 2 additions & 2 deletions secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func TestBadMultipleAllowHosts(t *testing.T) {

assert.Equal(t, http.StatusForbidden, w.Code)
}

func TestAllowHostsInDevMode(t *testing.T) {
router := newServer(Config{
AllowedHosts: []string{"www.example.com", "sub.example.com"},
Expand All @@ -122,7 +123,6 @@ func TestAllowHostsInDevMode(t *testing.T) {
}

func TestBadHostHandler(t *testing.T) {

badHandler := func(c *gin.Context) {
c.String(http.StatusInternalServerError, "BadHost")
c.Abort()
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestBasicSSL(t *testing.T) {

func TestDontRedirectIPV4Hostnames(t *testing.T) {
router := newServer(Config{
SSLRedirect: true,
SSLRedirect: true,
DontRedirectIPV4Hostnames: true,
})

Expand Down

0 comments on commit f6e7c81

Please sign in to comment.