Skip to content
Open
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: 7 additions & 3 deletions Storage/src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -1743,9 +1743,13 @@ public function signedUrl($expires, array $options = [])
* Regardless of the method used, it is always your responsibility to validate
* configurations received from external sources.
* @see https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
* hostname is provided via `$options.bucketBoundHostname`. If a
* custom bucketBoundHostname is provided, **defaults to** `http`.
* In all other cases, **defaults to** `https`.
* @type string $scheme Either `http` or `https`. Only used if a custom
* hostname is provided via `$options.bucketBoundHostname`. When
* omitted, if a custom bucketBoundHostname includes an
* `http://` or `https://` prefix, that scheme is preserved;
* otherwise, **defaults to** `https`. CNAME-aliased bucket
* hostnames serve HTTP only; set this option to `http` when
* using one.
* @type string|array $scopes One or more authentication scopes to be
* used with a key file. This option is ignored unless
* `$options.keyFile` or `$options.keyFilePath` is set.
Expand Down
17 changes: 13 additions & 4 deletions Storage/src/SigningHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private function chooseScheme($scheme, $bucketBoundHostname, $virtualHostedStyle
return 'https';
}

// not virtual hosted style, and custom hostname -- use default (http) or user choice.
// not virtual hosted style, and custom hostname -- use default (https) or user choice.
return $scheme;
}

Expand Down Expand Up @@ -618,6 +618,11 @@ private function normalizeResource($resource, $urlencode = true)
*/
private function normalizeOptions(array $options)
{
$explicitScheme = isset($options['scheme']);
if (!$explicitScheme) {
unset($options['scheme']);
}

$options += [
'allowPost' => false,
'cname' => null, //@deprecated
Expand All @@ -635,8 +640,8 @@ private function normalizeOptions(array $options)
'responseType' => null,
'saveAsName' => null,

// note that in almost every case this default will be overridden.
'scheme' => 'http',
// Defaults to https unless scheme is set or the custom hostname has an http:// or https:// prefix.
'scheme' => 'https',
'timestamp' => null,
'virtualHostedStyle' => false,
];
Expand All @@ -658,9 +663,13 @@ private function normalizeOptions(array $options)
$options['bucketBoundHostname'] = $options['cname'];
}

// strip protocol from hostname.
// strip protocol from hostname, preserving scheme if not explicitly set.
$hostnameParts = explode('//', $options['bucketBoundHostname']);
if (count($hostnameParts) > 1) {
$prefix = strtolower($hostnameParts[0]);
if (!$explicitScheme && in_array($prefix, ['http:', 'https:'], true)) {
$options['scheme'] = substr($prefix, 0, -1);
}
$options['bucketBoundHostname'] = $hostnameParts[1];
}

Expand Down
25 changes: 15 additions & 10 deletions Storage/src/StorageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,12 +881,11 @@ public function downloadAsStreamAsync(array $options = [])
*
* ```
* // Using Bucket-Bound hostnames
* // By default, a custom bucket-bound hostname will use `http` as the schema rather than `https`.
* // In order to get an https URI, we need to specify the proper scheme.
* // By default, a custom bucket-bound hostname will use `https` as the scheme,
* // or preserve an `http`/`https` prefix from `$options.bucketBoundHostname`.
* $url = $object->signedUrl(new \DateTime('tomorrow'), [
* 'version' => 'v4',
* 'bucketBoundHostname' => 'cdn.example.com',
* 'scheme' => 'https'
* 'bucketBoundHostname' => 'cdn.example.com'
* ]);
* ```
*
Expand Down Expand Up @@ -987,9 +986,12 @@ public function downloadAsStreamAsync(array $options = [])
* file as when the signed url is accessed. This is ignored if
* `$options.responseDisposition` is set.
* @type string $scheme Either `http` or `https`. Only used if a custom
* hostname is provided via `$options.bucketBoundHostname`. If a
* custom bucketBoundHostname is provided, **defaults to** `http`.
* In all other cases, **defaults to** `https`.
* hostname is provided via `$options.bucketBoundHostname`. When
* omitted, if a custom bucketBoundHostname includes an
* `http://` or `https://` prefix, that scheme is preserved;
* otherwise, **defaults to** `https`. CNAME-aliased bucket
* hostnames serve HTTP only; set this option to `http` when
* using one.
* @type string|array $scopes One or more authentication scopes to be
* used with a key file. This option is ignored unless
* `$options.keyFile` or `$options.keyFilePath` is set.
Expand Down Expand Up @@ -1142,9 +1144,12 @@ public function signedUrl($expires, array $options = [])
* file as when the signed url is accessed. This is ignored if
* `$options.responseDisposition` is set.
* @type string $scheme Either `http` or `https`. Only used if a custom
* hostname is provided via `$options.bucketBoundHostname`. In all
* other cases, `https` is used. When a custom bucketBoundHostname
* is provided, **defaults to** `http`.
* hostname is provided via `$options.bucketBoundHostname`. When
* omitted, if a custom bucketBoundHostname includes an
* `http://` or `https://` prefix, that scheme is preserved;
* otherwise, **defaults to** `https`. CNAME-aliased bucket
* hostnames serve HTTP only; set this option to `http` when
* using one.
* @type string|array $scopes One or more authentication scopes to be
* used with a key file. This option is ignored unless
* `$options.keyFile` or `$options.keyFilePath` is set.
Expand Down
43 changes: 37 additions & 6 deletions Storage/tests/Unit/SigningHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function testV4SignCanonicalRequestAndBucketBoundHostname()
/**
* @dataProvider hostnames
*/
public function testV4BucketBoundHostnameFix($bucketBoundHostname, $expected = null)
public function testV4BucketBoundHostnameFix($bucketBoundHostname, $expected = null, $expectedScheme = 'https')
{
$credentials = $this->createCredentialsMock();
$expires = time() + 2;
Expand All @@ -316,6 +316,7 @@ public function testV4BucketBoundHostnameFix($bucketBoundHostname, $expected = n
);

$parts = parse_url($url);
$this->assertEquals($expectedScheme, $parts['scheme']);
$this->assertEquals($expected ?: $bucketBoundHostname, $parts['host']);
}

Expand Down Expand Up @@ -378,11 +379,11 @@ public function testCnameStillWorks($bucketBoundHostname, $expected = null)
public function hostnames()
{
return [
['example.com'],
['foo.example.com'],
['https://example.com', 'example.com'],
['http://example.com', 'example.com'],
['//example.com', 'example.com']
['example.com', null, 'https'],
['foo.example.com', null, 'https'],
['https://example.com', 'example.com', 'https'],
['http://example.com', 'example.com', 'http'],
['//example.com', 'example.com', 'https']
];
}

Expand Down Expand Up @@ -535,6 +536,36 @@ public function options()
['method' => 'Foo'], null, \InvalidArgumentException::class
], [
['method' => 'POST'], null, \InvalidArgumentException::class
], [
['bucketBoundHostname' => 'example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
], [
['bucketBoundHostname' => 'https://example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
], [
['bucketBoundHostname' => 'http://example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'http']
], [
['bucketBoundHostname' => '//example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
], [
['cname' => 'https://example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
], [
['cname' => 'http://example.com'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'http']
], [
['bucketBoundHostname' => 'example.com', 'scheme' => null],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
], [
['bucketBoundHostname' => 'http://example.com', 'scheme' => null],
['bucketBoundHostname' => 'example.com', 'scheme' => 'http']
], [
['bucketBoundHostname' => 'https://example.com', 'scheme' => 'http'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'http']
], [
['bucketBoundHostname' => 'http://example.com', 'scheme' => 'https'],
['bucketBoundHostname' => 'example.com', 'scheme' => 'https']
]
];
}
Expand Down
Loading