Skip to content

Commit 98824f3

Browse files
author
Nick Thomas
committedMar 27, 2019
Merge branch 'issue_58547' into 'master'
Add API access check to Graphql Closes #58547 See merge request gitlab-org/gitlab-ce!26570
2 parents b78aa81 + 73b553a commit 98824f3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
 

‎app/controllers/graphql_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class GraphqlController < ApplicationController
1212
protect_from_forgery with: :null_session, only: :execute
1313

1414
before_action :check_graphql_feature_flag!
15+
before_action :authorize_access_api!
1516
before_action(only: [:execute]) { authenticate_sessionless_user!(:api) }
1617

1718
def execute
@@ -37,6 +38,10 @@ def execute
3738

3839
private
3940

41+
def authorize_access_api!
42+
access_denied!("API not accessible for user.") unless can?(current_user, :access_api)
43+
end
44+
4045
# Overridden from the ApplicationController to make the response look like
4146
# a GraphQL response. That is nicely picked up in Graphiql.
4247
def render_404

‎changelogs/unreleased/issue_58547.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Add API access check to Graphql
3+
merge_request: 26570
4+
author:
5+
type: other
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe GraphqlController do
6+
before do
7+
stub_feature_flags(graphql: true)
8+
end
9+
10+
describe 'POST #execute' do
11+
context 'when user is logged in' do
12+
let(:user) { create(:user) }
13+
14+
before do
15+
sign_in(user)
16+
end
17+
18+
it 'returns 200 when user can access API' do
19+
post :execute
20+
21+
expect(response).to have_gitlab_http_status(200)
22+
end
23+
24+
it 'returns access denied template when user cannot access API' do
25+
# User cannot access API in a couple of cases
26+
# * When user is internal(like ghost users)
27+
# * When user is blocked
28+
expect(Ability).to receive(:allowed?).with(user, :access_api, :global).and_return(false)
29+
30+
post :execute
31+
32+
expect(response.status).to eq(403)
33+
expect(response).to render_template('errors/access_denied')
34+
end
35+
end
36+
37+
context 'when user is not logged in' do
38+
it 'returns 200' do
39+
post :execute
40+
41+
expect(response).to have_gitlab_http_status(200)
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)
Please sign in to comment.