|
1 | 1 | package nginx
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
5 | 6 | "io/ioutil"
|
6 | 7 | "os"
|
7 | 8 | "path/filepath"
|
8 | 9 | "reflect"
|
| 10 | + "regexp" |
9 | 11 | "testing"
|
| 12 | + "text/template" |
10 | 13 |
|
| 14 | + "github.com/Masterminds/sprig" |
11 | 15 | "github.com/deis/router/model"
|
12 | 16 | )
|
13 | 17 |
|
@@ -218,3 +222,66 @@ func checkCertAndKey(crtPath string, keyPath string, expectedCertContents string
|
218 | 222 |
|
219 | 223 | return nil
|
220 | 224 | }
|
| 225 | + |
| 226 | +func TestDisableServerTokens(t *testing.T) { |
| 227 | + routerConfig := &model.RouterConfig{ |
| 228 | + WorkerProcesses: "auto", |
| 229 | + MaxWorkerConnections: "768", |
| 230 | + TrafficStatusZoneSize: "1m", |
| 231 | + DefaultTimeout: "1300s", |
| 232 | + ServerNameHashMaxSize: "512", |
| 233 | + ServerNameHashBucketSize: "64", |
| 234 | + GzipConfig: &model.GzipConfig{ |
| 235 | + Enabled: true, |
| 236 | + CompLevel: "5", |
| 237 | + Disable: "msie6", |
| 238 | + HTTPVersion: "1.1", |
| 239 | + MinLength: "256", |
| 240 | + Proxied: "any", |
| 241 | + Types: "application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component", |
| 242 | + Vary: "on", |
| 243 | + }, |
| 244 | + BodySize: "1m", |
| 245 | + ProxyRealIPCIDRs: []string{"10.0.0.0/8"}, |
| 246 | + ErrorLogLevel: "error", |
| 247 | + UseProxyProtocol: false, |
| 248 | + EnforceWhitelists: false, |
| 249 | + WhitelistMode: "extend", |
| 250 | + SSLConfig: &model.SSLConfig{ |
| 251 | + Enforce: false, |
| 252 | + Protocols: "TLSv1 TLSv1.1 TLSv1.2", |
| 253 | + SessionTimeout: "10m", |
| 254 | + UseSessionTickets: true, |
| 255 | + BufferSize: "4k", |
| 256 | + HSTSConfig: &model.HSTSConfig{ |
| 257 | + Enabled: false, |
| 258 | + MaxAge: 15552000, // 180 days |
| 259 | + IncludeSubDomains: false, |
| 260 | + Preload: false, |
| 261 | + }, |
| 262 | + }, |
| 263 | + |
| 264 | + DisableServerTokens: true, |
| 265 | + } |
| 266 | + |
| 267 | + var b bytes.Buffer |
| 268 | + |
| 269 | + tmpl, err := template.New("nginx").Funcs(sprig.TxtFuncMap()).Parse(confTemplate) |
| 270 | + |
| 271 | + if err != nil { |
| 272 | + t.Fatalf("Encountered an error: %v", err) |
| 273 | + } |
| 274 | + |
| 275 | + err = tmpl.Execute(&b, routerConfig) |
| 276 | + |
| 277 | + if err != nil { |
| 278 | + t.Fatalf("Encountered an error: %v", err) |
| 279 | + } |
| 280 | + |
| 281 | + validDirective := regexp.MustCompile(`(?m)^(\s*)server_tokens off;$`) |
| 282 | + |
| 283 | + if !validDirective.Match(b.Bytes()) { |
| 284 | + t.Errorf("Expected: 'server_tokens off' in the configuration. Actual: no match") |
| 285 | + } |
| 286 | + |
| 287 | +} |
0 commit comments