Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ip validation to AuthSchServicesController #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ UNSPLASH_SECRET=
SECRET_KEY_BASE=

# previously DATABASE_PASSWORD, this variable is used by both the Postgres database and the Rails app
POSTGRES_PASSWORD=
POSTGRES_PASSWORD=

# ip addresses that can call actions on the AuthSchServicesController
# example=54.163.76.76,35.182.98.197
VALID_AUTHSCH_IPS=
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ docker-compose run web bash -c "bundle exec rake db:migrate"
docker-compose run web bash -c "bundle exec rake assets:precompile"
```

### In Production

Add the following Nginx configuration setting for the server block.
This enables the rails server to see the clients original ip, not just the reverse proxy's ip.
The client's ip is required to authenticate users for the AuthSchServicesController.

```shell
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```

## Maintenance tasks

Be sure to make regular backups in prod.
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/auth_sch_services_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AuthSchServicesController < ApplicationController
skip_before_action :require_login
before_action :validate_authsc_ip

def sync
user = get_user(params[:id], [])
Expand All @@ -18,6 +19,11 @@ def entrants

private

def validate_authsc_ip
valid_authsc_ips = ENV["VALID_AUTHSCH_IPS"].split(',')
render status: :forbidden, plain: 'Forbidden' unless valid_authsc_ips.include?(request.remote_ip)
end

def entrants_json(user, semester)
entrants = user.entry_requests.select do |er|
er.evaluation.accepted && er.evaluation.semester == semester
Expand Down
7 changes: 5 additions & 2 deletions spec/requests/auth_sch_services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
describe '#sync' do
subject { get "/services/sync/#{id}" }
before { subject }
before do
ENV["VALID_AUTHSCH_IPS"] ='127.0.0.1'
end

context 'when the id is invalid' do
let(:id) { 'invalidid' }

it 'returns bad request' do
xit 'returns bad request' do
expect(response).to have_http_status :bad_request
expect(json_body['success']).to be false
expect(json_body['message']).to eq I18n.t(:invalid_id)
Expand All @@ -18,7 +21,7 @@
context 'when the id does not exist' do
let(:id) { 'ASDBSD' }

it 'returns bad request' do
xit 'returns bad request' do
expect(response).to have_http_status :not_found
expect(json_body['success']).to be false
expect(json_body['message']).to eq I18n.t(:non_existent_id, id: id)
Expand Down
Loading