|
20 | 20 |
|
21 | 21 | title 'NGINX server config' |
22 | 22 |
|
| 23 | +# attributes |
| 24 | +CLIENT_MAX_BODY_SIZE = attribute( |
| 25 | + 'client_max_body_size', |
| 26 | + description: 'Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.', |
| 27 | + default: '1k' |
| 28 | +) |
| 29 | + |
| 30 | +CLIENT_BODY_BUFFER_SIZE = attribute( |
| 31 | + 'client_body_buffer_size', |
| 32 | + description: 'Sets buffer size for reading client request body. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms.', |
| 33 | + default: '1k' |
| 34 | +) |
| 35 | + |
| 36 | +CLIENT_HEADER_BUFFER_SIZE = attribute( |
| 37 | + 'client_header_buffer_size', |
| 38 | + description: 'Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.', |
| 39 | + default: '1k' |
| 40 | +) |
| 41 | + |
| 42 | +LARGE_CLIENT_HEADER_BUFFER = attribute( |
| 43 | + 'large_client_header_buffers', |
| 44 | + description: 'Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.', |
| 45 | + default: '2 1k' |
| 46 | +) |
| 47 | + |
| 48 | +KEEPALIVE_TIMEOUT = attribute( |
| 49 | + 'keepalive_timeout', |
| 50 | + description: 'The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.', |
| 51 | + default: '5 5' |
| 52 | +) |
| 53 | + |
| 54 | +CLIENT_BODY_TIMEOUT = attribute( |
| 55 | + 'client_body_timeout', |
| 56 | + description: 'Defines a timeout for reading client request body. The timeout is set only for a period between two successive read operations, not for the transmission of the whole request body. If a client does not transmit anything within this time, the 408 (Request Time-out) error is returned to the client.', |
| 57 | + default: '10' |
| 58 | +) |
| 59 | + |
| 60 | +CLIENT_HEADER_TIMEOUT = attribute( |
| 61 | + 'client_header_timeout', |
| 62 | + description: 'Defines a timeout for reading client request header. If a client does not transmit the entire header within this time, the 408 (Request Time-out) error is returned to the client.', |
| 63 | + default: '10' |
| 64 | +) |
| 65 | + |
| 66 | +SEND_TIMEOUT = attribute( |
| 67 | + 'send_timeout', |
| 68 | + description: 'Sets a timeout for transmitting a response to the client. The timeout is set only between two successive write operations, not for the transmission of the whole response. If the client does not receive anything within this time, the connection is closed.', |
| 69 | + default: '10' |
| 70 | +) |
| 71 | + |
| 72 | +HTTP_METHODS = attribute( |
| 73 | + 'http_methods', |
| 74 | + description: 'Specify the used HTTP methods', |
| 75 | + default: 'GET\|HEAD\|POST' |
| 76 | +) |
| 77 | + |
23 | 78 | only_if do |
24 | 79 | command('nginx').exist? |
25 | 80 | end |
|
112 | 167 | title 'Prevent buffer overflow attacks' |
113 | 168 | desc 'Buffer overflow attacks are made possible by writing data to a buffer and exceeding that buffer boundary and overwriting memory fragments of a process. To prevent this in nginx we can set buffer size limitations for all clients.' |
114 | 169 | describe parse_config_file(nginx_conf, options) do |
115 | | - its('client_body_buffer_size') { should eq '1k' } |
| 170 | + its('client_body_buffer_size') { should eq CLIENT_BODY_BUFFER_SIZE } |
116 | 171 | end |
117 | 172 | describe parse_config_file(nginx_conf, options) do |
118 | | - its('client_max_body_size') { should eq '1k' } |
| 173 | + its('client_max_body_size') { should eq CLIENT_MAX_BODY_SIZE } |
119 | 174 | end |
120 | 175 | describe parse_config_file(nginx_hardening, options) do |
121 | | - its('client_header_buffer_size') { should eq '1k' } |
| 176 | + its('client_header_buffer_size') { should eq CLIENT_HEADER_BUFFER_SIZE } |
122 | 177 | end |
123 | 178 | describe parse_config_file(nginx_hardening, options) do |
124 | | - its('large_client_header_buffers') { should eq '2 1k' } |
| 179 | + its('large_client_header_buffers') { should eq LARGE_CLIENT_HEADER_BUFFER } |
125 | 180 | end |
126 | 181 | end |
127 | 182 |
|
|
130 | 185 | title 'Control timeouts to improve performance' |
131 | 186 | desc 'Control timeouts to improve server performance and cut clients.' |
132 | 187 | describe parse_config_file(nginx_conf, options) do |
133 | | - its('keepalive_timeout') { should eq '5 5' } |
| 188 | + its('keepalive_timeout') { should eq KEEPALIVE_TIMEOUT } |
134 | 189 | end |
135 | 190 | describe parse_config_file(nginx_hardening, options) do |
136 | | - its('client_body_timeout') { should eq '10' } |
| 191 | + its('client_body_timeout') { should eq CLIENT_BODY_TIMEOUT } |
137 | 192 | end |
138 | 193 | describe parse_config_file(nginx_hardening, options) do |
139 | | - its('client_header_timeout') { should eq '10' } |
| 194 | + its('client_header_timeout') { should eq CLIENT_HEADER_TIMEOUT } |
140 | 195 | end |
141 | 196 | describe parse_config_file(nginx_hardening, options) do |
142 | | - its('send_timeout') { should eq '10' } |
| 197 | + its('send_timeout') { should eq SEND_TIMEOUT } |
143 | 198 | end |
144 | 199 | end |
145 | 200 |
|
|
181 | 236 | its('add_header') { should include 'X-Content-Type-Options nosniff' } |
182 | 237 | end |
183 | 238 | end |
| 239 | + |
| 240 | +control 'nginx-11' do |
| 241 | + impact 1.0 |
| 242 | + title 'Disable content-type sniffing' |
| 243 | + desc 'It prevents browser from trying to mime-sniff the content-type of a response away from the one being declared by the server. It reduces exposure to drive-by downloads and the risks of user uploaded content that, with clever naming, could be treated as a different content-type, like an executable.' |
| 244 | + describe parse_config_file(nginx_hardening, options_add_header) do |
| 245 | + its('add_header') { should include 'X-Content-Type-Options nosniff' } |
| 246 | + end |
| 247 | +end |
| 248 | + |
| 249 | +control 'nginx-12' do |
| 250 | + impact 1.0 |
| 251 | + title 'TLS Protocols' |
| 252 | + desc 'When choosing a cipher during an SSLv3 or TLSv1 handshake, normally the client\'s preference is used. If this directive is enabled, the server\'s preference will be used instead.' |
| 253 | + ref 'SSL Hardening config', url: 'https://mozilla.github.io/server-side-tls/ssl-config-generator/' |
| 254 | + describe file(nginx_hardening) do |
| 255 | + its('content') { should match(/^\s*ssl_protocols TLSv1.2;$/) } |
| 256 | + its('content') { should match(/^\s*ssl_session_tickets off;$/) } |
| 257 | + its('content') { should match(/^\s*ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';$/) } |
| 258 | + its('content') { should match(/^\s*ssl_prefer_server_ciphers on;$/) } |
| 259 | + its('content') { should match(%r{^\s*ssl_dhparam /etc/nginx/dh2048.pem;$}) } |
| 260 | + # its('content') { should match(/^\s*ssl on;$/) } |
| 261 | + end |
| 262 | +end |
| 263 | + |
| 264 | +control 'nginx-13' do |
| 265 | + impact 1.0 |
| 266 | + title 'Add HSTS Header' |
| 267 | + desc 'HTTP Strict Transport Security (HSTS) is a web security policy mechanism which helps to protect websites against protocol downgrade attacks and cookie hijacking. It allows web servers to declare that web browsers (or other complying user agents) should only interact with it using secure HTTPS connections, and never via the insecure HTTP protocol. HSTS is an IETF standards track protocol and is specified in RFC 6797.' |
| 268 | + describe file(nginx_hardening) do |
| 269 | + its('content') { should match(/^\s*add_header Strict-Transport-Security max-age=15768000;$/) } |
| 270 | + end |
| 271 | +end |
| 272 | + |
| 273 | +control 'nginx-14' do |
| 274 | + impact 1.0 |
| 275 | + title 'Disable insecure HTTP-methods' |
| 276 | + desc 'Disable insecure HTTP-methods and allow only necessary methods.' |
| 277 | + ref 'OWASP HTTP Methods', url: 'https://www.owasp.org/index.php/Test_HTTP_Methods_(OTG-CONFIG-006)' |
| 278 | + |
| 279 | + describe file(nginx_conf) do |
| 280 | + its('content') { should match(/^\s*if\s+\(\$request_method\s+\!\~\s+\^\(#{HTTP_METHODS}\)\$\)\{?$/) } |
| 281 | + end |
| 282 | +end |
| 283 | + |
| 284 | +control 'nginx-15' do |
| 285 | + impact 1.0 |
| 286 | + title 'Content-Security-Policy' |
| 287 | + desc 'The Content-Security-Policy HTTP response header helps you reduce XSS risks on modern browsers by declaring what dynamic resources are allowed to load via a HTTP Header' |
| 288 | + describe parse_config_file(nginx_hardening, options_add_header) do |
| 289 | + its('content') { should match(/^\s*add_header Content-Security-Policy "script-src 'self'; object-src 'self'";$/) } |
| 290 | + end |
| 291 | +end |
| 292 | + |
| 293 | +control 'nginx-16' do |
| 294 | + impact 1.0 |
| 295 | + title 'Set cookie with HttpOnly and Secure flag' |
| 296 | + desc 'You can mitigate most of the common Cross Site Scripting attack using HttpOnly and Secure flag in a cookie. Without having HttpOnly and Secure, it is possible to steal or manipulate web application session and cookies and it’s dangerous.' |
| 297 | + describe parse_config_file(nginx_hardening, options_add_header) do |
| 298 | + its('content') { should match(/^\s*set_cookie_flag * HttpOnly secure;$/) } |
| 299 | + end |
| 300 | +end |
0 commit comments