Add protection for version inconsistency on opening or editing file

This commit is contained in:
Andrey Antukh 2023-11-29 17:13:54 +01:00 committed by Andrés Moya
parent ad0378270f
commit afa735a9c1
5 changed files with 80 additions and 60 deletions

View file

@ -11,7 +11,7 @@
[app.common.exceptions :as ex]
[app.common.features :as cfeat]
[app.common.files.helpers :as cfh]
[app.common.files.migrations :as pmg]
[app.common.files.migrations :as fmg]
[app.common.schema :as sm]
[app.common.schema.desc-js-like :as-alias smdj]
[app.common.spec :as us]
@ -68,6 +68,22 @@
changes (assoc :changes (blob/decode changes))
data (assoc :data (blob/decode data)))))
(defn check-version!
[{:keys [data] :as file}]
(dm/assert!
"expect data to be decoded"
(map? data))
(let [version (:version data 0)]
(when (> version fmg/version)
(ex/raise :type :restriction
:code :file-version-not-supported
:hint "file version is greated that the maximum"
:file-version version
:max-version fmg/version))
file))
;; --- FILE PERMISSIONS
(def ^:private sql:file-permissions
@ -283,7 +299,7 @@
file (-> (db/get conn :file params)
(decode-row)
(pmg/migrate-file))]
(fmg/migrate-file))]
;; NOTE: when file is migrated, we break the rule of no perform
;; mutations on get operations and update the file with all
@ -292,7 +308,7 @@
;; NOTE: the following code will not work on read-only mode, it
;; is a known issue; we keep is not implemented until we really
;; need this
(if (pmg/migrated? file)
(if (fmg/migrated? file)
(let [file (update file :features cfeat/migrate-legacy-features)
features (set/union (deref cfeat/*new*) (:features file))]
(db/update! conn :file
@ -328,7 +344,8 @@
:file-id id)
file (-> (get-file conn id project-id)
(assoc :permissions perms))
(assoc :permissions perms)
(check-version!))
_ (-> (cfeat/get-team-enabled-features cf/flags team)
(cfeat/check-client-features! (:features params))
@ -824,7 +841,7 @@
into the file local libraries"
[conn {:keys [id] :as library}]
(let [ldata (binding [pmap/*load-fn* (partial load-pointer conn id)]
(-> library decode-row (process-pointers deref) pmg/migrate-file :data))
(-> library decode-row (process-pointers deref) fmg/migrate-file :data))
rows (db/exec! conn [sql:get-referenced-files id])]
(doseq [file-id (map :id rows)]
(binding [pmap/*load-fn* (partial load-pointer conn file-id)
@ -834,7 +851,7 @@
::db/remove-deleted? false)
(decode-row)
(load-all-pointers!)
(pmg/migrate-file))
(fmg/migrate-file))
data (ctf/absorb-assets (:data file) ldata)]
(db/update! conn :file
{:revn (inc (:revn file))