Skip to content
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

Improve shop domain sanitization #162

Merged
merged 2 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidShopDomainException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Osiset\ShopifyApp\Exceptions;

/**
* Exception for handling an invalid shop domain.
*/
class InvalidShopDomainException extends BaseException
{
}
20 changes: 16 additions & 4 deletions src/Objects/Values/ShopDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain as ShopDomainValue;
use Osiset\ShopifyApp\Exceptions\InvalidShopDomainException;
use Osiset\ShopifyApp\Objects\Enums\DataSource;
use Osiset\ShopifyApp\Util;

Expand All @@ -22,11 +23,17 @@ final class ShopDomain implements ShopDomainValue
*
* @param string $domain The shop's domain.
*
* @throws InvalidShopDomainException
*
* @return void
*/
public function __construct(string $domain)
{
$this->string = $this->sanitizeShopDomain($domain);

if ($this->string === '') {
throw new InvalidShopDomainException("Invalid shop domain [{$domain}]");
}
}

/**
Expand Down Expand Up @@ -101,17 +108,22 @@ public static function fromRequest(Request $request): ShopDomainValue
*
* @return string
*/
protected function sanitizeShopDomain(string $domain): ?string
protected function sanitizeShopDomain(string $domain): string
{
$configEndDomain = Util::getShopifyConfig('myshopify_domain');
$domain = strtolower(preg_replace('/https?:\/\//i', '', trim($domain)));
$domain = strtolower(preg_replace('/^https?:\/\//i', '', trim($domain)));

if (strpos($domain, $configEndDomain) === false && strpos($domain, '.') === false) {
// No myshopify.com ($configEndDomain) in shop's name
$domain .= ".{$configEndDomain}";
}

// Return the host after cleaned up
return parse_url("https://{$domain}", PHP_URL_HOST);
$hostname = parse_url("https://{$domain}", PHP_URL_HOST);

if (! preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\-]*\.'.preg_quote($configEndDomain, '/').'$/', $hostname)) {
return '';
}

return $hostname;
}
}
47 changes: 47 additions & 0 deletions tests/Objects/Values/ShopDomainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Osiset\ShopifyApp\Test\Objects\Values;

use Osiset\ShopifyApp\Exceptions\InvalidShopDomainException;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Test\TestCase;

class ShopDomainTest extends TestCase
{
public function testAddsMyshopifyDomainIfNotContainsPeriod()
{
$shopDomain = ShopDomain::fromNative('test');

$this->assertEquals('test.myshopify.com', $shopDomain->toNative());
}

public function testDoesNotAddMyshopifyDomainIfContainsPeriod()
{
$shopDomain = ShopDomain::fromNative('test.myshopify.com');

$this->assertEquals('test.myshopify.com', $shopDomain->toNative());
}

public function testStripsTheProtocol()
{
$shopDomainA = ShopDomain::fromNative('https://test.myshopify.com');
$shopDomainB = ShopDomain::fromNative('http://test.myshopify.com');

$this->assertEquals('test.myshopify.com', $shopDomainA->toNative());
$this->assertEquals('test.myshopify.com', $shopDomainB->toNative());
}

public function testDoesNotAcceptNonMyshopifyDomains()
{
$this->expectException(InvalidShopDomainException::class);

ShopDomain::fromNative('test.github.com');
}

public function testDoesNotAcceptNonMyshopifyDomainsWithSinglePeriod()
{
$this->expectException(InvalidShopDomainException::class);

ShopDomain::fromNative('test.invalid-domain');
}
}