Class: Spree::AgentTools::RecordSummary
- Inherits:
-
Object
- Object
- Spree::AgentTools::RecordSummary
- Defined in:
- lib/spree/agent_tools/record_summary.rb
Overview
Turns a record into the compact shape both the model and the panel read.
One shape serves both audiences deliberately: the model needs enough to
answer the question, and the panel needs enough to render a row the
merchant can click through to. Anything richer belongs in get_resource.
Constant Summary collapse
- SUBTITLE_FIELDS =
Fields worth showing beneath the title, in the order they are preferred. Status is deliberately absent — it has its own badge on the row, and repeating it would push out the field that actually adds something.
%w[display_total display_price price email number sku variant_sku].freeze
- DETAIL_FIELDS =
Quantities and places a list row must carry for the model to answer from the search alone. Without them a stock level row was an id and nothing else, and "how much do I have" took one lookup per row.
%w[count_on_hand available_count stock_location_name options_text].freeze
- SECRET_PATTERN =
Anything that looks like a credential is dropped before a record can reach the model. The serializers are supposed to mask these already — this is the second lock, because a serializer that gains a field later would otherwise send live secrets to an AI vendor with no code change here at all.
/(token|secret|password|api_key|access_key|private)/i.freeze
- SECRET_ASSOCIATIONS =
Whole associations that are never safe to forward, whatever their serializer happens to expose today. A gift card's code IS the bearer instrument — anyone holding it can spend the balance — and an order serializes its gift card unconditionally, so an order lookup would otherwise hand live codes to the AI vendor and write them into chat history. A plain
codematch is too broad: channels, promotions and commission rates all usecodeas an identifier merchants ask about. %w[gift_card gift_cards].freeze
- SECRET_VALUE_PATTERN =
A credential does not always sit under a credential-shaped key. Several serializers expose a URL whose path or query string IS the secret — an invitation's
acceptance_urlcarries the token that mints a staff account, a digital link'sdownload_urlis the customer's purchased file, and a data request's, media's, shipping label's and export's download links all address endpoints that authenticate on the link alone. Matching the key name cannot catch those, so the value is checked too.Admin::DataRequestSerializeralready deletes its own inherited link for exactly this reason; this generalises that decision to every outbound record, so a serializer that gains such a field later is covered without a change here. / [?&](?:token|secret|signature|sig|key)= # a credential in the query string | \/(?:accept-invitation|digital_links)\/ # or a path whose segment is the token /xi.freeze
- SECRET_ATTRIBUTES =
Keys whose value is a link that authenticates on itself. Named rather than pattern-matched, because a bare
_urlmatch would also drop the ordinary public URLs merchants ask about (a product's storefront link, a store's domain). %w[ acceptance_url download_url ].freeze
- LINK_SAFE_ATTRIBUTES =
Attributes a named resource may keep, because on that resource the value is an ordinary authenticated path rather than a link that authenticates on itself.
An export's download endpoint runs the Admin API's own scope check and
authorize_resource!, so the link grants nothing on its own and withholding it only sends the merchant to the dashboard for a file the agent just made for them. Keyed per attribute rather than per resource: exempting a whole record would also release any other credential-shaped field it later gains. { 'exports' => %w[download_url].freeze }.freeze
Class Method Summary collapse
- .call(entry:, record:, attributes: nil) ⇒ Hash
-
.from_payload(entry:, payload:) ⇒ Hash
A summary from what the Admin API returned, rather than from a record.
-
.sanitize(value, resource = nil) ⇒ Hash
Strips credential-shaped keys from a serialized record.
Class Method Details
.call(entry:, record:, attributes: nil) ⇒ Hash
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/spree/agent_tools/record_summary.rb', line 121 def call(entry:, record:, attributes: nil) attributes ||= serialize(entry, record) title = title_for(record, attributes) { id: record.prefixed_id, resource: entry.key, title: title, subtitle: subtitle_for(attributes, title), status: attributes['status'], image_url: attributes['thumbnail_url'], # The dashboard route this record lives at, so the panel links # straight to it instead of the merchant hunting for it. path: entry.dashboard_path_for(record) }.compact end |
.from_payload(entry:, payload:) ⇒ Hash
A summary from what the Admin API returned, rather than from a record.
The payload is already the serializer's own output, so there is no second serialization and no second set of association queries. It still passes the credential filter: the API may hand an authorised admin a gift card code, and a model is not an authorised admin.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/spree/agent_tools/record_summary.rb', line 149 def from_payload(entry:, payload:) attributes = sanitize(payload.to_h.stringify_keys, entry.key) title = title_from(attributes) { id: attributes['id'], resource: entry.key, title: title, subtitle: subtitle_for(attributes, title), status: attributes['status'], details: attributes.slice(*DETAIL_FIELDS).compact.presence, image_url: attributes['thumbnail_url'], path: dashboard_path_from(entry, attributes['id']) }.compact end |
.sanitize(value, resource = nil) ⇒ Hash
Strips credential-shaped keys from a serialized record.
Public because it is the ONE filter every outbound record must pass:
get_resource emits a full serializer hash rather than a summary row,
and without this it would bypass the protection entirely.
Recursive, because serializers nest: an order carries a gift card, a customer carries addresses. A shallow reject would scrub the top level and hand the vendor whatever sat one layer down, which is the failure this filter exists to prevent.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/spree/agent_tools/record_summary.rb', line 88 def sanitize(value, resource = nil) case value when Hash exempt = LINK_SAFE_ATTRIBUTES.fetch(resource.to_s, []) value.to_h.stringify_keys.each_with_object({}) do |(key, nested), result| link_safe = exempt.include?(key) next if key.match?(SECRET_PATTERN) || SECRET_ASSOCIATIONS.include?(key) next if SECRET_ATTRIBUTES.include?(key) && !link_safe next if credential_value?(nested) && !link_safe # Only the record's own keys are exempt. A nested association # carries its own rules, so recursion drops the exemption. result[key] = sanitize(nested) end when Array # Elements are filtered too, not just hash values: a customer's or # order's tags are caller-controlled and serialized as a bare # string array, so a tag holding a token-bearing URL would # otherwise pass straight through. value.reject { |element| credential_value?(element) }.map { |element| sanitize(element) } else value end end |