FIX: Better error handling for invalid locale bundle versions

This commit is contained in:
Gerhard Schlager 2019-11-11 22:30:31 +01:00
parent 34665d3f96
commit 6ebffaaf6e
2 changed files with 17 additions and 4 deletions

View File

@ -9,15 +9,20 @@ class ExtraLocalesController < ApplicationController
:verify_authenticity_token
OVERRIDES_BUNDLE ||= 'overrides'
MD5_HASH_LENGTH ||= 32
def show
bundle = params[:bundle]
raise Discourse::InvalidAccess.new if !valid_bundle?(bundle)
if params[:v]&.size == 32
hash = ExtraLocalesController.bundle_js_hash(bundle)
immutable_for(1.year) if hash == params[:v]
version = params[:v]
if version.present?
if version.kind_of?(String) && version.length == MD5_HASH_LENGTH
hash = ExtraLocalesController.bundle_js_hash(bundle)
immutable_for(1.year) if hash == version
else
raise Discourse::InvalidParameters.new(:v)
end
end
render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript"

View File

@ -23,6 +23,14 @@ describe ExtraLocalesController do
expect(response.status).to eq(403)
end
it "requires a valid version" do
get "/extra-locales/overrides", params: { v: 'a' }
expect(response.status).to eq(400)
get "/extra-locales/overrides?v[foo]=1"
expect(response.status).to eq(400)
end
context "logged in as a moderator" do
let(:moderator) { Fabricate(:moderator) }