[patchew-devel] [PATCH] Set Host header in nginx

Fam Zheng posted 1 patch 6 years ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20180301100442.30308-1-famz@redhat.com
patchew/settings.py               | 10 ++++------
scripts/patchew-server.nginx.conf |  2 +-
tests/test_rest.py                | 10 ++++++++++
3 files changed, 15 insertions(+), 7 deletions(-)
[patchew-devel] [PATCH] Set Host header in nginx
Posted by Fam Zheng 6 years ago
From: Paolo Bonzini <pbonzini@redhat.com>

This fixes absolute URIs in the REST API.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 patchew/settings.py               | 10 ++++------
 scripts/patchew-server.nginx.conf |  2 +-
 tests/test_rest.py                | 10 ++++++++++
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/patchew/settings.py b/patchew/settings.py
index cca6c79..90a28c3 100644
--- a/patchew/settings.py
+++ b/patchew/settings.py
@@ -113,12 +113,10 @@ def env_detect():
         raise Exception("Unknown running environment")
 
 DEBUG, DATA_DIR = env_detect()
-if DEBUG:
-    ALLOWED_HOSTS = ["*"]
-else:
-    # On deployment environments, we are behind apache/nginx so allow local
-    # address only
-    ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
+
+# In production environments, we run in a container, behind nginx, which should
+# filter the allowed host names. So be a little flexible here
+ALLOWED_HOSTS = ["*"]
 
 if not os.path.isdir(DATA_DIR):
     os.makedirs(DATA_DIR)
diff --git a/scripts/patchew-server.nginx.conf b/scripts/patchew-server.nginx.conf
index d87d8b9..dbac28f 100644
--- a/scripts/patchew-server.nginx.conf
+++ b/scripts/patchew-server.nginx.conf
@@ -66,7 +66,7 @@ http {
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       # enable this if and only if you use HTTPS
       # proxy_set_header X-Forwarded-Proto https;
-      proxy_set_header Host localhost;
+      proxy_set_header Host $host;
       # we don't want nginx trying to do something clever with
       # redirects, we set the Host: header above already.
       proxy_redirect off;
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 0e43797..1a7285b 100755
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -36,6 +36,16 @@ class RestTest(PatchewTestCase):
         self.admin = User.objects.get(username='admin')
         self.USER_BASE = '%susers/%d/' % (self.REST_BASE, self.admin.id)
 
+    def test_root(self):
+        resp = self.api_client.get(self.REST_BASE)
+        self.assertEquals(resp.data['users'], self.REST_BASE + 'users/')
+        self.assertEquals(resp.data['projects'], self.REST_BASE + 'projects/')
+        self.assertEquals(resp.data['series'], self.REST_BASE + 'series/')
+        resp = self.api_client.get(self.REST_BASE, HTTP_HOST='patchew.org')
+        self.assertEquals(resp.data['users'], 'http://patchew.org/api/v1/users/')
+        self.assertEquals(resp.data['projects'], 'http://patchew.org/api/v1/projects/')
+        self.assertEquals(resp.data['series'], 'http://patchew.org/api/v1/series/')
+
     def test_users(self):
         resp = self.api_client.get(self.REST_BASE + 'users/')
         self.assertEquals(resp.data['count'], 1)
-- 
2.14.3