replace zxq.co/ripple/hanayo
This commit is contained in:
10
vendor/github.com/johnniedoe/contrib/secure/Godeps/Godeps.json
generated
vendored
Normal file
10
vendor/github.com/johnniedoe/contrib/secure/Godeps/Godeps.json
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/contrib/secure",
|
||||
"GoVersion": "go1.3",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/gin",
|
||||
"Rev": "ac0ad2fed865d40a0adc1ac3ccaadc3acff5db4b"
|
||||
}
|
||||
]
|
||||
}
|
32
vendor/github.com/johnniedoe/contrib/secure/example/example.go
generated
vendored
Normal file
32
vendor/github.com/johnniedoe/contrib/secure/example/example.go
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/contrib/secure"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(secure.Secure(secure.Options{
|
||||
AllowedHosts: []string{"example.com", "ssl.example.com"},
|
||||
SSLRedirect: true,
|
||||
SSLHost: "ssl.example.com",
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
STSSeconds: 315360000,
|
||||
STSIncludeSubdomains: true,
|
||||
FrameDeny: true,
|
||||
ContentTypeNosniff: true,
|
||||
BrowserXssFilter: true,
|
||||
ContentSecurityPolicy: "default-src 'self'",
|
||||
}))
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
179
vendor/github.com/johnniedoe/contrib/secure/secure.go
generated
vendored
Normal file
179
vendor/github.com/johnniedoe/contrib/secure/secure.go
generated
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
package secure
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
stsHeader = "Strict-Transport-Security"
|
||||
stsSubdomainString = "; includeSubdomains"
|
||||
frameOptionsHeader = "X-Frame-Options"
|
||||
frameOptionsValue = "DENY"
|
||||
contentTypeHeader = "X-Content-Type-Options"
|
||||
contentTypeValue = "nosniff"
|
||||
xssProtectionHeader = "X-XSS-Protection"
|
||||
xssProtectionValue = "1; mode=block"
|
||||
cspHeader = "Content-Security-Policy"
|
||||
)
|
||||
|
||||
func defaultBadHostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Bad Host", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Options is a struct for specifying configuration options for the secure.Secure middleware.
|
||||
type Options struct {
|
||||
// AllowedHosts is a list of fully qualified domain names that are allowed. 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.
|
||||
SSLRedirect bool
|
||||
// If SSLTemporaryRedirect is true, the a 302 will be used while redirecting. Default is false (301).
|
||||
SSLTemporaryRedirect bool
|
||||
// SSLHost is the host name that is used to redirect http requests to https. Default is "", which indicates to use the same host.
|
||||
SSLHost string
|
||||
// SSLProxyHeaders is set of header keys with associated values that would indicate a valid https request. Useful when using Nginx: `map[string]string{"X-Forwarded-Proto": "https"}`. Default is blank map.
|
||||
SSLProxyHeaders map[string]string
|
||||
// STSSeconds is the max-age of the Strict-Transport-Security header. Default is 0, which would NOT include the header.
|
||||
STSSeconds int64
|
||||
// If STSIncludeSubdomains is set to true, the `includeSubdomains` will be appended to the Strict-Transport-Security header. Default is false.
|
||||
STSIncludeSubdomains bool
|
||||
// If FrameDeny is set to true, adds the X-Frame-Options header with the value of `DENY`. Default is false.
|
||||
FrameDeny bool
|
||||
// CustomFrameOptionsValue allows the X-Frame-Options header value to be set with a custom value. This overrides the FrameDeny option.
|
||||
CustomFrameOptionsValue string
|
||||
// If ContentTypeNosniff is true, adds the X-Content-Type-Options header with the value `nosniff`. Default is false.
|
||||
ContentTypeNosniff bool
|
||||
// If BrowserXssFilter is true, adds the X-XSS-Protection header with the value `1; mode=block`. Default is false.
|
||||
BrowserXssFilter bool
|
||||
// ContentSecurityPolicy allows the Content-Security-Policy header value to be set with a custom value. Default is "".
|
||||
ContentSecurityPolicy string
|
||||
// When developing, the AllowedHosts, SSL, and STS options can cause some unwanted effects. Usually testing happens on http, not https, and on localhost, not your production domain... so set this to true for dev environment.
|
||||
// If you would like your development environment to mimic production with complete Host blocking, SSL redirects, and STS headers, leave this as false. Default if false.
|
||||
IsDevelopment bool
|
||||
|
||||
// Handlers for when an error occurs (ie bad host).
|
||||
BadHostHandler http.Handler
|
||||
}
|
||||
|
||||
// Secure is a middleware that helps setup a few basic security features. A single secure.Options struct can be
|
||||
// provided to configure which features should be enabled, and the ability to override a few of the default values.
|
||||
type secure struct {
|
||||
// Customize Secure with an Options struct.
|
||||
opt Options
|
||||
}
|
||||
|
||||
// Constructs a new Secure instance with supplied options.
|
||||
func New(options Options) *secure {
|
||||
if options.BadHostHandler == nil {
|
||||
options.BadHostHandler = http.HandlerFunc(defaultBadHostHandler)
|
||||
}
|
||||
|
||||
return &secure{
|
||||
opt: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *secure) process(w http.ResponseWriter, r *http.Request) error {
|
||||
// Allowed hosts check.
|
||||
if len(s.opt.AllowedHosts) > 0 && !s.opt.IsDevelopment {
|
||||
isGoodHost := false
|
||||
for _, allowedHost := range s.opt.AllowedHosts {
|
||||
if strings.EqualFold(allowedHost, r.Host) {
|
||||
isGoodHost = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isGoodHost {
|
||||
s.opt.BadHostHandler.ServeHTTP(w, r)
|
||||
return fmt.Errorf("Bad host name: %s", r.Host)
|
||||
}
|
||||
}
|
||||
|
||||
// SSL check.
|
||||
if s.opt.SSLRedirect && s.opt.IsDevelopment == false {
|
||||
isSSL := false
|
||||
if strings.EqualFold(r.URL.Scheme, "https") || r.TLS != nil {
|
||||
isSSL = true
|
||||
} else {
|
||||
for k, v := range s.opt.SSLProxyHeaders {
|
||||
if r.Header.Get(k) == v {
|
||||
isSSL = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isSSL == false {
|
||||
url := r.URL
|
||||
url.Scheme = "https"
|
||||
url.Host = r.Host
|
||||
|
||||
if len(s.opt.SSLHost) > 0 {
|
||||
url.Host = s.opt.SSLHost
|
||||
}
|
||||
|
||||
status := http.StatusMovedPermanently
|
||||
if s.opt.SSLTemporaryRedirect {
|
||||
status = http.StatusTemporaryRedirect
|
||||
}
|
||||
|
||||
http.Redirect(w, r, url.String(), status)
|
||||
return fmt.Errorf("Redirecting to HTTPS")
|
||||
}
|
||||
}
|
||||
|
||||
// Strict Transport Security header.
|
||||
if s.opt.STSSeconds != 0 && !s.opt.IsDevelopment {
|
||||
stsSub := ""
|
||||
if s.opt.STSIncludeSubdomains {
|
||||
stsSub = stsSubdomainString
|
||||
}
|
||||
|
||||
w.Header().Add(stsHeader, fmt.Sprintf("max-age=%d%s", s.opt.STSSeconds, stsSub))
|
||||
}
|
||||
|
||||
// Frame Options header.
|
||||
if len(s.opt.CustomFrameOptionsValue) > 0 {
|
||||
w.Header().Add(frameOptionsHeader, s.opt.CustomFrameOptionsValue)
|
||||
} else if s.opt.FrameDeny {
|
||||
w.Header().Add(frameOptionsHeader, frameOptionsValue)
|
||||
}
|
||||
|
||||
// Content Type Options header.
|
||||
if s.opt.ContentTypeNosniff {
|
||||
w.Header().Add(contentTypeHeader, contentTypeValue)
|
||||
}
|
||||
|
||||
// XSS Protection header.
|
||||
if s.opt.BrowserXssFilter {
|
||||
w.Header().Add(xssProtectionHeader, xssProtectionValue)
|
||||
}
|
||||
|
||||
// Content Security Policy header.
|
||||
if len(s.opt.ContentSecurityPolicy) > 0 {
|
||||
w.Header().Add(cspHeader, s.opt.ContentSecurityPolicy)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func Secure(options Options) gin.HandlerFunc {
|
||||
s := New(options)
|
||||
|
||||
return func(c *gin.Context) {
|
||||
err := s.process(c.Writer, c.Request)
|
||||
if err != nil {
|
||||
if c.Writer.Written() {
|
||||
c.AbortWithStatus(c.Writer.Status())
|
||||
} else {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
470
vendor/github.com/johnniedoe/contrib/secure/secure_test.go
generated
vendored
Normal file
470
vendor/github.com/johnniedoe/contrib/secure/secure_test.go
generated
vendored
Normal file
@@ -0,0 +1,470 @@
|
||||
package secure
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
testResponse = "bar"
|
||||
)
|
||||
|
||||
func newServer(options Options) *gin.Engine {
|
||||
r := gin.Default()
|
||||
r.Use(Secure(options))
|
||||
r.GET("/foo", func(c *gin.Context) {
|
||||
c.String(200, testResponse)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func TestNoConfig(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
// Intentionally left blank.
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Body.String(), "bar")
|
||||
}
|
||||
|
||||
func TestNoAllowHosts(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Body.String(), `bar`)
|
||||
}
|
||||
|
||||
func TestGoodSingleAllowHosts(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"www.example.com"},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Body.String(), `bar`)
|
||||
}
|
||||
|
||||
func TestBadSingleAllowHosts(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"sub.example.com"},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestGoodMultipleAllowHosts(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"www.example.com", "sub.example.com"},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "sub.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Body.String(), `bar`)
|
||||
}
|
||||
|
||||
func TestBadMultipleAllowHosts(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"www.example.com", "sub.example.com"},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www3.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestAllowHostsInDevMode(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"www.example.com", "sub.example.com"},
|
||||
IsDevelopment: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www3.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestBadHostHandler(t *testing.T) {
|
||||
|
||||
badHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "BadHost", http.StatusInternalServerError)
|
||||
})
|
||||
|
||||
s := newServer(Options{
|
||||
AllowedHosts: []string{"www.example.com", "sub.example.com"},
|
||||
BadHostHandler: badHandler,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www3.example.com"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusInternalServerError)
|
||||
|
||||
// http.Error outputs a new line character with the response.
|
||||
expect(t, res.Body.String(), "BadHost\n")
|
||||
}
|
||||
|
||||
func TestSSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "https"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestSSLInDevMode(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
IsDevelopment: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestBasicSSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusMovedPermanently)
|
||||
expect(t, res.Header().Get("Location"), "https://www.example.com/foo")
|
||||
}
|
||||
|
||||
func TestBasicSSLWithHost(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLHost: "secure.example.com",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusMovedPermanently)
|
||||
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo")
|
||||
}
|
||||
|
||||
func TestBadProxySSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "https")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusMovedPermanently)
|
||||
expect(t, res.Header().Get("Location"), "https://www.example.com/foo")
|
||||
}
|
||||
|
||||
func TestCustomProxySSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "https")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestCustomProxySSLInDevMode(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
IsDevelopment: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "http")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestCustomProxyAndHostSSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
SSLHost: "secure.example.com",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "https")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestCustomBadProxyAndHostSSL(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "superman"},
|
||||
SSLHost: "secure.example.com",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "https")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusMovedPermanently)
|
||||
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo")
|
||||
}
|
||||
|
||||
func TestCustomBadProxyAndHostSSLWithTempRedirect(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
SSLRedirect: true,
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "superman"},
|
||||
SSLHost: "secure.example.com",
|
||||
SSLTemporaryRedirect: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
req.Host = "www.example.com"
|
||||
req.URL.Scheme = "http"
|
||||
req.Header.Add("X-Forwarded-Proto", "https")
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusTemporaryRedirect)
|
||||
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo")
|
||||
}
|
||||
|
||||
func TestStsHeader(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
STSSeconds: 315360000,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000")
|
||||
}
|
||||
|
||||
func TestStsHeaderInDevMode(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
STSSeconds: 315360000,
|
||||
IsDevelopment: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("Strict-Transport-Security"), "")
|
||||
}
|
||||
|
||||
func TestStsHeaderWithSubdomain(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
STSSeconds: 315360000,
|
||||
STSIncludeSubdomains: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000; includeSubdomains")
|
||||
}
|
||||
|
||||
func TestFrameDeny(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
FrameDeny: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-Frame-Options"), "DENY")
|
||||
}
|
||||
|
||||
func TestCustomFrameValue(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
CustomFrameOptionsValue: "SAMEORIGIN",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-Frame-Options"), "SAMEORIGIN")
|
||||
}
|
||||
|
||||
func TestCustomFrameValueWithDeny(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
FrameDeny: true,
|
||||
CustomFrameOptionsValue: "SAMEORIGIN",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-Frame-Options"), "SAMEORIGIN")
|
||||
}
|
||||
|
||||
func TestContentNosniff(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
ContentTypeNosniff: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-Content-Type-Options"), "nosniff")
|
||||
}
|
||||
|
||||
func TestXSSProtection(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
BrowserXssFilter: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-XSS-Protection"), "1; mode=block")
|
||||
}
|
||||
|
||||
func TestCsp(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
ContentSecurityPolicy: "default-src 'self'",
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("Content-Security-Policy"), "default-src 'self'")
|
||||
}
|
||||
|
||||
func TestInlineSecure(t *testing.T) {
|
||||
s := newServer(Options{
|
||||
FrameDeny: true,
|
||||
})
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/foo", nil)
|
||||
|
||||
s.ServeHTTP(res, req)
|
||||
|
||||
expect(t, res.Code, http.StatusOK)
|
||||
expect(t, res.Header().Get("X-Frame-Options"), "DENY")
|
||||
}
|
||||
|
||||
/* Test Helpers */
|
||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if a != b {
|
||||
t.Errorf("Expected [%v] (type %v) - Got [%v] (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user