Add directive to allow underscores in hostnames

Aleksandr Kupriyanov sasha at instartlogic.com
Thu Nov 17 00:36:12 UTC 2016


<http://www.google.com/url?q=http%3A%2F%2Fwww.instartlogic.com%2F&sa=D&sntz=1&usg=AFrqEzc4puDXYOgyifEWrSJrJIfW1sViFg>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20161116/eaf8911b/attachment.html>
-------------- next part --------------
# HG changeset patch
# User Aleksandr Kupriyanov <sasha at instartlogic.com>
# Date 1479340749 21600
# Node ID af947b854971993f318417c70c3818147b320a0d
# Parent  6a26016e9a138102798a7ec3e74747fbd6018f82
Add directive to allow underscores in hostnames

Two equivalent requests generate different responses:

1. ---------------
GET http://host_1.home/ HTTP/1.1
Host: host_1.home
...
HTTP/1.1 400 Bad Request
Server: nginx/1.X.XX
------------------

2. ---------------
GET / HTTP/1.1
Host: host_1.home
...
HTTP/1.1 200 OK
Server: nginx/1.X.XX
------------------

To avoid that a new directive is proposed:

Syntax: underscores_in_hostname on | off;
Default: underscores_in_headers off;
Context: http, server

Enables or disables the use of underscores in host names of
client request line.

See a discussion about underscores in DNS here:
http://domainkeys.sourceforge.net/underscore.html

diff -r 6a26016e9a13 -r af947b854971 src/http/ngx_http.h
--- a/src/http/ngx_http.h	Tue Nov 15 18:11:46 2016 +0300
+++ b/src/http/ngx_http.h	Wed Nov 16 17:59:09 2016 -0600
@@ -89,7 +89,8 @@
 int ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg);
 #endif
 
-ngx_int_t ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b);
+ngx_int_t ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b,
+    ngx_uint_t underscores_in_hostname);
 ngx_int_t ngx_http_parse_uri(ngx_http_request_t *r);
 ngx_int_t ngx_http_parse_complex_uri(ngx_http_request_t *r,
     ngx_uint_t merge_slashes);
diff -r 6a26016e9a13 -r af947b854971 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Tue Nov 15 18:11:46 2016 +0300
+++ b/src/http/ngx_http_core_module.c	Wed Nov 16 17:59:09 2016 -0600
@@ -264,6 +264,13 @@
       offsetof(ngx_http_core_srv_conf_t, underscores_in_headers),
       NULL },
 
+    { ngx_string("underscores_in_hostname"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_SRV_CONF_OFFSET,
+      offsetof(ngx_http_core_srv_conf_t, underscores_in_hostname),
+      NULL },
+
     { ngx_string("location"),
       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
       ngx_http_core_location,
@@ -3420,6 +3427,7 @@
     cscf->ignore_invalid_headers = NGX_CONF_UNSET;
     cscf->merge_slashes = NGX_CONF_UNSET;
     cscf->underscores_in_headers = NGX_CONF_UNSET;
+    cscf->underscores_in_hostname = NGX_CONF_UNSET;
 
     return cscf;
 }
@@ -3463,6 +3471,9 @@
     ngx_conf_merge_value(conf->underscores_in_headers,
                               prev->underscores_in_headers, 0);
 
+    ngx_conf_merge_value(conf->underscores_in_hostname,
+                              prev->underscores_in_hostname, 0);
+
     if (conf->server_names.nelts == 0) {
         /* the array has 4 empty preallocated elements, so push cannot fail */
         sn = ngx_array_push(&conf->server_names);
diff -r 6a26016e9a13 -r af947b854971 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h	Tue Nov 15 18:11:46 2016 +0300
+++ b/src/http/ngx_http_core_module.h	Wed Nov 16 17:59:09 2016 -0600
@@ -192,6 +192,7 @@
     ngx_flag_t                  ignore_invalid_headers;
     ngx_flag_t                  merge_slashes;
     ngx_flag_t                  underscores_in_headers;
+    ngx_flag_t                  underscores_in_hostname;
 
     unsigned                    listen:1;
 #if (NGX_PCRE)
diff -r 6a26016e9a13 -r af947b854971 src/http/ngx_http_parse.c
--- a/src/http/ngx_http_parse.c	Tue Nov 15 18:11:46 2016 +0300
+++ b/src/http/ngx_http_parse.c	Wed Nov 16 17:59:09 2016 -0600
@@ -101,7 +101,8 @@
 /* gcc, icc, msvc and others compile these switches as an jump table */
 
 ngx_int_t
-ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
+ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b,
+    ngx_uint_t underscores_in_hostname)
 {
     u_char  c, ch, *p, *m;
     enum {
@@ -357,7 +358,8 @@
                 break;
             }
 
-            if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') {
+            if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-' ||
+                (ch == '_' && underscores_in_hostname)) {
                 break;
             }
 
diff -r 6a26016e9a13 -r af947b854971 src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c	Tue Nov 15 18:11:46 2016 +0300
+++ b/src/http/ngx_http_request.c	Wed Nov 16 17:59:09 2016 -0600
@@ -922,6 +922,7 @@
     ngx_str_t            host;
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
+    ngx_http_core_srv_conf_t   *cscf;
 
     c = rev->data;
     r = c->data;
@@ -948,7 +949,10 @@
             }
         }
 
-        rc = ngx_http_parse_request_line(r, r->header_in);
+        cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+
+        rc = ngx_http_parse_request_line(r, r->header_in,
+                                         cscf->underscores_in_hostname);
 
         if (rc == NGX_OK) {
 


More information about the nginx-devel mailing list