Skip to content

Commit

Permalink
Fix URI path generation in the Realtime Database Component
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Oct 5, 2021
1 parent 417e9b3 commit 3b7f972
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
### Changed
* Bumped `kreait/firebase-tokens` to `^1.16.1` to ensure a minor security fix in `lcobucci/jwt`
(More info: [GHSA-7322-jrq4-x5hf](https://github.com/lcobucci/jwt/security/advisories/GHSA-7322-jrq4-x5hf))
### Fixed
* Fixed a bug that occurs when using Realtime Database Paths without a leading slash with newer
releases of `guzzle/psr7`

## [5.23.0] - 2021-08-26
### Added
Expand Down
6 changes: 4 additions & 2 deletions src/Firebase/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function getReference(?string $path = null): Reference
$path = '/';
}

$path = '/'.\ltrim($path, '/');

try {
return new Reference($this->uri->withPath($path), $this->client);
} catch (\InvalidArgumentException $e) {
Expand All @@ -59,14 +61,14 @@ public function getReferenceFromUrl($uri): Reference

public function getRuleSet(): RuleSet
{
$rules = $this->client->get($this->uri->withPath('.settings/rules'));
$rules = $this->client->get($this->uri->withPath('/.settings/rules'));

return RuleSet::fromArray($rules);
}

public function updateRules(RuleSet $ruleSet): void
{
$this->client->updateRules($this->uri->withPath('.settings/rules'), $ruleSet);
$this->client->updateRules($this->uri->withPath('/.settings/rules'), $ruleSet);
}

public function runTransaction(callable $callable)
Expand Down
4 changes: 2 additions & 2 deletions src/Firebase/Database/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getParent(): self
throw new OutOfRangeException('Cannot get parent of root reference');
}

return new self($this->uri->withPath($parentPath), $this->apiClient, $this->validator);
return new self($this->uri->withPath('/'.\ltrim($parentPath, '/')), $this->apiClient, $this->validator);
}

/**
Expand All @@ -106,7 +106,7 @@ public function getRoot(): self
*/
public function getChild(string $path): self
{
$childPath = \sprintf('%s/%s', \trim($this->uri->getPath(), '/'), \trim($path, '/'));
$childPath = \sprintf('/%s/%s', \trim($this->uri->getPath(), '/'), \trim($path, '/'));

try {
return new self($this->uri->withPath($childPath), $this->apiClient, $this->validator);
Expand Down
2 changes: 1 addition & 1 deletion src/Firebase/Http/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function ensureJsonSuffix(): callable
return static function (callable $handler) {
return static function (RequestInterface $request, ?array $options = null) use ($handler) {
$uri = $request->getUri();
$path = $uri->getPath();
$path = '/'.\ltrim($uri->getPath(), '/');

if (!\str_ends_with($path, '.json')) {
$uri = $uri->withPath($path.'.json');
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Database/Reference/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ protected function setUp(): void

public function testValidateDepth(): void
{
$uri = $this->uri->withPath(\str_pad('', (Validator::MAX_DEPTH + 1) * 2, 'x/'));
$uri = $this->uri->withPath('/'.\str_pad('', (Validator::MAX_DEPTH + 1) * 2, 'x/'));

$this->expectException(InvalidArgumentException::class);
$this->validator->validateUri($uri);
}

public function testValidateKeySize(): void
{
$uri = $this->uri->withPath(\str_pad('', Validator::MAX_KEY_SIZE + 1, 'x'));
$uri = $this->uri->withPath('/'.\str_pad('', Validator::MAX_KEY_SIZE + 1, 'x'));

$this->expectException(InvalidArgumentException::class);
$this->validator->validateUri($uri);
Expand All @@ -48,7 +48,7 @@ public function testValidateKeySize(): void
*/
public function testValidateChars(string $value): void
{
$uri = $this->uri->withPath($value);
$uri = $this->uri->withPath('/'.\ltrim($value, '/'));

$this->expectException(InvalidArgumentException::class);
$this->validator->validateUri($uri);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void

public function testGetReference(): void
{
$this->assertSame('any', \trim($this->database->getReference('any')->getUri()->getPath(), '/'));
$this->assertSame('any', $this->database->getReference('any')->getPath());
}

public function testGetRootReference(): void
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testGetRuleSet(): void
{
$this->apiClient
->method('get')
->with($this->uri->withPath('.settings/rules'))
->with($this->uri->withPath('/.settings/rules'))
->willReturn($expected = RuleSet::default()->getRules())
;

Expand Down

0 comments on commit 3b7f972

Please sign in to comment.