Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Fixing tests / CI
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Jul 28, 2023
1 parent f7697f4 commit b35d930
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-pans-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': patch
---

Fixing host validation to work with unified admin shops
2 changes: 1 addition & 1 deletion lib/auth/get-embedded-app-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getEmbeddedAppUrl(config: ConfigInterface) {

export function buildEmbeddedAppUrl(config: ConfigInterface) {
return (host: string): string => {
sanitizeHost(config)(host, true);
sanitizeHost()(host, true);
const decodedHost = decodeHost(host);

return `https://${decodedHost}/apps/${config.apiKey}`;
Expand Down
1 change: 1 addition & 0 deletions lib/utils/__tests__/shop-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const VALID_HOSTS = [
'my-other-host.myshopify.com/admin',
'my-other-other-host.myshopify.io/admin',
'admin.shopify.com/store/my-shop',
'admin.spin.dev/store/my-shop',
].map((testhost) => {
return {testhost, base64host: Buffer.from(testhost).toString('base64')};
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {versionCompatible, versionPriorTo} from './version-compatible';
export function shopifyUtils(config: ConfigInterface) {
return {
sanitizeShop: sanitizeShop(config),
sanitizeHost: sanitizeHost(config),
sanitizeHost: sanitizeHost(),
validateHmac: validateHmac(config),
versionCompatible: versionCompatible(config),
versionPriorTo: versionPriorTo(config),
Expand Down
16 changes: 12 additions & 4 deletions lib/utils/shop-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ export function sanitizeShop(config: ConfigInterface) {
};
}

export function sanitizeHost(config: ConfigInterface) {
export function sanitizeHost() {
return (host: string, throwOnInvalid = false): string | null => {
const base64regex = /^[0-9a-zA-Z+/]+={0,2}$/;

let sanitizedHost = base64regex.test(host) ? host : null;
if (sanitizedHost) {
const url = new URL(`https://${decodeHost(sanitizedHost)}`);
// allow-list of origins
if (!/\.((my)?shopify\.com|myshopify\.io|spin\.dev)$/.test(url.hostname)) {
const {hostname} = new URL(`https://${decodeHost(sanitizedHost)}`);

const originsRegex = [
'myshopify\\.com',
'shopify\\.com',
'myshopify\\.io',
'spin\\.dev',
];

const hostRegex = new RegExp(`\\.(${originsRegex.join('|')})$`);
if (!hostRegex.test(hostname)) {
sanitizedHost = null;
}
}
Expand Down

0 comments on commit b35d930

Please sign in to comment.