-
Notifications
You must be signed in to change notification settings - Fork 29
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
[Theme Check] Add allowedDomains check for RemoteAsset theme checker #711
base: main
Are you sure you want to change the base?
Conversation
const urlObj = new URL(url); | ||
return SHOPIFY_CDN_DOMAINS.includes(urlObj.hostname); | ||
return [...SHOPIFY_CDN_DOMAINS, ...allowedDomains].includes(urlObj.hostname); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should allowedDomains be normalized before doing the include check? IIRC .includes
compares using strict equality, this would fail in the case a user puts in http://domain.com
in their config since urlObj.hostName
would compare domain.com
with http://domain.com
which does not match exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charlespwd good point, inline with how we check i've introduced a normalise func that takes an array of strings and returns a new array of mapped strings.
String normalisation follows web URL standard and returns the hostname for comparison.
Enhances developer experience by validating the remote assets (scripts, stylesheets) are loaded not only from Shopify CDNs but now approved domains. This prevents potential performance risks and incorrect errors from loading resources from untrusted sources, forcing developers to go and enable a source if it's _required_ The implementation includes: - Schema property for configuring allowedDomains - Domain validation against configured allow list - Test coverage for both allowed and non-allowed domains Reverted minor changeset to patch bump Fixed prettier issues Refactor domain list from global to parameter Changes domain validation to accept allowedDomains as a parameter instead of mutating a global array. This improves code clarity and testability by making data flow explicit. The change reduces side effets by: - Removing relince on shared mutable state - Making dpendencies clear through function signatures - Enabling better unit testing of domain validation logic Added file formatting Add normalisaztion & tests. - Added normalization to domains - Added extra tests to test against and for normalisation of strings. Update domains in test refactor: remove redundant regex validation in normaliseAllowedDomains - Remove regex pre-validation since new URL() already enforces strict URL standards - Simplify code by relying solely on new URL() for validation and normalisation
I'll update the PR template based on the revised commit message that focuses on developer experience and performance:
What are you adding in this PR?
Fixes #410
This PR adds domain validation to the RemoteAsset theme checker to ensure remote assets (scripts, stylesheets) are loaded from Shopify CDNs and approved domains. When a remote asset is referenced from an unlisted domain, the checker will provide guidance to help developers optimize performance and follow best practices.
Key changes:
allowedDomains
configuration in RemoteAsset check schemaWhat's next? Any followup issues?
Potential follow-ups:
What did you learn?
The variety of ways developers include remote assets highlighted the need for clear guidance on performance best practices. The test cases helped identify common patterns in how external resources are referenced in themes.
Before you deploy
changeset
allChecks
array insrc/checks/index.ts
yarn build
and committed the updated configuration filestheme-app-extension.yml
config (N/A - performance check applies to all themes)changeset