Skip to content

A platform for community discussion. Free, open, simple.

DEV: Public `color_scheme` requests can disclose non-user-selectable theme color definitions and raw SCSS (#38497)

Isaac Janzen855ba4cf996b
diff --git a/app/controllers/stylesheets_controller.rb b/app/controllers/stylesheets_controller.rb
index a65ae6fa1e9..a5f9740613c 100644
--- a/app/controllers/stylesheets_controller.rb
+++ b/app/controllers/stylesheets_controller.rb
@@ -24,7 +24,10 @@ class StylesheetsController < ApplicationController
params.require("id")
params.permit("theme_id")
- manager = Stylesheet::Manager.new(theme_id: params[:theme_id]&.to_i)
+ theme_id = params[:theme_id]&.to_i
+ theme_id = nil if theme_id && !guardian.allow_themes?([theme_id])
+
+ manager = Stylesheet::Manager.new(theme_id: theme_id)
stylesheet = manager.color_scheme_stylesheet_details(params[:id], fallback_to_base: true)
render json: stylesheet
diff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rb
index 3063e278426..d19ecdefcd3 100644
--- a/spec/requests/stylesheets_controller_spec.rb
+++ b/spec/requests/stylesheets_controller_spec.rb
@@ -141,6 +141,30 @@ RSpec.describe StylesheetsController do
end
describe "#color_scheme" do
+ it "does not use a non-user-selectable theme_id for anonymous users" do
+ scheme = ColorScheme.create_from_base(name: "hidden scheme", colors: [])
+ non_selectable_theme = Fabricate(:theme, user_selectable: false, color_scheme_id: scheme.id)
+
+ get "/color-scheme-stylesheet/-1/#{non_selectable_theme.id}.json"
+
+ expect(response.status).to eq(200)
+ json = JSON.parse(response.body)
+ expect(json["color_scheme_id"]).not_to eq(scheme.id)
+ end
+
+ it "does not use a non-user-selectable theme_id for regular users" do
+ scheme = ColorScheme.create_from_base(name: "hidden scheme", colors: [])
+ non_selectable_theme = Fabricate(:theme, user_selectable: false, color_scheme_id: scheme.id)
+ user = Fabricate(:user)
+ sign_in(user)
+
+ get "/color-scheme-stylesheet/-1/#{non_selectable_theme.id}.json"
+
+ expect(response.status).to eq(200)
+ json = JSON.parse(response.body)
+ expect(json["color_scheme_id"]).not_to eq(scheme.id)
+ end
+
it "works as expected" do
scheme = ColorScheme.last
get "/color-scheme-stylesheet/#{scheme.id}.json"