SECURITY: moderator shouldn't be able to import a theme via API (#18418)

* SECURITY: moderator shouldn't be able to import a theme via API.
* DEV: apply `AdminConstraint` for all the "themes" routes.

Co-authored-by: Vinoth Kannan <svkn.87@gmail.com>
This commit is contained in:
Jarek Radosz 2022-09-29 20:00:20 +02:00 committed by GitHub
parent ba139b8c23
commit ae1e536e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 7 deletions

View File

@ -5,6 +5,7 @@ require 'base64'
class Admin::ThemesController < Admin::AdminController
skip_before_action :check_xhr, only: [:show, :preview, :export]
before_action :ensure_admin
def preview
theme = Theme.find_by(id: params[:id])

View File

@ -206,13 +206,17 @@ Discourse::Application.routes.draw do
get "customize/embedding" => "embedding#show", constraints: AdminConstraint.new
put "customize/embedding" => "embedding#update", constraints: AdminConstraint.new
resources :themes, constraints: AdminConstraint.new
post "themes/import" => "themes#import"
post "themes/upload_asset" => "themes#upload_asset"
post "themes/generate_key_pair" => "themes#generate_key_pair"
get "themes/:id/preview" => "themes#preview"
put "themes/:id/setting" => "themes#update_single_setting"
resources :themes, constraints: AdminConstraint.new do
member do
get "preview" => "themes#preview"
put "setting" => "themes#update_single_setting"
end
collection do
post "import" => "themes#import"
post "upload_asset" => "themes#upload_asset"
post "generate_key_pair" => "themes#generate_key_pair"
end
end
scope "/customize", constraints: AdminConstraint.new do
resources :user_fields, constraints: AdminConstraint.new

View File

@ -160,6 +160,13 @@ RSpec.describe Admin::ThemesController do
expect(response.status).to eq(201)
end
it 'should not be able to import a theme by moderator' do
sign_in(Fabricate(:moderator))
post "/admin/themes/import.json", params: { theme: theme_json_file }
expect(response.status).to eq(404)
end
it 'imports a theme' do
post "/admin/themes/import.json", params: { theme: theme_json_file }
expect(response.status).to eq(201)