Skip to content

Commit

Permalink
fix: escape "%" in yaml parameters (#127)
Browse files Browse the repository at this point in the history
* fix: escape "%" in yaml parameters

* test: add test for percent

Co-authored-by: Kieran <[email protected]>
  • Loading branch information
Gaitholabi and bytestream authored Feb 17, 2021
1 parent 758fce2 commit 6971041
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/SupportPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

use function array_merge;
use function str_replace;
use function sys_get_temp_dir;

/**
Expand Down Expand Up @@ -54,8 +55,8 @@ public function __construct(
$containerBuilder = new ContainerBuilder;
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('Resources/services.yml');
$containerBuilder->setParameter('apiUrl', $apiContext->getApiUrl());
$containerBuilder->setParameter('apiToken', $apiContext->getApiToken());
$containerBuilder->setParameter('apiUrl', $this->escapeSpecialCharacters($apiContext->getApiUrl()));
$containerBuilder->setParameter('apiToken', $this->escapeSpecialCharacters($apiContext->getApiToken()));
$containerBuilder->setParameter('defaultParameters', $requestDefaults->getDefaultParameters());
$containerBuilder->setParameter('defaultBodyContent', $requestDefaults->getDefaultBodyContent());

Expand Down Expand Up @@ -176,4 +177,13 @@ private function buildCacheStrategy(ApiContext $apiContext, string $cacheDir): C
return (new CacheStrategyConfigurator(new ApiCacheMap))
->buildCacheStrategy($cacheDir, $apiContext->getApiPath());
}

/**
* This function escapes '%' because Symfony dependency injection uses it to detect the value of the set parameter
* using the following regex: `%%|%([^%\s]+)%` in the yaml file
*/
private function escapeSpecialCharacters(string $value): string
{
return str_replace('%', '%%', $value);
}
}
29 changes: 29 additions & 0 deletions test/Functional/SupportPalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace SupportPal\ApiClient\Tests\Functional;

use Exception;
use GuzzleHttp\Psr7\Response;
use SupportPal\ApiClient\Config\ApiContext;
use SupportPal\ApiClient\Exception\HttpResponseException;
use SupportPal\ApiClient\Factory\RequestFactory;
use SupportPal\ApiClient\SupportPal;
use SupportPal\ApiClient\Tests\ContainerAwareBaseTestCase;
use SupportPal\ApiClient\Tests\DataFixtures\SelfService\CommentData;

use function base64_encode;
use function current;

/**
* Class SupportPalTest
* @package SupportPal\ApiClient\Tests\Functional
Expand Down Expand Up @@ -43,4 +49,27 @@ public function testSendRequestUnSuccessful(): void
$request = $this->getSupportPal()->getRequestFactory()->create('GET', 'test_endpoint');
$this->getSupportPal()->sendRequest($request);
}

/**
* @dataProvider provideApiTokens
* @param string $apiToken
* @throws Exception
*/
public function testEscapePercentApiToken(string $apiToken): void
{
$request = (new SupportPal(new ApiContext('localhost', $apiToken)))->getRequestFactory()->create('GET', 'test');
self::assertSame('Basic ' . base64_encode($apiToken . ':X'), current($request->getHeader('Authorization')));
}

/**
* @return iterable<array<int, string>>
*/
public function provideApiTokens(): iterable
{
yield ['api_token_without_percent'];
yield ['api_token%'];
yield ['%api_token%'];
yield ['%api%_tok%en%'];
yield ['%%%%'];
}
}

0 comments on commit 6971041

Please sign in to comment.