1919
2020typedef struct {
2121 ngx_flag_t enable ;
22-
23- ngx_uint_t xss ;
24- ngx_uint_t fo ;
25-
22+
23+ ngx_uint_t xss ;
24+ ngx_uint_t fo ;
25+
2626 ngx_hash_t nosniff_types ;
2727 ngx_array_t * types_keys ;
2828
@@ -45,8 +45,8 @@ static ngx_conf_enum_t ngx_http_frame_options[] = {
4545
4646static ngx_int_t ngx_http_security_headers_filter (ngx_http_request_t * r );
4747static void * ngx_http_security_headers_create_loc_conf (ngx_conf_t * cf );
48- static char * ngx_http_security_headers_merge_loc_conf (ngx_conf_t * cf , void * parent ,
49- void * child );
48+ static char * ngx_http_security_headers_merge_loc_conf (ngx_conf_t * cf ,
49+ void * parent , void * child );
5050static ngx_int_t ngx_http_security_headers_init (ngx_conf_t * cf );
5151
5252ngx_str_t ngx_http_security_headers_default_nosniff_types [] = {
@@ -63,29 +63,29 @@ static ngx_command_t ngx_http_security_headers_commands[] = {
6363 ngx_conf_set_flag_slot ,
6464 NGX_HTTP_LOC_CONF_OFFSET ,
6565 offsetof( ngx_http_security_headers_loc_conf_t , enable ),
66- NULL },
67-
66+ NULL },
67+
6868 { ngx_string ("security_headers_nosniff_types" ),
6969 NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_1MORE ,
7070 ngx_http_types_slot ,
7171 NGX_HTTP_LOC_CONF_OFFSET ,
7272 offsetof(ngx_http_security_headers_loc_conf_t , types_keys ),
73- & ngx_http_security_headers_default_nosniff_types [0 ] },
74-
73+ & ngx_http_security_headers_default_nosniff_types [0 ] },
74+
7575 { ngx_string ("security_headers_xss" ),
7676 NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
7777 ngx_conf_set_enum_slot ,
7878 NGX_HTTP_LOC_CONF_OFFSET ,
7979 offsetof(ngx_http_security_headers_loc_conf_t , xss ),
8080 ngx_http_xss_protection },
81-
81+
8282 { ngx_string ("security_headers_frame" ),
8383 NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
8484 ngx_conf_set_enum_slot ,
8585 NGX_HTTP_LOC_CONF_OFFSET ,
8686 offsetof(ngx_http_security_headers_loc_conf_t , fo ),
87- ngx_http_frame_options },
88-
87+ ngx_http_frame_options },
88+
8989 ngx_null_command
9090};
9191
@@ -100,8 +100,8 @@ static ngx_http_module_t ngx_http_security_headers_module_ctx = {
100100 NULL , /* create server configuration */
101101 NULL , /* merge server configuration */
102102
103- ngx_http_security_headers_create_loc_conf , /* create location configuration */
104- ngx_http_security_headers_merge_loc_conf /* merge location configuration */
103+ ngx_http_security_headers_create_loc_conf , /* create location config */
104+ ngx_http_security_headers_merge_loc_conf /* merge location config */
105105};
106106
107107
@@ -135,17 +135,18 @@ ngx_http_security_headers_filter(ngx_http_request_t *r)
135135 ngx_table_elt_t * h_x_xss ;
136136 ngx_table_elt_t * h_x_fo ;
137137 ngx_http_security_headers_loc_conf_t * slcf ;
138- ngx_uint_t i ;
138+ ngx_uint_t i ;
139139
140140 slcf = ngx_http_get_module_loc_conf (r , ngx_http_security_headers_module );
141141
142142 if (1 != slcf -> enable ) {
143143 return ngx_http_next_header_filter (r );
144144 }
145-
145+
146146 /* add X-Content-Type-Options to output */
147- if (r -> headers_out .status == NGX_HTTP_OK
148- && ngx_http_test_content_type (r , & slcf -> nosniff_types ) != NULL ) {
147+ if (r -> headers_out .status == NGX_HTTP_OK
148+ && ngx_http_test_content_type (r , & slcf -> nosniff_types ) != NULL )
149+ {
149150 h_x_cto = ngx_list_push (& r -> headers_out .headers );
150151 if (h_x_cto == NULL ) {
151152 return NGX_ERROR ;
@@ -155,10 +156,11 @@ ngx_http_security_headers_filter(ngx_http_request_t *r)
155156 ngx_str_set (& h_x_cto -> key , "X-Content-Type-Options" );
156157 ngx_str_set (& h_x_cto -> value , "nosniff" );
157158 }
158-
159+
159160 /* Add X-XSS-Protection */
160161 if (r -> headers_out .status != NGX_HTTP_NOT_MODIFIED
161- && NGX_HTTP_SECURITY_HEADER_OMIT != slcf -> xss ) {
162+ && NGX_HTTP_SECURITY_HEADER_OMIT != slcf -> xss )
163+ {
162164 h_x_xss = ngx_list_push (& r -> headers_out .headers );
163165 if (h_x_xss == NULL ) {
164166 return NGX_ERROR ;
@@ -174,10 +176,11 @@ ngx_http_security_headers_filter(ngx_http_request_t *r)
174176 ngx_str_set (& h_x_xss -> value , "0" );
175177 }
176178 }
177-
179+
178180 /* Add X-Frame-Options */
179181 if (r -> headers_out .status != NGX_HTTP_NOT_MODIFIED
180- && NGX_HTTP_SECURITY_HEADER_OMIT != slcf -> fo ) {
182+ && NGX_HTTP_SECURITY_HEADER_OMIT != slcf -> fo )
183+ {
181184 h_x_fo = ngx_list_push (& r -> headers_out .headers );
182185 if (h_x_fo == NULL ) {
183186 return NGX_ERROR ;
@@ -189,53 +192,55 @@ ngx_http_security_headers_filter(ngx_http_request_t *r)
189192 ngx_str_set (& h_x_fo -> value , "SAMEORIGIN" );
190193 } else if (NGX_HTTP_FO_HEADER_DENY == slcf -> fo ) {
191194 ngx_str_set (& h_x_fo -> value , "DENY" );
192- }
195+ }
193196 }
194-
197+
195198 /* Deal with Server header */
196199 ngx_table_elt_t * h_server ;
197200 h_server = r -> headers_out .server ;
198201 if (h_server == NULL ) {
199202 h_server = ngx_list_push (& r -> headers_out .headers );
200203 if (h_server == NULL ) {
201204 return NGX_ERROR ;
202- }
205+ }
203206 /*
204207 * h->key.data = (u_char *) "Server";
205208 * h->key.len = sizeof("Server") - 1;
206209 * h->value.data = (u_char *) "";
207210 * h->value.len = sizeof("") - 1;
208211 */
209-
212+
210213 r -> headers_out .server = h_server ;
211- }
214+ }
212215 h_server -> hash = 0 ;
213-
216+
214217 /* Find X-Powered-By header */
215218 ngx_list_part_t * part = NULL ;
216219 ngx_table_elt_t * header = NULL ;
217220
218221 part = & r -> headers_out .headers .part ;
219222 header = part -> elts ;
220- for ( i = 0 ; ; i ++ ) {
221- if ( i >= part -> nelts ) {
222- if ( part -> next == NULL ) {
223- break ;
223+ for ( i = 0 ; ; i ++ ) {
224+ if (i >= part -> nelts ) {
225+ if (part -> next == NULL ) {
226+ break ;
224227 }
225228
226229 part = part -> next ;
227230 header = part -> elts ;
228231 i = 0 ;
229232 }
230233 if (header [i ].hash == 0 ) {
231- continue ;
234+ continue ;
232235 }
233- if ( ngx_strcasecmp (header [i ].key .data , (u_char * )"x-powered-by" ) == 0 ) {
236+ if (ngx_strcasecmp (header [i ].key .data ,
237+ (u_char * )"x-powered-by" ) == 0 )
238+ {
234239 header [i ].hash = 0 ;
235240 break ;
236241 }
237242 }
238-
243+
239244 /* proceed to the next handler in chain */
240245
241246 return ngx_http_next_header_filter (r );
@@ -251,7 +256,7 @@ ngx_http_security_headers_create_loc_conf(ngx_conf_t *cf)
251256 if (conf == NULL ) {
252257 return NULL ;
253258 }
254-
259+
255260 conf -> xss = NGX_CONF_UNSET_UINT ;
256261 conf -> fo = NGX_CONF_UNSET_UINT ;
257262 conf -> enable = NGX_CONF_UNSET_UINT ;
@@ -261,26 +266,26 @@ ngx_http_security_headers_create_loc_conf(ngx_conf_t *cf)
261266
262267
263268static char *
264- ngx_http_security_headers_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child )
269+ ngx_http_security_headers_merge_loc_conf (ngx_conf_t * cf , void * parent ,
270+ void * child )
265271{
266272 ngx_http_security_headers_loc_conf_t * prev = parent ;
267273 ngx_http_security_headers_loc_conf_t * conf = child ;
268274
269275 ngx_conf_merge_value ( conf -> enable , prev -> enable , 0 );
270-
276+
271277 if (ngx_http_merge_types (cf , & conf -> types_keys , & conf -> nosniff_types ,
272278 & prev -> types_keys , & prev -> nosniff_types ,
273279 ngx_http_security_headers_default_nosniff_types )
274280 != NGX_OK )
275281 {
276282 return NGX_CONF_ERROR ;
277283 }
278-
284+
279285 ngx_conf_merge_uint_value (conf -> xss , prev -> xss ,
280286 NGX_HTTP_XSS_HEADER_BLOCK );
281-
282287 ngx_conf_merge_uint_value (conf -> fo , prev -> fo ,
283- NGX_HTTP_FO_HEADER_SAME );
288+ NGX_HTTP_FO_HEADER_SAME );
284289
285290 return NGX_CONF_OK ;
286291}
0 commit comments