Skip to content

Commit

Permalink
add ip validation to AuthSchServicesController
Browse files Browse the repository at this point in the history
  • Loading branch information
SepsiLaszlo committed Feb 4, 2024
1 parent cdc9084 commit 33076a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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

0 comments on commit 33076a8

Please sign in to comment.