Merge pull request #3944 from shtrom/always-hash-exists-url

Always hash exists url
This commit is contained in:
Jérémy Benoist
2019-05-28 14:18:33 +02:00
committed by GitHub
5 changed files with 84 additions and 51 deletions

View File

@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Wallabag\CoreBundle\Helper\UrlHasher;
use Wallabag\UserBundle\Entity\User;
class GenerateUrlHashesCommand extends ContainerAwareCommand
@ -65,7 +66,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
$i = 1;
foreach ($entries as $entry) {
$entry->setHashedUrl(hash('sha1', $entry->getUrl()));
$entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl()));
$em->persist($entry);
if (0 === ($i % 20)) {
@ -84,7 +85,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
*
* @param string $username
*
* @return \Wallabag\UserBundle\Entity\User
* @return User
*/
private function getUser($username)
{

View File

@ -13,6 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot;
use Symfony\Component\Validator\Constraints as Assert;
use Wallabag\AnnotationBundle\Entity\Annotation;
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
use Wallabag\CoreBundle\Helper\UrlHasher;
use Wallabag\UserBundle\Entity\User;
/**
@ -324,7 +325,7 @@ class Entry
public function setUrl($url)
{
$this->url = $url;
$this->hashedUrl = hash('sha1', $url);
$this->hashedUrl = UrlHasher::hashUrl($url);
return $this;
}

View File

@ -0,0 +1,23 @@
<?php
namespace Wallabag\CoreBundle\Helper;
/**
* Hash URLs for privacy and performance.
*/
class UrlHasher
{
/**
* Hash the given url using the given algorithm.
* Hashed url are faster to be retrieved in the database than the real url.
*
* @param string $url
* @param string $algorithm
*
* @return string
*/
public static function hashUrl(string $url, $algorithm = 'sha1')
{
return hash($algorithm, urldecode($url));
}
}

View File

@ -9,6 +9,7 @@ use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Helper\UrlHasher;
class EntryRepository extends EntityRepository
{
@ -348,17 +349,10 @@ class EntryRepository extends EntityRepository
*/
public function findByUrlAndUserId($url, $userId)
{
$res = $this->createQueryBuilder('e')
->where('e.url = :url')->setParameter('url', urldecode($url))
->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
->getQuery()
->getResult();
if (\count($res)) {
return current($res);
}
return false;
return $this->findByHashedUrlAndUserId(
UrlHasher::hashUrl($url),
$userId
);
}
/**