Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate token before returning the timestamp #19

Merged
merged 3 commits into from
Jun 21, 2021
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.


## [2.2.2](https://github.com/tuupola/branca/compare/2.2.1...2.x) - unreleased
### Fixed
- Token is now validated before using the `timestamp()` helper ([#19](https://github.com/tuupola/branca-php/pull/19)).


## [2.2.1](https://github.com/tuupola/branca/compare/2.2.0...2.2.1) - 2020-10-29
### Fixed
- Add `SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES` define which is missing from some PHP 7.2 installations ([#12](https://github.com/tuupola/branca-php/pull/12)).
Expand Down
18 changes: 11 additions & 7 deletions src/Branca.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Branca
*/
private $key;

/**
* @var int
*/
private $timestamp = null;

/**
* Nonce used for unit testing only.
* @var string
Expand Down Expand Up @@ -145,6 +150,9 @@ public function decode(string $token, int $ttl = null): string
throw new \RuntimeException("Invalid token");
}

/* Store timestamp value for the helper. */
$this->timestamp = $parts["time"];

/* Check for expired token if TTL is set. */
if (is_integer($ttl)) {
$future = $parts["time"] + $ttl;
Expand All @@ -158,14 +166,10 @@ public function decode(string $token, int $ttl = null): string

public function timestamp(string $token): int
{
$token = (new Base62)->decode($token);
$parts = unpack("Cversion/Ntime", $token);

/* Unpack failed, should not ever happen. */
if (false === $parts) {
throw new \RuntimeException("Cannot extract token header");
if (null === $this->timestamp) {
$this->decode($token);
}

return $parts["time"];
return $this->timestamp;
}
}
20 changes: 19 additions & 1 deletion tests/BrancaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testShouldThrowWithModifiedNonce()
* Token was created with time: hex2bin("0757fb00") ie. 123206400
* 875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT
*
* Before base62 encoding the time was modified to: hex2bin("0057fb00")
* Before base62 encoding the time was modified to: hex2bin("0057fb00") ie. 5765888
* 870g1RCk4lW1YInhaU3TP8u2hGtfol16ettLcTOSoA0JIpjCaQRW7tQeP6dQmTvFIB2s6wL5deMXr
*/
public function testShouldThrowWithModifiedTimestamp()
Expand Down Expand Up @@ -482,6 +482,24 @@ public function testShouldThrowWithInvalidNonce()
$encoded = $branca->encode($payload);
}

/**
* Timestamp helper should throw is token has been tampered.
*
* Token was created with time: hex2bin("0757fb00") ie. 123206400
* 875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT
*
* Before base62 encoding the time was modified to: hex2bin("0057fb00") ie. 5765888
* 870g1RCk4lW1YInhaU3TP8u2hGtfol16ettLcTOSoA0JIpjCaQRW7tQeP6dQmTvFIB2s6wL5deMXr
*/
public function testShouldThrowWithModifiedTimestampWhenReadingTimestamp()
{
$this->expectException(RuntimeException::class);

$token = "870g1RCk4lW1YInhaU3TP8u2hGtfol16ettLcTOSoA0JIpjCaQRW7tQeP6dQmTvFIB2s6wL5deMXr";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$timestamp = $branca->timestamp($token);
}

// public function testShouldThrowWhenTimestampOverflows()
// {
// $this->expectException(RuntimeException::class);
Expand Down