You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+14-5
Original file line number
Diff line number
Diff line change
@@ -62,8 +62,8 @@ Secure comes with a variety of configuration options (Note: these are not the de
62
62
// ...
63
63
s:= secure.New(secure.Options{
64
64
AllowedHosts: []string{"ssl.example.com"}, // AllowedHosts is a list of fully qualified domain names that are allowed. Default is empty list, which allows any and all host names.
65
-
AllowedHostsFunc: func() []string { return []string{"example.com", "www.example.com" } //AllowedHostsFunc is a custom function that returns a list of fully qualified domain names that are allowed. This can be used in combination with the above AllowedHosts.
66
-
AllowedHostsAreRegex: false, //AllowedHostsAreRegex determines, if the provided AllowedHosts slice contains valid regular expressions. This does not apply to the `AllowedHostsFunc` values! Default is false.
65
+
AllowedHostsAreRegex: false, //AllowedHostsAreRegex determines, if the provided AllowedHosts slice contains valid regular expressions. Default is false.
66
+
AllowRequestFunc: nil, //AllowRequestFunc is a custom function type that allows you to determine if the request should proceed or not based on your own custom logic. Default is nil.
67
67
HostsProxyHeaders: []string{"X-Forwarded-Hosts"}, // HostsProxyHeaders is a set of header keys that may hold a proxied hostname value for the request.
68
68
SSLRedirect: true, // If SSLRedirect is set to true, then only allow HTTPS requests. Default is false.
69
69
SSLTemporaryRedirect: false, // If SSLTemporaryRedirect is true, the a 302 will be used while redirecting. Default is false (301).
@@ -102,8 +102,8 @@ s := secure.New()
102
102
103
103
l:= secure.New(secure.Options{
104
104
AllowedHosts: []string,
105
-
AllowedHostsFunc: nil,
106
105
AllowedHostsAreRegex: false,
106
+
AllowRequestFunc: nil,
107
107
HostsProxyHeaders: []string,
108
108
SSLRedirect: false,
109
109
SSLTemporaryRedirect: false,
@@ -127,11 +127,20 @@ l := secure.New(secure.Options{
127
127
IsDevelopment: false,
128
128
})
129
129
~~~
130
-
Also note the default bad host handler returns anerror:
130
+
The default bad host handler returns the following error:
Call `secure.SetBadRequestHandler` to set your own custom handler.
141
+
142
+
### Allow Request Function
143
+
Secure allows you to set a custom function (`func(r *http.Request) bool`) for the `AllowRequestFunc` option. You can use this function as a custom filter to allow the request to continue or simply reject it. This can be handy if you need to do any dynamic filtering on any of the request properties. It should be noted that this function will be called on every request, so be sure to make your checks quick and not relying on time consuming external calls (or you will be slowing down all requests). See above on how to set a custom handler for the rejected requests.
135
144
136
145
### Redirecting HTTP to HTTPS
137
146
If you want to redirect all HTTP requests to HTTPS, you can use the following example.
// Options is a struct for specifying configuration options for the secure.Secure middleware.
47
51
typeOptionsstruct {
48
52
// If BrowserXssFilter is true, adds the X-XSS-Protection header with the value `1; mode=block`. Default is false.
@@ -95,10 +99,10 @@ type Options struct {
95
99
SSLHoststring
96
100
// AllowedHosts is a slice of fully qualified domain names that are allowed. Default is an empty slice, which allows any and all host names.
97
101
AllowedHosts []string
98
-
// AllowedHostsFunc is a custom function that returns a slice of fully qualified domain names that are allowed. If set, values will be used in combination with the above AllowedHosts. Default is nil.
99
-
AllowedHostsFuncAllowedHostsFunc
100
-
// AllowedHostsAreRegex determines, if the provided `AllowedHosts` slice contains valid regular expressions. This does not apply to `AllowedHostsFunc`! If this flag is set to true, every request's host will be checked against these expressions. Default is false.
102
+
// AllowedHostsAreRegex determines, if the provided `AllowedHosts` slice contains valid regular expressions. If this flag is set to true, every request's host will be checked against these expressions. Default is false.
101
103
AllowedHostsAreRegexbool
104
+
// AllowRequestFunc is a custom function that allows you to determine if the request should proceed or not based on your own custom logic. Default is nil.
105
+
AllowRequestFuncAllowRequestFunc
102
106
// HostsProxyHeaders is a set of header keys that may hold a proxied hostname value for the request.
103
107
HostsProxyHeaders []string
104
108
// SSLHostFunc is a function pointer, the return value of the function is the host name that has same functionality as `SSHost`. Default is nil.
@@ -123,6 +127,9 @@ type Secure struct {
123
127
// badHostHandler is the handler used when an incorrect host is passed in.
124
128
badHostHandler http.Handler
125
129
130
+
// badRequestHandler is the handler used when the AllowRequestFunc rejects a request.
131
+
badRequestHandler http.Handler
132
+
126
133
// cRegexAllowedHosts saves the compiled regular expressions of the AllowedHosts
0 commit comments