2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Hijack do
|
2017-11-27 00:43:24 -06:00
|
|
|
class Hijack::Tester < ApplicationController
|
2017-11-23 22:31:23 -06:00
|
|
|
attr_reader :io
|
|
|
|
|
|
|
|
include Hijack
|
2017-11-27 00:43:24 -06:00
|
|
|
|
2017-11-27 23:47:20 -06:00
|
|
|
def initialize(env = {})
|
2017-11-23 22:31:23 -06:00
|
|
|
@io = StringIO.new
|
2017-11-27 23:47:20 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
env.merge!("rack.hijack" => lambda { @io }, "rack.input" => StringIO.new)
|
2017-11-27 23:47:20 -06:00
|
|
|
|
|
|
|
self.request = ActionController::TestRequest.new(env, nil, nil)
|
|
|
|
|
2017-11-27 00:43:24 -06:00
|
|
|
# we need this for the 418
|
2024-06-20 03:33:01 -05:00
|
|
|
set_response!(ActionDispatch::Response.new)
|
2017-11-23 22:31:23 -06:00
|
|
|
end
|
|
|
|
|
2017-11-27 00:43:24 -06:00
|
|
|
def hijack_test(&blk)
|
|
|
|
hijack(&blk)
|
2017-11-23 22:31:23 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-20 03:33:01 -05:00
|
|
|
let(:tester) { Hijack::Tester.new }
|
2017-11-23 22:31:23 -06:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "Request Tracker integration" do
|
2024-06-20 03:33:01 -05:00
|
|
|
let(:logger) do
|
2017-11-27 23:47:20 -06:00
|
|
|
lambda do |env, data|
|
|
|
|
@calls += 1
|
|
|
|
@status = data[:status]
|
|
|
|
@total = data[:timing][:total_duration]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Middleware::RequestTracker.register_detailed_request_logger logger
|
|
|
|
@calls = 0
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { Middleware::RequestTracker.unregister_detailed_request_logger logger }
|
2017-11-27 23:47:20 -06:00
|
|
|
|
|
|
|
it "can properly track execution" do
|
2023-01-09 05:18:21 -06:00
|
|
|
app =
|
|
|
|
lambda do |env|
|
|
|
|
tester = Hijack::Tester.new(env)
|
|
|
|
tester.hijack_test { render body: "hello", status: 201 }
|
2017-11-27 23:47:20 -06:00
|
|
|
end
|
|
|
|
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-17 14:27:30 -06:00
|
|
|
env = create_request_env(path: "/")
|
2017-11-27 23:47:20 -06:00
|
|
|
middleware = Middleware::RequestTracker.new(app)
|
|
|
|
|
|
|
|
middleware.call(env)
|
|
|
|
|
|
|
|
expect(@calls).to eq(1)
|
|
|
|
expect(@status).to eq(201)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-27 16:28:40 -06:00
|
|
|
it "dupes the request params and env" do
|
|
|
|
orig_req = tester.request
|
|
|
|
copy_req = nil
|
|
|
|
|
|
|
|
tester.hijack_test do
|
|
|
|
copy_req = request
|
|
|
|
render body: "hello world", status: 200
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(copy_req.object_id).not_to eq(orig_req.object_id)
|
|
|
|
end
|
|
|
|
|
2017-12-06 17:30:50 -06:00
|
|
|
it "handles cors" do
|
|
|
|
SiteSetting.cors_origins = "www.rainbows.com"
|
2023-12-01 06:57:11 -06:00
|
|
|
global_setting :enable_cors, true
|
2017-12-06 17:30:50 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
app =
|
|
|
|
lambda do |env|
|
|
|
|
tester = Hijack::Tester.new(env)
|
|
|
|
tester.hijack_test { render body: "hello", status: 201 }
|
2017-12-06 17:30:50 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(tester.io.string).to include("Access-Control-Allow-Origin: www.rainbows.com")
|
|
|
|
end
|
2017-12-06 17:30:50 -06:00
|
|
|
|
|
|
|
env = {}
|
|
|
|
middleware = Discourse::Cors.new(app)
|
|
|
|
middleware.call(env)
|
|
|
|
|
|
|
|
# it can do pre-flight
|
2023-01-09 05:18:21 -06:00
|
|
|
env = { "REQUEST_METHOD" => "OPTIONS", "HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET" }
|
2017-12-06 17:30:50 -06:00
|
|
|
|
|
|
|
status, headers, _body = middleware.call(env)
|
|
|
|
|
|
|
|
expect(status).to eq(200)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"Access-Control-Allow-Origin" => "www.rainbows.com",
|
2023-01-09 05:18:21 -06:00
|
|
|
"Access-Control-Allow-Headers" =>
|
|
|
|
"Content-Type, Cache-Control, X-Requested-With, X-CSRF-Token, Discourse-Present, User-Api-Key, User-Api-Client-Id, Authorization",
|
2020-10-28 21:01:06 -05:00
|
|
|
"Access-Control-Allow-Credentials" => "true",
|
2021-10-14 20:37:53 -05:00
|
|
|
"Access-Control-Allow-Methods" => "POST, PUT, GET, OPTIONS, DELETE",
|
|
|
|
"Access-Control-Max-Age" => "7200",
|
2020-10-28 21:01:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(headers).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes trailing slash in cors origin" do
|
|
|
|
GlobalSetting.stubs(:enable_cors).returns(true)
|
|
|
|
GlobalSetting.stubs(:cors_origin).returns("https://www.rainbows.com/")
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
app =
|
|
|
|
lambda do |env|
|
|
|
|
tester = Hijack::Tester.new(env)
|
|
|
|
tester.hijack_test { render body: "hello", status: 201 }
|
2020-10-28 21:01:06 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(tester.io.string).to include("Access-Control-Allow-Origin: https://www.rainbows.com")
|
|
|
|
end
|
2020-10-28 21:01:06 -05:00
|
|
|
|
|
|
|
env = {}
|
|
|
|
middleware = Discourse::Cors.new(app)
|
|
|
|
middleware.call(env)
|
|
|
|
|
|
|
|
# it can do pre-flight
|
2023-01-09 05:18:21 -06:00
|
|
|
env = { "REQUEST_METHOD" => "OPTIONS", "HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET" }
|
2020-10-28 21:01:06 -05:00
|
|
|
|
|
|
|
status, headers, _body = middleware.call(env)
|
|
|
|
|
|
|
|
expect(status).to eq(200)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"Access-Control-Allow-Origin" => "https://www.rainbows.com",
|
2023-01-09 05:18:21 -06:00
|
|
|
"Access-Control-Allow-Headers" =>
|
|
|
|
"Content-Type, Cache-Control, X-Requested-With, X-CSRF-Token, Discourse-Present, User-Api-Key, User-Api-Client-Id, Authorization",
|
2018-09-16 20:37:01 -05:00
|
|
|
"Access-Control-Allow-Credentials" => "true",
|
2021-10-14 20:37:53 -05:00
|
|
|
"Access-Control-Allow-Methods" => "POST, PUT, GET, OPTIONS, DELETE",
|
|
|
|
"Access-Control-Max-Age" => "7200",
|
2017-12-06 17:30:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(headers).to eq(expected)
|
|
|
|
end
|
|
|
|
|
2018-01-20 21:26:42 -06:00
|
|
|
it "handles transfers headers" do
|
|
|
|
tester.response.headers["Hello-World"] = "sam"
|
|
|
|
tester.hijack_test do
|
|
|
|
expires_in 1.year
|
|
|
|
render body: "hello world", status: 402
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tester.io.string).to include("Hello-World: sam")
|
|
|
|
end
|
|
|
|
|
2017-11-27 00:43:24 -06:00
|
|
|
it "handles expires_in" do
|
|
|
|
tester.hijack_test do
|
|
|
|
expires_in 1.year
|
|
|
|
render body: "hello world", status: 402
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tester.io.string).to include("max-age=31556952")
|
|
|
|
end
|
|
|
|
|
2017-11-23 22:31:23 -06:00
|
|
|
it "renders non 200 status if asked for" do
|
2023-01-09 05:18:21 -06:00
|
|
|
tester.hijack_test { render body: "hello world", status: 402 }
|
2017-11-23 22:31:23 -06:00
|
|
|
|
|
|
|
expect(tester.io.string).to include("402")
|
|
|
|
expect(tester.io.string).to include("world")
|
|
|
|
end
|
|
|
|
|
2017-11-27 17:59:53 -06:00
|
|
|
it "handles send_file correctly" do
|
2023-01-09 05:18:21 -06:00
|
|
|
tester.hijack_test { send_file __FILE__, disposition: nil }
|
2017-11-27 17:59:53 -06:00
|
|
|
|
|
|
|
expect(tester.io.string).to start_with("HTTP/1.1 200")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders a redirect correctly" do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(1.0)
|
2017-11-27 17:59:53 -06:00
|
|
|
tester.hijack_test do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(2.0)
|
2023-01-09 05:18:21 -06:00
|
|
|
redirect_to "http://awesome.com", allow_other_host: true
|
2017-11-27 17:59:53 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result =
|
2024-06-20 03:33:01 -05:00
|
|
|
"HTTP/1.1 302 Found\r\nLocation: http://awesome.com\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 0\r\nConnection: close\r\nX-Runtime: 1.000000\r\n\r\n"
|
2017-11-27 17:59:53 -06:00
|
|
|
expect(tester.io.string).to eq(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders stuff correctly if is empty" do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(1.0)
|
2017-11-27 17:59:53 -06:00
|
|
|
tester.hijack_test do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(2.0)
|
2017-11-27 17:59:53 -06:00
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result =
|
|
|
|
"HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 0\r\nConnection: close\r\nX-Runtime: 1.000000\r\n\r\n"
|
2017-11-27 17:59:53 -06:00
|
|
|
expect(tester.io.string).to eq(result)
|
|
|
|
end
|
|
|
|
|
2017-11-23 22:31:23 -06:00
|
|
|
it "renders stuff correctly if it works" do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(1.0)
|
2017-11-23 22:31:23 -06:00
|
|
|
tester.hijack_test do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(2.0)
|
2017-11-23 22:31:23 -06:00
|
|
|
render plain: "hello world"
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
result =
|
|
|
|
"HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 11\r\nConnection: close\r\nX-Runtime: 1.000000\r\n\r\nhello world"
|
2017-11-23 22:31:23 -06:00
|
|
|
expect(tester.io.string).to eq(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 500 by default" do
|
2018-01-18 15:26:18 -06:00
|
|
|
Process.stubs(:clock_gettime).returns(1.0)
|
2017-11-23 22:31:23 -06:00
|
|
|
tester.hijack_test
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expected =
|
|
|
|
"HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 0\r\nConnection: close\r\nX-Runtime: 0.000000\r\n\r\n"
|
2017-11-23 22:31:23 -06:00
|
|
|
expect(tester.io.string).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not run the block if io is closed" do
|
|
|
|
tester.io.close
|
|
|
|
|
|
|
|
ran = false
|
2023-01-09 05:18:21 -06:00
|
|
|
tester.hijack_test { ran = true }
|
2017-11-23 22:31:23 -06:00
|
|
|
|
|
|
|
expect(ran).to eq(false)
|
|
|
|
end
|
2023-07-28 06:53:51 -05:00
|
|
|
|
|
|
|
it "handles the queue being full" do
|
|
|
|
Scheduler::Defer.stubs(:later).raises(WorkQueue::WorkQueueFull.new)
|
|
|
|
|
|
|
|
tester.hijack_test {}
|
|
|
|
|
|
|
|
expect(tester.response.status).to eq(503)
|
|
|
|
end
|
2017-11-23 22:31:23 -06:00
|
|
|
end
|