Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@
one declaring neither axis returns `reply` unchanged.

Reads and mutations alike call it only at off-box egress
(`trace_egress/redact-reply-declarations`), because the app's own
`:reply-to` handler is entitled to the real values. A mutation's coarse
(`trace_egress/redact-reply-declarations`), on a continuation reply and on
a transport reply's result, because the handlers receiving them are
entitled to the real values. A mutation's coarse
claim is likewise applied only off-box (`trace_egress`). Matching is
index-free, so a declaration such as `[:data :email]` covers every item of
an infinite feed's merged list, as it does on the durable side."
Expand Down
94 changes: 54 additions & 40 deletions implementation/resources/src/re_frame/resources/trace_egress.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,32 @@
(seq (:sensitive spec)) (update :sensitive read-as callable)
(seq (:large spec)) (update :large read-as callable))))))

(defn- reply-owner-spec
"The registration a family continuation reply's declarations come from: a
read reply's resource, read the way the reply carries the data
(`feed-reply-spec`), or a mutation reply's `:mutation`. Nil for an
unregistered owner or a reply naming none."
[reply]
(if (resource-reply? reply)
(let [rid (second (:resource/key reply))]
(some-> (when (keyword? rid) (rf.resources.registry/resource-meta rid))
feed-reply-spec))
(let [mid (:mutation reply)]
(when (keyword? mid) (rf.resources.mutation-registry/mutation-meta mid)))))

(defn- redact-reply-declarations
"A family continuation reply with its owner's projection-relative
declarations redacted through `redact-continuation-reply`: a read reply's
resource, read the way the reply carries the data (`feed-reply-spec`), or a
mutation reply's `:mutation`. Needed because a spec that declares only paths
"`carrier`, a family continuation reply or a transport reply's result, with
its owner `spec`'s projection-relative declarations redacted through
`redact-continuation-reply`. Needed because a spec that declares only paths
classifies `:serialize`, so the coarse arm misses it while the durable entry
or instance redacts the same bytes. Applied at egress rather than source
because the app's own `:reply-to` handler is entitled to the real values. An
unregistered owner rides here: the coarse arm has already tokenized it."
[reply]
(let [spec (if (resource-reply? reply)
(let [rid (second (:resource/key reply))]
(some-> (when (keyword? rid) (rf.resources.registry/resource-meta rid))
feed-reply-spec))
(let [mid (:mutation reply)]
(when (keyword? mid) (rf.resources.mutation-registry/mutation-meta mid))))]
(if spec
(rf.resources.classification/redact-continuation-reply reply spec)
reply)))
because the handlers receiving the carrier are entitled to the real values.
A nil `spec`, an unregistered owner's, rides here: the coarse arm has already
tokenized it."
[carrier spec]
(if spec
(rf.resources.classification/redact-continuation-reply carrier spec)
carrier))

(defn- tokenize
"`v` tokenized content-free. A token rides as it is, because `redact-value`
Expand All @@ -399,7 +406,7 @@
(if (some? (get projected slot))
(update projected slot tokenize)
projected))
(if owner-redacts? reply (redact-reply-declarations reply))
(if owner-redacts? reply (redact-reply-declarations reply (reply-owner-spec reply)))
(cond-> reply-error-slot owner-redacts? (into reply-payload-slot)))))

(defn- carrier-family-value?
Expand Down Expand Up @@ -465,34 +472,41 @@
(family-work-id? (:work/id (nth v 1)))
(map? (nth v 2))))

(defn- transport-owner-redacts?
"Whether the owner a transport reply's verification `payload` names makes the
coarse claim: the resource its work id names, or a mutation attempt's
`:mutation-id`. An unregistered owner fails closed; a payload naming none
rides."
(defn- transport-owner
"The owner a transport reply's verification `payload` names, as
`[named? spec]`: the resource its work id names, or a mutation attempt's
`:mutation-id`, with that owner's registration (nil when unregistered).
`named?` is false for a payload naming neither."
[payload]
(let [subject (nth (:work/id payload) 1 nil)
disposition (fn [spec] (if spec
(rf.resources.classification/whole-entry-disposition spec)
:redact))]
(not= :serialize
(cond
(keyword? subject) (disposition (rf.resources.registry/resource-meta subject))
(keyword? (:mutation-id payload)) (disposition (rf.resources.mutation-registry/mutation-meta
(:mutation-id payload)))
:else :serialize))))
(let [subject (nth (:work/id payload) 1 nil)
mid (:mutation-id payload)]
(cond
(keyword? subject) [true (rf.resources.registry/resource-meta subject)]
(keyword? mid) [true (rf.resources.mutation-registry/mutation-meta mid)]
:else [false nil])))

(defn- project-transport-result
"A transport reply's `result` with the slots a continuation reply's would
take: `:value` tokenizes when the owner named by `payload` redacts, and the
`:error` envelope tokenizes unconditionally (`reply-error-slot`)."
take, from the owner `payload` names (`transport-owner`): `:value` tokenizes
when that owner makes the coarse claim, an unregistered owner failing closed,
and otherwise its declared paths substitute (`redact-reply-declarations`);
the `:error` envelope tokenizes unconditionally (`reply-error-slot`). A
feed's declaration reads as written rather than through `feed-reply-spec`,
because the result carries the single decoded page it is written against. A
payload naming no owner leaves `:value` riding."
[result payload]
(reduce (fn [projected slot]
(if (some? (get projected slot))
(update projected slot tokenize)
projected))
result
(cond-> reply-error-slot (transport-owner-redacts? payload) (conj :value))))
(let [[named? spec] (transport-owner payload)
owner-redacts? (and named?
(not= :serialize
(if spec
(rf.resources.classification/whole-entry-disposition spec)
:redact)))]
(reduce (fn [projected slot]
(if (some? (get projected slot))
(update projected slot tokenize)
projected))
(if owner-redacts? result (redact-reply-declarations result spec))
(cond-> reply-error-slot owner-redacts? (conj :value)))))

(defn- project-embedded-keys
"Projects the family's own data inside a carrier value `v` (`carrier-slot`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@

(def ^:private sunk (atom []))
(def ^:private managed-args (atom nil))
(def ^:private replied (atom nil))

(defn- request [_params _ctx] {:request {:method :get :url "/x"}})

(defn- init! []
(reset! sunk [])
(reset! managed-args nil)
(reset! replied nil)
(rf.observability/clear-observability-sinks!)
(rf/register-observability-sink! ::errors (fn [record] (swap! sunk conj record)))
(rf/make-frame {:id frame-id :observability {:errors [{:sink ::errors}]}})
Expand All @@ -57,8 +59,14 @@
(rf/reg-resource :r/plain {:scope {:from-db :t/caller-scope}
:params-schema [:map [:slug :string]]}
request)
(rf/reg-resource :r/paths {:scope {:from-db :t/caller-scope}
:params-schema [:map [:slug :string]]
:sensitive [[:token]]}
request)
(rf/reg-mutation :m/coarse {:params-schema [:map [:slug :string]] :sensitive? true} request)
(rf/reg-mutation :m/plain {:params-schema [:map [:slug :string]]} request)
(rf/reg-mutation :m/paths {:params-schema [:map [:slug :string]] :sensitive [[:token]]} request)
(rf/reg-event :app/replied (fn [_ [_ reply]] (reset! replied reply) {}))
(rf/reg-event :app/replied-and-failed (fn [_ _] (throw (ex-info "reply target failed" {})))))

(use-fixtures :each
Expand Down Expand Up @@ -222,3 +230,64 @@
:error {:kind :rf.http/http-5xx :body {:echo V}}})]
(is (every? #(redacted-token? (get-in % [2 :error])) copies)
"the failure envelope tokenizes whatever the owner")))))

;; ===========================================================================
;; a transport reply's result takes its owner's declared paths
;; ===========================================================================

;; An owner declaring only paths classifies `:serialize`, so its coarse claim
;; leaves the value alone and the declared paths substitute instead, as they do
;; on the continuation reply carrying the same value to its `:reply-to` target.

(defn- event-copies
"The off-box copies, event args opted in, of every event the frame ran that
`pred` accepts, by carrier: each epoch record's `:trigger-event` and its
trace's `:rf.event/v`."
[pred]
(let [records (mapv #(rf/project-egress % {:rf.egress/include-event-args? true})
(rf.epoch/epoch-history frame-id))
accepted (fn [vs] (filterv #(and (vector? %) (pred %)) vs))]
{:trigger-event (accepted (map :trigger-event records))
:rf.event/v (accepted (for [r records ev (:trace-events r)]
(get-in ev [:tags :rf.event/v])))}))

(deftest a-transport-replys-result-takes-its-owners-declared-paths
(doseq [[kind owner transport-id] [[:resource :r/paths :rf.resource.internal/succeeded]
[:mutation :m/paths :rf.mutation.internal/succeeded]]]
(testing (str kind)
(let [rows (atom [])]
(rf/register-listener! :trace ::rows (fn [ev] (swap! rows conj ev)))
(try
(rf/dispatch-sync (case kind
:resource [:rf.resource/ensure {:resource owner :params {:slug "p"}
:scope :rf.scope/global
:owner [:app :reader 1]
:reply-to [:app/replied]}]
:mutation [:rf.mutation/execute {:mutation owner :params {:slug "p"}
:instance [owner "p"]
:reply-to [:app/replied]}])
{:frame frame-id})
(rf/dispatch-sync (conj (:on-success @managed-args) {:status :ok :value {:token V :ok 1}})
{:frame frame-id})
(finally (rf/unregister-listener! :trace ::rows)))
(let [transport (event-copies #(= transport-id (first %)))
reply-to (event-copies #(and (= :app/replied (first %))
(= kind (get-in % [1 :rf.reply/work-kind]))))
on-box (filterv #(and (vector? %) (= transport-id (first %)))
(keep #(get-in % [:tags :rf.event/v]) @rows))]
(doseq [[copy vs] transport]
(is (seq vs) (str "the transport event rides " copy))
(is (every? #(= :rf/redacted (get-in % [2 :value :token])) vs)
(str "the declared path redacts on " copy))
(is (every? #(= 1 (get-in % [2 :value :ok])) vs)
(str "control: the undeclared sibling rides on " copy)))
(doseq [[copy vs] reply-to]
(is (seq vs) (str "the :reply-to target event rides " copy))
(is (every? #(= {:token :rf/redacted :ok 1} (get-in % [1 :value])) vs)
(str "control: the continuation reply redacts the path on " copy)))
(is (= [] (leak-paths [transport reply-to] V)) "no off-box copy carries the raw value")
(is (seq on-box) "the on-box trace carries the transport event")
(is (every? #(= {:token V :ok 1} (get-in % [2 :value])) on-box)
"the on-box trace keeps the raw value")
(is (= {:token V :ok 1} (:value @replied))
"the app's own :reply-to handler receives the raw value"))))))
Loading