mirror of
https://github.com/discourse/discourse.git
synced 2025-02-21 22:59:57 +08:00
SECURITY: theme key should be an anon cache breaker
This commit is contained in:
parent
8f48c20598
commit
ac1f84d3e1
@ -15,6 +15,7 @@ module Middleware
|
|||||||
|
|
||||||
def initialize(env)
|
def initialize(env)
|
||||||
@env = env
|
@env = env
|
||||||
|
@request = Rack::Request.new(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_mobile=(val)
|
def is_mobile=(val)
|
||||||
@ -54,7 +55,16 @@ module Middleware
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cache_key
|
def cache_key
|
||||||
@cache_key ||= "ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}|b=#{has_brotli?}"
|
@cache_key ||= "ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}|b=#{has_brotli?}|t=#{theme_key}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def theme_key
|
||||||
|
key = @request.cookies['theme_key']
|
||||||
|
if key && Guardian.new.allow_theme?(key)
|
||||||
|
key
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_key_body
|
def cache_key_body
|
||||||
|
@ -31,6 +31,21 @@ describe Middleware::AnonymousCache::Helper do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "per theme cache" do
|
||||||
|
it "handles theme keys" do
|
||||||
|
theme = Theme.create(name: "test", user_id: -1, user_selectable: true)
|
||||||
|
|
||||||
|
with_bad_theme_key = new_helper("HTTP_COOKIE" => "theme_key=abc").cache_key
|
||||||
|
with_no_theme_key = new_helper().cache_key
|
||||||
|
|
||||||
|
expect(with_bad_theme_key).to eq(with_no_theme_key)
|
||||||
|
|
||||||
|
with_good_theme_key = new_helper("HTTP_COOKIE" => "theme_key=#{theme.key}").cache_key
|
||||||
|
|
||||||
|
expect(with_good_theme_key).not_to eq(with_no_theme_key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "cached" do
|
context "cached" do
|
||||||
let!(:helper) do
|
let!(:helper) do
|
||||||
new_helper("ANON_CACHE_DURATION" => 10)
|
new_helper("ANON_CACHE_DURATION" => 10)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user