Skip to content
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ gem 'puma'

gem 'bootsnap', require: false
gem 'csv'
gem 'uri', '>= 1.1.1' # CVE-2025-61594

# Extend irb for better output
gem 'hirb'
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ GEM
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.3)
uri (1.1.1)
useragent (0.16.11)
version_gem (1.1.6)
warden (1.2.9)
Expand Down Expand Up @@ -623,6 +623,7 @@ DEPENDENCIES
sprockets-rails
sys-filesystem
tca_client
uri (>= 1.1.1)
webmock

RUBY VERSION
Expand Down
3 changes: 0 additions & 3 deletions app/api/api_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ class ApiRoot < Grape::API
format :json

before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'

Thread.current.thread_variable_set(:ip, request.ip)
end

Expand Down
21 changes: 19 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,27 @@ def self.fetch_boolean_env(name)
Rails.root.join('app/models/d2l')

# CORS config
# Configure a strict allowlist. Override per environment via:
# CORS_ALLOWED_ORIGINS="http://localhost:4200,https://frontend.example.edu"
default_cors_origins = [
'http://localhost:4200',
"https://#{config.institution[:host]}"
].uniq
allowed_cors_origins = ENV.fetch('CORS_ALLOWED_ORIGINS', default_cors_origins.join(','))
.split(',')
.map(&:strip)
.reject(&:empty?)
.uniq

config.middleware.insert_before Warden::Manager, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: %i(get post put delete options)
origins do |source, _env|
allowed_cors_origins.include?(source)
end

resource '*',
headers: %w[Content-Type Authorization Accept],
methods: %i[get post put delete options]
end
end

Expand Down
21 changes: 21 additions & 0 deletions config/initializers/uri_credential_leak_mitigation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'uri'

# Mitigates credential leakage when combining a credential-bearing base URI
# with a relative URI via URI#+ by stripping userinfo from the merged result.
module UriCredentialLeakMitigation
def +(other)
combined = super

return combined unless other.respond_to?(:absolute?) && !other.absolute?
return combined unless combined.respond_to?(:user=) || combined.respond_to?(:password=)

sanitized = combined.dup
sanitized.user = nil if sanitized.respond_to?(:user=)
sanitized.password = nil if sanitized.respond_to?(:password=)
sanitized
rescue URI::InvalidComponentError
combined
end
end

URI::Generic.prepend(UriCredentialLeakMitigation)