2017-12-06 17:30:50 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Discourse::Cors
|
|
|
|
ORIGINS_ENV = "Discourse_Cors_Origins"
|
|
|
|
|
|
|
|
def initialize(app, options = nil)
|
|
|
|
@app = app
|
|
|
|
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?
|
2020-10-28 21:01:06 -05:00
|
|
|
@global_origins = GlobalSetting.cors_origin.split(",").map { |x| x.strip.chomp("/") }
|
2014-07-23 02:03:52 -05:00
|
|
|
end
|
2017-12-06 17:30:50 -06:00
|
|
|
end
|
2014-07-23 02:03:52 -05:00
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
def call(env)
|
2023-12-01 06:57:11 -06:00
|
|
|
return @app.call(env) if !GlobalSetting.enable_cors && !GlobalSetting.cdn_url
|
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
cors_origins = @global_origins || []
|
|
|
|
cors_origins += SiteSetting.cors_origins.split("|") if SiteSetting.cors_origins.present?
|
|
|
|
cors_origins = cors_origins.presence
|
2015-05-14 10:14:29 -05:00
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
if env["REQUEST_METHOD"] == ("OPTIONS") && env["HTTP_ACCESS_CONTROL_REQUEST_METHOD"]
|
|
|
|
return 200, Discourse::Cors.apply_headers(cors_origins, env, {}), []
|
2015-05-14 10:14:29 -05:00
|
|
|
end
|
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
env[Discourse::Cors::ORIGINS_ENV] = cors_origins if cors_origins
|
2015-05-14 10:14:29 -05:00
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
headers ||= {}
|
2011-10-15 13:00:00 -05:00
|
|
|
|
2021-01-28 20:14:49 -06:00
|
|
|
Discourse::Cors.apply_headers(cors_origins, env, headers)
|
2014-07-23 02:03:52 -05:00
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
[status, headers, body]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.apply_headers(cors_origins, env, headers)
|
2021-01-28 20:14:49 -06:00
|
|
|
request_method = env["REQUEST_METHOD"]
|
2017-12-06 17:30:50 -06:00
|
|
|
|
2023-12-01 06:57:11 -06:00
|
|
|
if headers["Access-Control-Allow-Origin"]
|
|
|
|
# Already configured. Probably by ApplicationController#apply_cdn_headers
|
2021-01-28 20:14:49 -06:00
|
|
|
elsif cors_origins
|
|
|
|
origin = nil
|
2017-12-06 17:30:50 -06:00
|
|
|
if origin = env["HTTP_ORIGIN"]
|
|
|
|
origin = nil unless cors_origins.include?(origin)
|
2014-07-23 02:03:52 -05:00
|
|
|
end
|
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
headers["Access-Control-Allow-Origin"] = origin || cors_origins[0]
|
2020-03-26 01:35:32 -05:00
|
|
|
headers[
|
|
|
|
"Access-Control-Allow-Headers"
|
|
|
|
] = "Content-Type, Cache-Control, X-Requested-With, X-CSRF-Token, Discourse-Present, User-Api-Key, User-Api-Client-Id, Authorization"
|
2017-12-06 17:30:50 -06:00
|
|
|
headers["Access-Control-Allow-Credentials"] = "true"
|
2018-09-16 20:01:08 -05:00
|
|
|
headers["Access-Control-Allow-Methods"] = "POST, PUT, GET, OPTIONS, DELETE"
|
2021-10-14 20:37:53 -05:00
|
|
|
headers["Access-Control-Max-Age"] = "7200"
|
2013-04-22 04:16:58 -05:00
|
|
|
end
|
2017-12-06 17:30:50 -06:00
|
|
|
|
|
|
|
headers
|
2013-04-22 04:16:58 -05:00
|
|
|
end
|
2017-12-06 17:30:50 -06:00
|
|
|
end
|
2014-07-23 02:03:52 -05:00
|
|
|
|
2023-12-01 06:57:11 -06:00
|
|
|
Rails.configuration.middleware.insert_before ActionDispatch::Flash, Discourse::Cors
|