Unleash / unleash-client-php

Unleash client SDK for PHP
MIT License
55 stars 14 forks source link

403 in PHP SDK when passing prefix or tags in the URL #205

Closed valtonia closed 5 months ago

valtonia commented 5 months ago

Describe the bug

Following the documentation when I try to pass in either a name prefix or tags in the URL using UnleashBuilder::create() I always get a 403 response.

Steps to reproduce the bug

With this code

$unleash = UnleashBuilder::create()
    ->withCacheHandler(new Cache\Adapter\PHPArray\ArrayCachePool())
    ->withAppName(APP_NAME)
    ->withAppUrl(new Url(UNLEASH_API_URL, "AH-"))
    ->withInstanceId(INSTANCE_ID)
    ->withHeader("Authorization", UNLEASH_API_KEY)
    ->build();

if ($unleash->isEnabled('AH-Experiment')) {
    echo "AH-Experiment: enabled\n";
}

Which gives a URL like this https://unleash.company.com/api?namePrefix=AH-

The response is always

Unleash\Client\Exception\HttpResponseException: Got invalid response code when getting features and no default bootstrap provided: 403 in ...

Expected behavior

The flag should be retrieved without error.

Logs, error output, etc.

Unleash\Client\Exception\HttpResponseException: Got invalid response code when getting features and no default bootstrap provided: 403 in ...

Screenshots

No response

Additional context

The same thing happens if I try to include any tags in the URL.

$url = new Url(UNLEASH_API_URL, null, ["myTag" => "myValue"]);

Unleash version

PHP SDK 2.4

Subscription type

None

Hosting type

Hosted by Unleash

SDK information (language and version)

PHP SDK 2.4

ivarconr commented 5 months ago

thanks for raising this.

The biggest thing that stands out to me is that you say the URL it produces is https://unleash.company.com/api?namePrefix=AH- while the expected URL for fetching feature flags is https://unleash.company.com/api/client/features.

In the PHP SDK doc it suggest to use the following format to use the namePrefix:

<?php

use Unleash\Client\UnleashBuilder;
use Unleash\Client\Helper\Url;

$builder = UnleashBuilder::create()
    ->withAppName('Some app name')
    ->withAppUrl(new Url('https://some-app-url.com', namePrefix: 'somePrefix.'))
    ->withInstanceId('Some instance id');

Have you tried this configuration?

valtonia commented 5 months ago

Yes, I tried that.

Since raising the ticket I've been looking into the code and it seems the that the Url class is just appending the prefix (and tags) to the URL but other parts of the code are not expecting that and are then appending /client/features onto the end of it.

See line 120 of src/Repository/DefaultUnleashRepository.php in the SDK. Here we see this line

->createRequest('GET', $this->configuration->getUrl() . 'client/features')

But the result from ->getUrl() will already have the prefix and tags applied to it.

RikudouSage commented 5 months ago

@valtonia Can you test that the fix in #206 applies to your problem?

valtonia commented 5 months ago

@RikudouSage Can confirm that your fix works.