From c19f06ff4b43e0474c7d752c194d3dcd18ee73c9 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 6 Aug 2026 07:51:27 +0000 Subject: [PATCH] :bug: Validate version parameter in import-binfile Restrict version parameter to supported values (1 or 3) via schema validation instead of accepting any integer. Add content-based format detection when version is not provided, using bfc/parse-file-format to inspect file magic bytes. Closes #11105 AI-assisted-by: qwen3.7-plus --- backend/src/app/rpc/commands/binfile.clj | 31 ++++++++++++------ .../test/backend_tests/rpc_binfile_test.clj | 32 +++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index 78ba08e5fd..78363078b3 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -104,7 +104,11 @@ (try (case (int version) 1 (bf.v1/import-files! cfg) - 3 (bf.v3/import-files! cfg)) + 3 (bf.v3/import-files! cfg) + (throw (ex-info (str "Unsupported binfile version: " version) + {:type :validation + :code :unsupported-version + :version version}))) (finally (when owned? (fs/delete input-path))))] @@ -122,7 +126,7 @@ [:name [:or [:string {:max 250}] [:map-of ::sm/uuid [:string {:max 250}]]]] [:project-id ::sm/uuid] - [:version {:optional true} ::sm/int] + [:version {:optional true} [:enum 1 3]] [:file {:optional true} media.v/schema:upload] [:upload-id {:optional true} ::sm/uuid]] [:fn {:error/message "one of :file or :upload-id is required"} @@ -145,21 +149,28 @@ ::sm/params schema:import-binfile} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id project-id version upload-id] :as params}] (projects/check-edition-permissions! pool profile-id project-id) - (let [version (or version 3) + (let [params (if (some? upload-id) + (let [file (db/tx-run! cfg media-cmd/assemble-chunks upload-id)] + (assoc params :file file)) + params) + + version (or version + (case (bfc/parse-file-format (-> params :file :path)) + :binfile-v1 1 + :binfile-v3 3)) + params (-> params (assoc :profile-id profile-id) (assoc :version version)) - params - (if (some? upload-id) - (let [file (db/tx-run! cfg media-cmd/assemble-chunks upload-id)] - (assoc params :file file)) - params) - manifest (case (int version) 1 nil - 3 (bf.v3/get-manifest (-> params :file :path)))] + 3 (bf.v3/get-manifest (-> params :file :path)) + (throw (ex-info (str "Unsupported binfile version: " version) + {:type :validation + :code :unsupported-version + :version version})))] (with-meta (sse/response (partial import-binfile cfg params)) diff --git a/backend/test/backend_tests/rpc_binfile_test.clj b/backend/test/backend_tests/rpc_binfile_test.clj index 5ebf83bf18..2fb8917039 100644 --- a/backend/test/backend_tests/rpc_binfile_test.clj +++ b/backend/test/backend_tests/rpc_binfile_test.clj @@ -40,3 +40,35 @@ ;; (Currently this will fail because file-id is still in schema) (t/is (false? (validator params-with-file-id)) "params with file-id should be rejected"))) + +(t/deftest import-binfile-schema-rejects-unsupported-version + ;; T1-N2-03: version parameter should be restricted to supported values (1 or 3) + (let [schema @#'binfile/schema:import-binfile + validator (sm/lazy-validator schema) + base-params {:name "test" + :project-id (uuid/random) + :upload-id (uuid/random)}] + + ;; Version 1 should be accepted + (t/is (true? (validator (assoc base-params :version 1))) + "version 1 should be valid") + + ;; Version 3 should be accepted + (t/is (true? (validator (assoc base-params :version 3))) + "version 3 should be valid") + + ;; Version 2 should be rejected + (t/is (false? (validator (assoc base-params :version 2))) + "version 2 should be rejected") + + ;; Version 0 should be rejected + (t/is (false? (validator (assoc base-params :version 0))) + "version 0 should be rejected") + + ;; Negative version should be rejected + (t/is (false? (validator (assoc base-params :version -1))) + "negative version should be rejected") + + ;; Version 4 should be rejected + (t/is (false? (validator (assoc base-params :version 4))) + "version 4 should be rejected")))