Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Base URL Pinning. #205

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions src/DataStore/DefaultDataStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,22 @@ protected function qualify($href)

private function executeRequest($httpMethod, $href, $body = '', array $query = array())
{

if ($href == null) {
throw new \InvalidArgumentException("Cannot execute request against empty URL");
}

// Base URL Pinning - issue #198
$urlParts = parse_url($href);
$baseUrlParts = parse_url($this->baseUrl);

if(array_key_exists('host', $urlParts) && array_key_exists('host', $baseUrlParts)) {
if ($urlParts['host'] !== $baseUrlParts['host']) {
throw new \InvalidArgumentException("The HREF you are trying to access is not valid. The HREF must be
the base url set when instantiating the Stormpath Client.");
}
}

$headers = [];
$headers['Accept'] = 'application/json';

Expand Down
72 changes: 72 additions & 0 deletions tests/DataStore/DataStoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright 2017 Stormpath, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace Stormpath\Tests\DataStore;

class DataStoreTest extends \Stormpath\Tests\TestCase {
private static $application;
private static $inited;

private static $account;

protected static function init()
{
self::$application = \Stormpath\Resource\Application::instantiate(array('name' => makeUniqueName('ApplicationTest'), 'description' => 'Description of Main App', 'status' => 'enabled'));
self::createResource(\Stormpath\Resource\Application::PATH, self::$application, array('createDirectory' => true));
self::$inited = true;
}

public function setUp()
{
if (!self::$inited)
{
self::init();
}
}

public static function tearDownAfterClass()
{
if (self::$application && self::$application->href)
{
self::$application->delete();
}

parent::tearDownAfterClass();
}


/**
* @test
* @expectedException \InvalidArgumentException
*/
public function can_not_make_request_to_a_url_other_than_clients_base_url()
{
$application = \Stormpath\Resource\Application::get('http://test.tld/applications/123');
}

/** @test */
public function can_make_request_to_url_that_is_base_url()
{
$application = \Stormpath\Resource\Application::get(self::$application->href);
$this->assertInstanceOf(\Stormpath\Resource\Application::class, $application);
}




}