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

Bugfix - Handle invalid characters when decoding #6

Merged
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 src/Base62/BaseEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Tuupola\Base62;

use InvalidArgumentException;
use Tuupola\Base62;

abstract class BaseEncoder
Expand Down Expand Up @@ -48,6 +49,11 @@ public function encode($data, $integer = false)

public function decode($data, $integer = false)
{
// If the data contains characters that aren't in the character set
if (strlen($data) !== strspn($data, $this->options["characters"])) {
throw new InvalidArgumentException("Data contains invalid characters");
}

$data = str_split($data);
$data = array_map(function ($character) {
return strpos($this->options["characters"], $character);
Expand Down
6 changes: 6 additions & 0 deletions src/Base62/GmpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Tuupola\Base62;

use InvalidArgumentException;
use Tuupola\Base62;

class GmpEncoder
Expand Down Expand Up @@ -45,6 +46,11 @@ public function encode($data, $integer = false)

public function decode($data, $integer = false)
{
// If the data contains characters that aren't in the character set
if (strlen($data) !== strspn($data, $this->options["characters"])) {
throw new InvalidArgumentException("Data contains invalid characters");
}

if (Base62::GMP !== $this->options["characters"]) {
$data = strtr($data, $this->options["characters"], Base62::GMP);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/Base62Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Tuupola\Base62;

use InvalidArgumentException;
use Tuupola\Base62;
use Tuupola\Base62Proxy;

Expand Down Expand Up @@ -261,4 +262,58 @@ public function testShouldEncodeAndDecodeBigIntegers()
$this->assertEquals($encoded, $encoded5);
$this->assertEquals($data, $decoded5);
}

public function testShouldThrowExceptionOnDecodeInvalidData()
{
$invalid_data = "invalid~data-%@#!@*#-foo";

InvalidArgumentException::class;

$decoders = [
new PhpEncoder(),
new GmpEncoder(),
new BcmathEncoder(),
new Base62(),
];

foreach ($decoders as $decoder) {
$caught = null;

try {
$decoder->decode($invalid_data, false);
} catch (InvalidArgumentException $e) {
$caught = $e;
}

$this->assertInstanceOf(InvalidArgumentException::class, $caught);
}
}

public function testShouldThrowExceptionOnDecodeInvalidDataWithCustomCharacterSet()
{
$invalid_data = "normallyvaliddata";
$character_set = "01abc";
$options = ["characters" => $character_set];

InvalidArgumentException::class;

$decoders = [
new PhpEncoder($options),
new GmpEncoder($options),
new BcmathEncoder($options),
new Base62($options),
];

foreach ($decoders as $decoder) {
$caught = null;

try {
$decoder->decode($invalid_data, false);
} catch (InvalidArgumentException $e) {
$caught = $e;
}

$this->assertInstanceOf(InvalidArgumentException::class, $caught);
}
}
}