mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Updated logic to generate random short codes, increasing entropy
This commit is contained in:
@@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\Entity;
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use PUGX\Shortid\Factory as ShortIdFactory;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
|
||||
@@ -20,6 +21,8 @@ use function Functional\invoke;
|
||||
|
||||
class ShortUrl extends AbstractEntity
|
||||
{
|
||||
private const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
/** @var string */
|
||||
private $longUrl;
|
||||
/** @var string */
|
||||
@@ -53,10 +56,15 @@ class ShortUrl extends AbstractEntity
|
||||
$this->validSince = $meta->getValidSince();
|
||||
$this->validUntil = $meta->getValidUntil();
|
||||
$this->maxVisits = $meta->getMaxVisits();
|
||||
$this->shortCode = $meta->getCustomSlug() ?? ''; // TODO logic to calculate short code should be passed somehow
|
||||
$this->shortCode = $meta->getCustomSlug() ?? $this->generateShortCode();
|
||||
$this->domain = ($domainResolver ?? new SimpleDomainResolver())->resolveDomain($meta->getDomain());
|
||||
}
|
||||
|
||||
private function generateShortCode(): string
|
||||
{
|
||||
return (new ShortIdFactory())->generate(6, self::BASE62)->serialize();
|
||||
}
|
||||
|
||||
public function getLongUrl(): string
|
||||
{
|
||||
return $this->longUrl;
|
||||
@@ -67,13 +75,6 @@ class ShortUrl extends AbstractEntity
|
||||
return $this->shortCode;
|
||||
}
|
||||
|
||||
// TODO Short code is currently calculated based on the ID, so a setter is needed
|
||||
public function setShortCode(string $shortCode): self
|
||||
{
|
||||
$this->shortCode = $shortCode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateCreated(): Chronos
|
||||
{
|
||||
return $this->dateCreated;
|
||||
|
||||
@@ -8,26 +8,12 @@ use Zend\Stdlib\AbstractOptions;
|
||||
|
||||
class UrlShortenerOptions extends AbstractOptions
|
||||
{
|
||||
public const DEFAULT_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
// phpcs:disable
|
||||
protected $__strictMode__ = false;
|
||||
// phpcs:enable
|
||||
|
||||
private $shortcodeChars = self::DEFAULT_CHARS;
|
||||
private $validateUrl = true;
|
||||
|
||||
public function getChars(): string
|
||||
{
|
||||
return $this->shortcodeChars;
|
||||
}
|
||||
|
||||
protected function setShortcodeChars(string $shortcodeChars): self
|
||||
{
|
||||
$this->shortcodeChars = empty($shortcodeChars) ? self::DEFAULT_CHARS : $shortcodeChars;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isUrlValidationEnabled(): bool
|
||||
{
|
||||
return $this->validateUrl;
|
||||
|
||||
@@ -24,17 +24,11 @@ use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
use Throwable;
|
||||
|
||||
use function array_reduce;
|
||||
use function floor;
|
||||
use function fmod;
|
||||
use function preg_match;
|
||||
use function strlen;
|
||||
|
||||
class UrlShortener implements UrlShortenerInterface
|
||||
{
|
||||
use TagManagerTrait;
|
||||
|
||||
private const ID_INCREMENT = 200000;
|
||||
|
||||
/** @var ClientInterface */
|
||||
private $httpClient;
|
||||
/** @var EntityManagerInterface */
|
||||
@@ -77,16 +71,8 @@ class UrlShortener implements UrlShortenerInterface
|
||||
|
||||
// First, create the short URL with an empty short code
|
||||
$shortUrl = new ShortUrl($url, $meta, new PersistenceDomainResolver($this->em));
|
||||
$this->em->persist($shortUrl);
|
||||
$this->em->flush();
|
||||
|
||||
// Generate the short code and persist it if no custom slug was provided
|
||||
if (! $meta->hasCustomSlug()) {
|
||||
// TODO Somehow provide the logic to calculate the shortCode to avoid the need of a setter
|
||||
$shortCode = $this->convertAutoincrementIdToShortCode((float) $shortUrl->getId());
|
||||
$shortUrl->setShortCode($shortCode);
|
||||
}
|
||||
$shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
|
||||
$this->em->persist($shortUrl);
|
||||
$this->em->flush();
|
||||
|
||||
$this->em->commit();
|
||||
@@ -155,36 +141,12 @@ class UrlShortener implements UrlShortenerInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function convertAutoincrementIdToShortCode(float $id): string
|
||||
{
|
||||
$id += self::ID_INCREMENT; // Increment the Id so that the generated shortcode is not too short
|
||||
$chars = $this->options->getChars();
|
||||
|
||||
$length = strlen($chars);
|
||||
$code = '';
|
||||
|
||||
while ($id > 0) {
|
||||
// Determine the value of the next higher character in the short code and prepend it
|
||||
$code = $chars[(int) fmod($id, $length)] . $code;
|
||||
$id = floor($id / $length);
|
||||
}
|
||||
|
||||
return $chars[(int) $id] . $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidShortCodeException
|
||||
* @throws EntityDoesNotExistException
|
||||
*/
|
||||
public function shortCodeToUrl(string $shortCode, ?string $domain = null): ShortUrl
|
||||
{
|
||||
$chars = $this->options->getChars();
|
||||
|
||||
// Validate short code format
|
||||
if (! preg_match('|[' . $chars . ']+|', $shortCode)) {
|
||||
throw InvalidShortCodeException::fromCharset($shortCode, $chars);
|
||||
}
|
||||
|
||||
/** @var ShortUrlRepository $shortUrlRepo */
|
||||
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
|
||||
$shortUrl = $shortUrlRepo->findOneByShortCode($shortCode, $domain);
|
||||
|
||||
@@ -37,37 +37,41 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
/** @test */
|
||||
public function findOneByShortCodeReturnsProperData(): void
|
||||
{
|
||||
$regularOne = new ShortUrl('foo');
|
||||
$regularOne->setShortCode('foo');
|
||||
$regularOne = new ShortUrl('foo', ShortUrlMeta::createFromParams(null, null, 'foo'));
|
||||
$this->getEntityManager()->persist($regularOne);
|
||||
|
||||
$notYetValid = new ShortUrl('bar', ShortUrlMeta::createFromParams(Chronos::now()->addMonth()));
|
||||
$notYetValid->setShortCode('bar_very_long_text');
|
||||
$notYetValid = new ShortUrl(
|
||||
'bar',
|
||||
ShortUrlMeta::createFromParams(Chronos::now()->addMonth(), null, 'bar_very_long_text')
|
||||
);
|
||||
$this->getEntityManager()->persist($notYetValid);
|
||||
|
||||
$expired = new ShortUrl('expired', ShortUrlMeta::createFromParams(null, Chronos::now()->subMonth()));
|
||||
$expired->setShortCode('expired');
|
||||
$expired = new ShortUrl('expired', ShortUrlMeta::createFromParams(null, Chronos::now()->subMonth(), 'expired'));
|
||||
$this->getEntityManager()->persist($expired);
|
||||
|
||||
$allVisitsComplete = new ShortUrl('baz', ShortUrlMeta::createFromRawData(['maxVisits' => 3]));
|
||||
$allVisitsComplete = new ShortUrl('baz', ShortUrlMeta::createFromRawData([
|
||||
'maxVisits' => 3,
|
||||
'customSlug' => 'baz',
|
||||
]));
|
||||
$visits = [];
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$visit = new Visit($allVisitsComplete, Visitor::emptyInstance());
|
||||
$this->getEntityManager()->persist($visit);
|
||||
$visits[] = $visit;
|
||||
}
|
||||
$allVisitsComplete->setShortCode('baz')
|
||||
->setVisits(new ArrayCollection($visits));
|
||||
$allVisitsComplete->setVisits(new ArrayCollection($visits));
|
||||
$this->getEntityManager()->persist($allVisitsComplete);
|
||||
|
||||
$withDomain = new ShortUrl('foo', ShortUrlMeta::createFromRawData(['domain' => 'example.com']));
|
||||
$withDomain->setShortCode('domain-short-code');
|
||||
$withDomain = new ShortUrl('foo', ShortUrlMeta::createFromRawData([
|
||||
'domain' => 'example.com',
|
||||
'customSlug' => 'domain-short-code',
|
||||
]));
|
||||
$this->getEntityManager()->persist($withDomain);
|
||||
|
||||
$withDomainDuplicatingRegular = new ShortUrl('foo_with_domain', ShortUrlMeta::createFromRawData([
|
||||
'domain' => 'doma.in',
|
||||
'customSlug' => 'foo',
|
||||
]));
|
||||
$withDomainDuplicatingRegular->setShortCode('foo');
|
||||
$this->getEntityManager()->persist($withDomainDuplicatingRegular);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
@@ -96,9 +100,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
$count = 5;
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$this->getEntityManager()->persist(
|
||||
(new ShortUrl((string) $i))->setShortCode((string) $i)
|
||||
);
|
||||
$this->getEntityManager()->persist(new ShortUrl((string) $i));
|
||||
}
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
@@ -112,19 +114,16 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->persist($tag);
|
||||
|
||||
$foo = new ShortUrl('foo');
|
||||
$foo->setShortCode('foo')
|
||||
->setTags(new ArrayCollection([$tag]));
|
||||
$foo->setTags(new ArrayCollection([$tag]));
|
||||
$this->getEntityManager()->persist($foo);
|
||||
|
||||
$bar = new ShortUrl('bar');
|
||||
$visit = new Visit($bar, Visitor::emptyInstance());
|
||||
$this->getEntityManager()->persist($visit);
|
||||
$bar->setShortCode('bar_very_long_text')
|
||||
->setVisits(new ArrayCollection([$visit]));
|
||||
$bar->setVisits(new ArrayCollection([$visit]));
|
||||
$this->getEntityManager()->persist($bar);
|
||||
|
||||
$foo2 = new ShortUrl('foo_2');
|
||||
$foo2->setShortCode('foo_2');
|
||||
$this->getEntityManager()->persist($foo2);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
@@ -155,9 +154,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
$urls = ['a', 'z', 'c', 'b'];
|
||||
foreach ($urls as $url) {
|
||||
$this->getEntityManager()->persist(
|
||||
(new ShortUrl($url))->setShortCode($url)
|
||||
);
|
||||
$this->getEntityManager()->persist(new ShortUrl($url));
|
||||
}
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
@@ -174,13 +171,13 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
/** @test */
|
||||
public function slugIsInUseLooksForShortUrlInProperSetOfTables(): void
|
||||
{
|
||||
$shortUrlWithoutDomain = (new ShortUrl('foo'))->setShortCode('my-cool-slug');
|
||||
$shortUrlWithoutDomain = new ShortUrl('foo', ShortUrlMeta::createFromRawData(['customSlug' => 'my-cool-slug']));
|
||||
$this->getEntityManager()->persist($shortUrlWithoutDomain);
|
||||
|
||||
$shortUrlWithDomain = (new ShortUrl(
|
||||
$shortUrlWithDomain = new ShortUrl(
|
||||
'foo',
|
||||
ShortUrlMeta::createFromRawData(['domain' => 'doma.in'])
|
||||
))->setShortCode('another-slug');
|
||||
ShortUrlMeta::createFromRawData(['domain' => 'doma.in', 'customSlug' => 'another-slug'])
|
||||
);
|
||||
$this->getEntityManager()->persist($shortUrlWithDomain);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
@@ -19,20 +19,21 @@ use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlService;
|
||||
|
||||
use function Functional\map;
|
||||
use function range;
|
||||
use function sprintf;
|
||||
|
||||
class DeleteShortUrlServiceTest extends TestCase
|
||||
{
|
||||
/** @var DeleteShortUrlService */
|
||||
private $service;
|
||||
/** @var ObjectProphecy */
|
||||
private $em;
|
||||
/** @var string */
|
||||
private $shortCode;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$shortUrl = (new ShortUrl(''))->setShortCode('abc123')
|
||||
->setVisits(new ArrayCollection(map(range(0, 10), function () {
|
||||
return new Visit(new ShortUrl(''), Visitor::emptyInstance());
|
||||
})));
|
||||
$shortUrl = (new ShortUrl(''))->setVisits(new ArrayCollection(map(range(0, 10), function () {
|
||||
return new Visit(new ShortUrl(''), Visitor::emptyInstance());
|
||||
})));
|
||||
$this->shortCode = $shortUrl->getShortCode();
|
||||
|
||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||
|
||||
@@ -42,55 +43,56 @@ class DeleteShortUrlServiceTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function deleteByShortCodeThrowsExceptionWhenThresholdIsReached()
|
||||
public function deleteByShortCodeThrowsExceptionWhenThresholdIsReached(): void
|
||||
{
|
||||
$service = $this->createService();
|
||||
|
||||
$this->expectException(DeleteShortUrlException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Impossible to delete short URL with short code "abc123" since it has more than "5" visits.'
|
||||
);
|
||||
$this->expectExceptionMessage(sprintf(
|
||||
'Impossible to delete short URL with short code "%s" since it has more than "5" visits.',
|
||||
$this->shortCode
|
||||
));
|
||||
|
||||
$service->deleteByShortCode('abc123');
|
||||
$service->deleteByShortCode($this->shortCode);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButExplicitlyIgnored()
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButExplicitlyIgnored(): void
|
||||
{
|
||||
$service = $this->createService();
|
||||
|
||||
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null);
|
||||
$flush = $this->em->flush()->willReturn(null);
|
||||
|
||||
$service->deleteByShortCode('abc123', true);
|
||||
$service->deleteByShortCode($this->shortCode, true);
|
||||
|
||||
$remove->shouldHaveBeenCalledOnce();
|
||||
$flush->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButCheckIsDisabled()
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButCheckIsDisabled(): void
|
||||
{
|
||||
$service = $this->createService(false);
|
||||
|
||||
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null);
|
||||
$flush = $this->em->flush()->willReturn(null);
|
||||
|
||||
$service->deleteByShortCode('abc123');
|
||||
$service->deleteByShortCode($this->shortCode);
|
||||
|
||||
$remove->shouldHaveBeenCalledOnce();
|
||||
$flush->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsNotReached()
|
||||
public function deleteByShortCodeDeletesUrlWhenThresholdIsNotReached(): void
|
||||
{
|
||||
$service = $this->createService(true, 100);
|
||||
|
||||
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null);
|
||||
$flush = $this->em->flush()->willReturn(null);
|
||||
|
||||
$service->deleteByShortCode('abc123');
|
||||
$service->deleteByShortCode($this->shortCode);
|
||||
|
||||
$remove->shouldHaveBeenCalledOnce();
|
||||
$flush->shouldHaveBeenCalledOnce();
|
||||
|
||||
@@ -17,7 +17,6 @@ use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||
@@ -74,13 +73,13 @@ class UrlShortenerTest extends TestCase
|
||||
/** @test */
|
||||
public function urlIsProperlyShortened(): void
|
||||
{
|
||||
// 10 -> 0Q1Y
|
||||
$shortUrl = $this->urlShortener->urlToShortCode(
|
||||
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
||||
[],
|
||||
ShortUrlMeta::createEmpty()
|
||||
);
|
||||
$this->assertEquals('0Q1Y', $shortUrl->getShortCode());
|
||||
|
||||
$this->assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -243,9 +242,8 @@ class UrlShortenerTest extends TestCase
|
||||
/** @test */
|
||||
public function shortCodeIsProperlyParsed(): void
|
||||
{
|
||||
$shortCode = '12C1c';
|
||||
$shortUrl = new ShortUrl('expected_url');
|
||||
$shortUrl->setShortCode($shortCode);
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
|
||||
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
||||
$repo->findOneByShortCode($shortCode, null)->willReturn($shortUrl);
|
||||
@@ -254,11 +252,4 @@ class UrlShortenerTest extends TestCase
|
||||
$url = $this->urlShortener->shortCodeToUrl($shortCode);
|
||||
$this->assertSame($shortUrl, $url);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invalidCharSetThrowsException(): void
|
||||
{
|
||||
$this->expectException(InvalidShortCodeException::class);
|
||||
$this->urlShortener->shortCodeToUrl('&/(');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user