Merge branch '23197-add-custom-header-for-error-responses' into 'master'

Add custom header for error responses

Closes #23197

See merge request gitlab-org/gitlab-ce!21870
This commit is contained in:
Stan Hu 2018-10-01 17:43:40 +00:00
commit c9375e63d8
3 changed files with 91 additions and 0 deletions

View file

@ -109,6 +109,15 @@ class ApplicationController < ActionController::Base
request.env['rack.session.options'][:expire_after] = Settings.gitlab['unauthenticated_session_expire_delay']
end
def render(*args)
super.tap do
# Set a header for custom error pages to prevent them from being intercepted by gitlab-workhorse
if response.content_type == 'text/html' && (400..599).cover?(response.status)
response.headers['X-GitLab-Custom-Error'] = '1'
end
end
end
protected
def append_info_to_payload(payload)

View file

@ -0,0 +1,6 @@
---
title: Set a header for custom error pages to prevent them from being intercepted
by gitlab-workhorse
merge_request: 21870
author: David Piegza
type: fixed

View file

@ -728,4 +728,80 @@ describe ApplicationController do
end
end
end
context 'X-GitLab-Custom-Error header' do
before do
sign_in user
end
context 'given a 422 error page' do
controller do
def index
render 'errors/omniauth_error', layout: 'errors', status: 422
end
end
it 'sets a custom header' do
get :index
expect(response.headers['X-GitLab-Custom-Error']).to eq '1'
end
end
context 'given a 500 error page' do
controller do
def index
render 'errors/omniauth_error', layout: 'errors', status: 500
end
end
it 'sets a custom header' do
get :index
expect(response.headers['X-GitLab-Custom-Error']).to eq '1'
end
end
context 'given a 200 success page' do
controller do
def index
render 'errors/omniauth_error', layout: 'errors', status: 200
end
end
it 'does not set a custom header' do
get :index
expect(response.headers['X-GitLab-Custom-Error']).to be_nil
end
end
context 'given a json response' do
controller do
def index
render json: {}, status: :unprocessable_entity
end
end
it 'does not set a custom header' do
get :index, format: :json
expect(response.headers['X-GitLab-Custom-Error']).to be_nil
end
end
context 'given a json response for an html request' do
controller do
def index
render json: {}, status: :unprocessable_entity
end
end
it 'does not set a custom header' do
get :index
expect(response.headers['X-GitLab-Custom-Error']).to be_nil
end
end
end
end