-
Notifications
You must be signed in to change notification settings - Fork 480
Add shop validation #1443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add shop validation #1443
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # typed: strict | ||
| # frozen_string_literal: true | ||
|
|
||
| module ShopifyAPI | ||
| module Errors | ||
| class InvalidShopError < StandardError | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # typed: strict | ||
| # frozen_string_literal: true | ||
|
|
||
| require "addressable/uri" | ||
|
|
||
| module ShopifyAPI | ||
| module Utils | ||
| module ShopValidator | ||
| TRUSTED_SHOPIFY_DOMAINS = T.let( | ||
| [ | ||
| "shopify.com", | ||
| "myshopify.io", | ||
| "myshopify.com", | ||
| "spin.dev", | ||
| "shop.dev", | ||
| ].freeze, | ||
| T::Array[String], | ||
| ) | ||
|
|
||
| class << self | ||
| extend T::Sig | ||
|
|
||
| sig do | ||
| params( | ||
| shop_domain: String, | ||
| myshopify_domain: T.nilable(String), | ||
| ).returns(T.nilable(String)) | ||
| end | ||
| def sanitize_shop_domain(shop_domain, myshopify_domain: nil) | ||
| uri = uri_from_shop_domain(shop_domain, myshopify_domain) | ||
| return nil if uri.nil? || uri.host.nil? || uri.host.empty? | ||
|
|
||
| trusted_domains(myshopify_domain).each do |trusted_domain| | ||
| host = T.cast(uri.host, String) | ||
| uri_domain = uri.domain | ||
| next if uri_domain.nil? | ||
|
|
||
| no_shop_name_in_subdomain = host == trusted_domain | ||
| from_trusted_domain = trusted_domain == uri_domain | ||
|
|
||
| if unified_admin?(uri) && from_trusted_domain | ||
| return myshopify_domain_from_unified_admin(uri) | ||
| end | ||
| return nil if no_shop_name_in_subdomain || host.empty? | ||
| return host if from_trusted_domain | ||
| end | ||
| nil | ||
| end | ||
|
|
||
| sig do | ||
| params( | ||
| shop: String, | ||
| myshopify_domain: T.nilable(String), | ||
| ).returns(String) | ||
| end | ||
| def sanitize!(shop, myshopify_domain: nil) | ||
| host = sanitize_shop_domain(shop, myshopify_domain: myshopify_domain) | ||
| if host.nil? || host.empty? | ||
| raise Errors::InvalidShopError, | ||
| "shop must be a trusted Shopify domain (see ShopValidator::TRUSTED_SHOPIFY_DOMAINS), got: #{shop.inspect}" | ||
| end | ||
|
|
||
| host | ||
| end | ||
|
|
||
| private | ||
|
|
||
| sig { params(myshopify_domain: T.nilable(String)).returns(T::Array[String]) } | ||
| def trusted_domains(myshopify_domain) | ||
| trusted = TRUSTED_SHOPIFY_DOMAINS.dup | ||
| if myshopify_domain && !myshopify_domain.to_s.empty? | ||
| trusted << myshopify_domain | ||
| trusted.uniq! | ||
| end | ||
| trusted | ||
| end | ||
|
|
||
| sig do | ||
| params( | ||
| shop_domain: String, | ||
| myshopify_domain: T.nilable(String), | ||
| ).returns(T.nilable(Addressable::URI)) | ||
| end | ||
| def uri_from_shop_domain(shop_domain, myshopify_domain) | ||
| name = shop_domain.to_s.downcase.strip | ||
| return nil if name.empty? | ||
| return nil if name.include?("@") | ||
|
|
||
| if myshopify_domain && !myshopify_domain.to_s.empty? && | ||
| !name.include?(myshopify_domain.to_s) && !name.include?(".") | ||
| name += ".#{myshopify_domain}" | ||
| end | ||
|
|
||
| uri = Addressable::URI.parse(name) | ||
| if uri.scheme.nil? | ||
| name = "https://#{name}" | ||
| uri = Addressable::URI.parse(name) | ||
| end | ||
|
|
||
| uri | ||
| rescue Addressable::URI::InvalidURIError | ||
| nil | ||
| end | ||
|
|
||
| sig { params(uri: Addressable::URI).returns(T::Boolean) } | ||
| def unified_admin?(uri) | ||
| T.cast(uri.host, String).split(".").first == "admin" | ||
| end | ||
|
|
||
| sig { params(uri: Addressable::URI).returns(String) } | ||
| def myshopify_domain_from_unified_admin(uri) | ||
| shop = uri.path.to_s.split("/").last | ||
| "#{shop}.myshopify.com" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.