Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Never hand back a tracking link that is not a real address

Damian Legawiec53df90a85990
diff --git a/spree/core/app/models/spree/delivery.rb b/spree/core/app/models/spree/delivery.rb
index 4c7281f6ee..5932c22e5d 100644
--- a/spree/core/app/models/spree/delivery.rb
+++ b/spree/core/app/models/spree/delivery.rb
@@ -93,7 +93,7 @@ module Spree
def resolved_tracking_url
return if tracking_number.blank?
- tracking_url.presence ||
+ safe_tracking_url ||
(tracking_number if pasted_link?) ||
provider_tracking_url.presence ||
carrier_tracking_url.presence ||
@@ -108,6 +108,21 @@ module Spree
private
+ # The stored link is free text a merchant or seller pasted, and it is
+ # rendered as an href by every consumer of this field. Only an http(s)
+ # address with a host is handed back, so a `javascript:` or `data:` value
+ # is dropped rather than executed — the same guard ShippingLabel#file_url
+ # applies to a provider-supplied URL.
+ def safe_tracking_url
+ value = tracking_url.presence
+ return if value.blank?
+
+ uri = URI.parse(value)
+ value if %w[http https].include?(uri.scheme) && uri.host.present?
+ rescue URI::InvalidURIError
+ nil
+ end
+
def provider_tracking_url
return unless owner.respond_to?(:provider)
diff --git a/spree/core/spec/models/spree/delivery_spec.rb b/spree/core/spec/models/spree/delivery_spec.rb
index be636719b0..694da90e43 100644
--- a/spree/core/spec/models/spree/delivery_spec.rb
+++ b/spree/core/spec/models/spree/delivery_spec.rb
@@ -168,4 +168,23 @@ RSpec.describe Spree::Delivery, type: :model do
expect(delivery.correction_attributes(tracking_number: ' 1Z_OLD ')).not_to have_key(:status)
end
end
+ describe '#resolved_tracking_url' do
+ # The column is free text a merchant or seller pastes, and every consumer
+ # renders it as a link — so a scripting scheme must never come back out.
+ it 'drops a link that is not a real http address' do
+ ['javascript:alert(1)', 'data:text/html,<script>alert(1)</script>', 'file:///etc/passwd'].each do |hostile|
+ delivery = build(:delivery, owner: fulfillment, store: store,
+ tracking_number: '1Z999', tracking_url: hostile)
+
+ expect(delivery.resolved_tracking_url).not_to eq(hostile)
+ end
+ end
+
+ it 'keeps a link the merchant legitimately pasted' do
+ delivery = build(:delivery, owner: fulfillment, store: store, tracking_number: '1Z999',
+ tracking_url: 'https://forwarder.example/track/1Z999')
+
+ expect(delivery.resolved_tracking_url).to eq('https://forwarder.example/track/1Z999')
+ end
+ end
end