A platform for community discussion. Free, open, simple.
DEV: Better protection for drafts
Nat357febfb479c
diff --git a/app/controllers/drafts_controller.rb b/app/controllers/drafts_controller.rbindex a40c57a9520..c82b58a2a74 100644--- a/app/controllers/drafts_controller.rb+++ b/app/controllers/drafts_controller.rb@@ -6,6 +6,7 @@ class DraftsController < ApplicationControllerskip_before_action :check_xhr, :preload_jsonINDEX_LIMIT = 50+ BULK_DESTROY_LIMIT = 30def indexparams.permit(:offset)@@ -38,7 +39,7 @@ class DraftsController < ApplicationControllerdef createraise Discourse::NotFound.new if params[:draft_key].blank?- if params[:data].size > SiteSetting.max_draft_length+ if !params[:data].is_a?(String) || params[:data].size > SiteSetting.max_draft_lengthraise Discourse::InvalidParameters.new(:data)end@@ -103,7 +104,7 @@ class DraftsController < ApplicationControlleroriginal_tags = data["original_tags"]if original_text.present?- if post = Post.find_by(id: data["postId"])+ if post = Draft.allowed_draft_posts_for_user(current_user).find_by(id: data["postId"])conflict = original_text != post.rawif post.post_number == 1@@ -147,8 +148,8 @@ class DraftsController < ApplicationControllerrescue Draft::OutOfSequence# nothing really we can do here, if try clearing a draft that is not ours, just skip it.# rendering an error causes issues in the composer- rescue StandardError => e- return render json: failed_json.merge(errors: e), status: :unauthorized+ rescue StandardError+ return render json: failed_json, status: :unauthorizedendrender json: success_json@@ -158,6 +159,13 @@ class DraftsController < ApplicationControllerparams.require(:draft_keys)draft_keys = params[:draft_keys]++ if draft_keys.length > BULK_DESTROY_LIMIT+ raise Discourse::InvalidParameters.new(+ I18n.t("draft.bulk_destroy_limit", limit: BULK_DESTROY_LIMIT),+ )+ end+sequences = params[:sequences] || {}return render json: success_json.merge(deleted_count: 0) if draft_keys.empty?@@ -204,8 +212,8 @@ class DraftsController < ApplicationController# Update user draft countUserStat.update_draft_count(user.id)end- rescue StandardError => e- return render json: failed_json.merge(errors: e.message), status: :internal_server_error+ rescue StandardError+ return render json: failed_json, status: :internal_server_errorendrender json: success_json.merge(deleted_count: deleted_count)diff --git a/app/models/upload.rb b/app/models/upload.rbindex 2cda4c821d6..b21939a048b 100644--- a/app/models/upload.rb+++ b/app/models/upload.rb@@ -11,6 +11,8 @@ class Upload < ActiveRecord::BaseURL_REGEX = %r{(/original/\dX[/\.\w]*/(\h+)[\.\w]*)}MAX_IDENTIFY_SECONDS = 5DOMINANT_COLOR_COMMAND_TIMEOUT_SECONDS = 5+ # the maximum length of a base62 encoded sha1+ MAX_BASE62_SHA1_LENGTH = 27belongs_to :userbelongs_to :access_control_post, class_name: "Post"@@ -470,6 +472,7 @@ class Upload < ActiveRecord::Baseenddef self.sha1_from_base62_encoded(encoded_sha1)+ return nil if encoded_sha1.length > MAX_BASE62_SHA1_LENGTHsha1 = Base62.decode(encoded_sha1).to_s(16)if sha1.length > SHA1_LENGTHdiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex e071b92a376..3f24e3c9ef3 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1130,6 +1130,7 @@ en:too_many_drafts:title: "Too many drafts"description: "You have reached the maximum number of allowed drafts. Please delete some of [your drafts](%{base_url}/my/activity/drafts) and try again."+ bulk_destroy_limit: "You can only delete up to %{limit} drafts at once."draft_backup:pm_title: "Backup Drafts from ongoing topics"pm_body: "Topic containing backup drafts"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex d004cb8b2b5..115d93da8db 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1700,7 +1700,8 @@ posting:default: falsearea: "posts_and_topics"max_draft_length:- default: 400_000+ default: 150_000+ max: 150_000hidden: truemax_drafts_per_user:default: 10_000diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rbindex ebf895478bf..4314f47d437 100644--- a/spec/models/upload_spec.rb+++ b/spec/models/upload_spec.rb@@ -860,6 +860,13 @@ RSpec.describe Upload doendend+ describe ".sha1_from_base62_encoded" do+ it "rejects base62 strings that are too long" do+ long_base62 = "A" * 1000+ expect(Upload.sha1_from_base62_encoded(long_base62)).to be_nil+ end+ end+def enable_secure_uploadssetup_s3SiteSetting.secure_uploads = truediff --git a/spec/requests/drafts_controller_spec.rb b/spec/requests/drafts_controller_spec.rbindex 42ace161243..b5fac339d71 100644--- a/spec/requests/drafts_controller_spec.rb+++ b/spec/requests/drafts_controller_spec.rb@@ -320,6 +320,15 @@ RSpec.describe DraftsController doexpect(response).to have_http_status :bad_requestendend++ context "when data is not a string" do+ before { sign_in(user) }++ it "returns an error" do+ post "/drafts.json", params: { draft_key: "xyz", data: { cat: "tomtom" }, sequence: 0 }+ expect(response).to have_http_status :bad_request+ end+ endendit "returns 403 when the maximum amount of drafts per users is reached" do@@ -360,6 +369,25 @@ RSpec.describe DraftsController doexpect(Draft.where(user_id: user1.id).count).to eq(2)expect(Draft.where(user_id: user2.id).count).to eq(1)end++ it "does not leak conflict info for posts user cannot see" do+ private_post = Fabricate(:private_message_post)++ sign_in(user)+ post "/drafts.json",+ params: {+ draft_key: "topic_#{private_post.topic_id}",+ sequence: 0,+ data: {+ postId: private_post.id,+ action: "edit",+ original_text: "wrong text to trigger conflict",+ }.to_json,+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body).not_to have_key("conflict_user")+ endenddescribe "#destroy" do@@ -533,6 +561,18 @@ RSpec.describe DraftsController doexpect(response.status).to eq(400)end+ it "rejects too many draft_keys" do+ sign_in(user)++ keys = (1..31).map { |i| "key_#{i}" }+ delete "/drafts/bulk_destroy.json", params: { draft_keys: keys }++ expect(response.status).to eq(400)+ expect(response.parsed_body["errors"].first).to include(+ I18n.t("draft.bulk_destroy_limit", limit: DraftsController::BULK_DESTROY_LIMIT),+ )+ end+it "updates user draft count after bulk deletion" dosign_in(user)