Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
nginx: Support a "True-Client-IP" or similar header.
Alex Vandiver0754ca25854d
diff --git a/puppet/zulip/files/nginx/uwsgi_params b/puppet/zulip/files/nginx/uwsgi_paramsindex 47acef235ed..cc4e0904e5c 100644--- a/puppet/zulip/files/nginx/uwsgi_params+++ b/puppet/zulip/files/nginx/uwsgi_params@@ -8,12 +8,12 @@ uwsgi_param DOCUMENT_ROOT $document_root;uwsgi_param SERVER_PROTOCOL $server_protocol;uwsgi_param REQUEST_SCHEME $scheme;uwsgi_param HTTPS $https if_not_empty;-uwsgi_param REMOTE_ADDR $remote_addr;+uwsgi_param REMOTE_ADDR $trusted_remote_addr;uwsgi_param REMOTE_PORT $remote_port;uwsgi_param SERVER_ADDR $server_addr;uwsgi_param SERVER_PORT $server_port;uwsgi_param SERVER_NAME $server_name;-uwsgi_param HTTP_X_REAL_IP $remote_addr;+uwsgi_param HTTP_X_REAL_IP $trusted_remote_addr;uwsgi_param HTTP_X_FORWARDED_PROTO $trusted_x_forwarded_proto;uwsgi_param HTTP_X_FORWARDED_SSL "";uwsgi_param HTTP_X_PROXY_MISCONFIGURATION $x_proxy_misconfiguration;diff --git a/puppet/zulip/files/nginx/zulip-include-common/proxy b/puppet/zulip/files/nginx/zulip-include-common/proxyindex fade49bd522..e6d5323424a 100644--- a/puppet/zulip/files/nginx/zulip-include-common/proxy+++ b/puppet/zulip/files/nginx/zulip-include-common/proxy@@ -5,7 +5,7 @@ proxy_set_header Connection "";proxy_set_header Host $http_host;proxy_set_header X-Forwarded-Proto $trusted_x_forwarded_proto;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;-proxy_set_header X-Real-Ip $remote_addr;+proxy_set_header X-Real-Ip $trusted_remote_addr;proxy_set_header X-Proxy-Misconfiguration $x_proxy_misconfiguration;proxy_next_upstream off;proxy_redirect off;diff --git a/puppet/zulip/manifests/app_frontend_base.pp b/puppet/zulip/manifests/app_frontend_base.ppindex 0d9b35600b0..9fcf806f3d5 100644--- a/puppet/zulip/manifests/app_frontend_base.pp+++ b/puppet/zulip/manifests/app_frontend_base.pp@@ -62,14 +62,6 @@ class zulip::app_frontend_base {$loadbalancers = split(zulipconf('loadbalancer', 'ips', ''), ',')if $loadbalancers != [] {- file { '/etc/nginx/zulip-include/app.d/accept-loadbalancer.conf':- require => File['/etc/nginx/zulip-include/app.d'],- owner => 'root',- group => 'root',- mode => '0644',- content => template('zulip/accept-loadbalancer.conf.template.erb'),- notify => Service['nginx'],- }file { '/etc/nginx/zulip-include/app.d/keepalive-loadbalancer.conf':require => File['/etc/nginx/zulip-include/app.d'],owner => 'root',@@ -79,12 +71,18 @@ class zulip::app_frontend_base {notify => Service['nginx'],}} else {- file { ['/etc/nginx/zulip-include/app.d/accept-loadbalancer.conf',- '/etc/nginx/zulip-include/app.d/keepalive-loadbalancer.conf']:+ file { '/etc/nginx/zulip-include/app.d/keepalive-loadbalancer.conf':ensure => absent,notify => Service['nginx'],}}+ file { '/etc/nginx/zulip-include/app.d/accept-loadbalancer.conf':+ # This moved to /etc/nginx/zulip-include/trusted-ip, via+ # nginx.pp. This block can be removed once direct Zulip upgrades+ # from Zulip 11 are no longer supported.+ ensure => absent,+ notify => Service['nginx'],+ }file { '/etc/nginx/zulip-include/app.d/healthcheck.conf':require => File['/etc/nginx/zulip-include/app.d'],owner => 'root',diff --git a/puppet/zulip/manifests/nginx.pp b/puppet/zulip/manifests/nginx.ppindex c507010f3ef..0022a977f87 100644--- a/puppet/zulip/manifests/nginx.pp+++ b/puppet/zulip/manifests/nginx.pp@@ -58,7 +58,19 @@ class zulip::nginx {}$loadbalancers = split(zulipconf('loadbalancer', 'ips', ''), ',')+ $raw_true_client_header = zulipconf('loadbalancer', 'true_client_header', '')+ $true_client_header = regsubst(downcase($raw_true_client_header), '-', '_', 'G')+ $true_client_header_from = split(zulipconf('loadbalancer', 'true_client_header_from', ''), ',')$lb_rejects_http_requests = zulipconf('loadbalancer', 'rejects_http_requests', false)+ file { '/etc/nginx/zulip-include/trusted-ip':+ ensure => file,+ require => Package[$zulip::common::nginx],+ owner => 'root',+ group => 'root',+ mode => '0644',+ notify => Service['nginx'],+ content => template('zulip/nginx/trusted-ip.template.erb'),+ }file { '/etc/nginx/zulip-include/trusted-proto':ensure => file,require => Package[$zulip::common::nginx],diff --git a/puppet/zulip/templates/accept-loadbalancer.conf.template.erb b/puppet/zulip/templates/accept-loadbalancer.conf.template.erbdeleted file mode 100644index 8ffe2704b30..00000000000--- a/puppet/zulip/templates/accept-loadbalancer.conf.template.erb+++ /dev/null@@ -1,13 +0,0 @@-# Configuration for making Zulip app frontends accept the-# X-Forwarded-For header used by proxies. The trusted IP addresses-# here are set by `loadbalancer.ips` in /etc/zulip/zulip.conf.-#-# This causes us to update $remote_addr, which we use in logging, and-# also pass down to Zulip as X-Real-Ip.-real_ip_header X-Forwarded-For;-real_ip_recursive on;-<% @loadbalancers.each do |host| -%>-set_real_ip_from <%= host %>;-<%-end--%>diff --git a/puppet/zulip/templates/nginx.conf.template.erb b/puppet/zulip/templates/nginx.conf.template.erbindex e126170726a..8e89a514d76 100644--- a/puppet/zulip/templates/nginx.conf.template.erb+++ b/puppet/zulip/templates/nginx.conf.template.erb@@ -24,7 +24,7 @@ http {include /etc/nginx/mime.types;default_type application/octet-stream;- log_format combined_with_host_and_time '$remote_addr - $remote_user [$time_local] '+ log_format combined_with_host_and_time '$trusted_remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" $host $request_time';access_log /var/log/nginx/access.log combined_with_host_and_time;diff --git a/puppet/zulip/templates/nginx/trusted-ip.template.erb b/puppet/zulip/templates/nginx/trusted-ip.template.erbnew file mode 100644index 00000000000..44126b6432a--- /dev/null+++ b/puppet/zulip/templates/nginx/trusted-ip.template.erb@@ -0,0 +1,54 @@+<% if @loadbalancers.any? %>+ # Configuration for making Zulip app frontends accept the+ # X-Forwarded-For header used by proxies. The trusted IP addresses+ # here are set by `loadbalancer.ips` in /etc/zulip/zulip.conf.+ #+ # This causes us to update $remote_addr, which we use in logging, and+ # also pass down to Zulip as X-Real-Ip.+ real_ip_header X-Forwarded-For;+ real_ip_recursive on;+ <% @loadbalancers.each do |host| -%>+ set_real_ip_from <%= host %>;+ <% end -%>++ <% if @true_client_header == '' %>+ # real_ip has done the work of updating $remote_addr based+ # on the trusted @loadbalancers+ map $remote_addr $trusted_remote_addr {+ default $remote_addr;+ }+ <% else %>+ <% if @true_client_header_from.any? %>+ # We have a separate set of post-X-Forwarded-For IPs we+ # trust with $http_<%= @true_client_header %>+ geo $remote_addr $do_trust_<%= @true_client_header %> {+ default 0;+ <% @true_client_header_from.each do |host| %>+ <%= host %> 1;+ <%- end %>+ }+ <% else %>+ # If our most recent hop came from a trusted source, we+ # trust $http_<%= @true_client_header %>+ geo $realip_remote_addr $do_trust_<%= @true_client_header %> {+ default 0;+ <% @loadbalancers.each do |host| %>+ <%= host %> 1;+ <%- end %>+ }+ <% end %>++ # This is done in two steps because geo handles CIDRs,+ # and map handles interpolation.+ map "$do_trust_<%= @true_client_header %>:$http_<%= @true_client_header %>" $trusted_remote_addr {+ "~^0:" $remote_addr;+ "~^1:$" $remote_addr;+ "~^1:" $http_<%= @true_client_header %>;+ }+ <% end %>+<% else %>+ # No remote proxies -- the remote_addr is what it is+ map $remote_addr $trusted_remote_addr {+ default $remote_addr;+ }+<% end %>diff --git a/puppet/zulip/templates/nginx/zulip-enterprise.template.erb b/puppet/zulip/templates/nginx/zulip-enterprise.template.erbindex e11fea34409..662b10e3ac2 100644--- a/puppet/zulip/templates/nginx/zulip-enterprise.template.erb+++ b/puppet/zulip/templates/nginx/zulip-enterprise.template.erb@@ -24,6 +24,7 @@ server {}<% end -%>+include /etc/nginx/zulip-include/trusted-ip;include /etc/nginx/zulip-include/trusted-proto;include /etc/nginx/zulip-include/s3-cache;include /etc/nginx/zulip-include/upstreams;