Skip to content

Use sha1 to reduce the risk of collisions#12

Merged
sagikazarmark merged 7 commits into
php-http:masterfrom
GrahamCampbell:patch-3
Aug 2, 2016
Merged

Use sha1 to reduce the risk of collisions#12
sagikazarmark merged 7 commits into
php-http:masterfrom
GrahamCampbell:patch-3

Conversation

@GrahamCampbell

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
License MIT

What's in this PR?

Switches the key generation algorithm over to use sha1 rather than md5.

Why?

The performance is similar, and the collision risk is lower.

Example Usage

image

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix)

To Do

  • If the PR is not complete but you want to discuss the approach, list what remains to be done here

@sagikazarmark

Copy link
Copy Markdown
Member

Maybe we can make this configurable? Valid values are the results of hash_algos().

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

Maybe we should just allow people to implement a key generator interface and inject that into the constructor and have it do createCacheKey from a message. If you're ok with that, I can make that abstraction now.

@sagikazarmark

sagikazarmark commented Aug 1, 2016

Copy link
Copy Markdown
Member

Hm, okay, but I would be happier if we could make it kind of optional: add a hash option in config defaulting to sha1, allowed values are null and result of hash_algos. (createCacheKey in null case should throw a LogicException if no hash and no CacheKeyGenerator instances are provided). Also, a custom CacheKeyGenerator (name ok?) that could be provided via a setter (with a HashCacheKeyGenerator implementation used as the default).

IMO in most cases such implementation might be an overkill, so I think it would be better to provide a "fallback"/default solution.

WDYT?

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

Why would anyone want to customize this? If they do, a CacheKeyGenerator would be fine?

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

PS I really dislike setters. I prefer things to be immutable after instantiation.

@sagikazarmark

sagikazarmark commented Aug 1, 2016

Copy link
Copy Markdown
Member

PS I really dislike setters. I prefer things to be immutable after instantiation.

Me too, but considering that this adds an extra configuration effort, this seems to be a half-way solution to me.

Also, changing the constructor would be a BC break (unless the generator is optional?), while adding an extra config option and an optionally used setter is not.

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

I was thinking making it optional, with a default value of null, then filling it using a ternary statement, yeh.

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

Hmm, just looked at the constructor signature. I think using the options resolver might be better.

@sagikazarmark

Copy link
Copy Markdown
Member

Hmm, just looked at the constructor signature. I think using the options resolver might be better.

Hmm, right. You can still fall back to a string then using the hash implementation?

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

I've implemented the hash algo selection. How does that look?

Comment thread src/CachePlugin.php Outdated

return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) {
if ($this->isCacheable($response)) {
if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this from another PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, oops...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this from the github online editor, lol.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😝

@sagikazarmark

Copy link
Copy Markdown
Member

Can you please add change log? We follow keep a change log format.

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

Wanted to avoid conflicts with other PRs. Do you mind adding something on merge?

@Nyholm

Nyholm commented Aug 2, 2016

Copy link
Copy Markdown
Member

Thank you. 👍

@sagikazarmark

Copy link
Copy Markdown
Member

Looks good to me, thanks @GrahamCampbell

@sagikazarmark sagikazarmark merged commit c1608f5 into php-http:master Aug 2, 2016
@GrahamCampbell GrahamCampbell deleted the patch-3 branch August 2, 2016 08:55
@egeloen

egeloen commented Aug 5, 2016

Copy link
Copy Markdown

I'm a little bit late here but this PR is a BC break in a minor version. All my caches are become broken due to the new hash algo used for the cache key generation.

@sagikazarmark Can you confirm it has been done by mistake?

Anyway, as the new tag has been released, let it as it is, I just need to change my cache config.

@GrahamCampbell

Copy link
Copy Markdown
Contributor Author

I don't think it would be considered broken to just loose items from the cache?

@dbu

dbu commented Aug 5, 2016

Copy link
Copy Markdown
Contributor

did you get errors, or just cache misses? i think cache misses on a minor version sound ok. i hope we did not release it as a patch release though, that would be unexpected.

@sagikazarmark

Copy link
Copy Markdown
Member

I agree, cache misses doesn't sound like BC break.

@egeloen

egeloen commented Aug 5, 2016

Copy link
Copy Markdown

Yes, I probaby abuse when saying BC break as technically everything is working fine (just differently)... I use it in a specific context: caching http requests done to a third service secured by IPs and use the generated cache for tests execution on Travis but after upgrading, my cache is becomes broken. As already said, not a big deal to fix, just want to discuss your BC policy.

@Nyholm

Nyholm commented Aug 5, 2016

Copy link
Copy Markdown
Member

That is an interesting question. Our general BC promise is here: http://php-http.readthedocs.io/en/latest/httplug/backwards-compatibility.html

But should we consider the cache keys as a part of the BC promise? Whatever we decide should be documented.

I suggest that we are allowed to update the cache keys for minor versions but not for patches. But I would try to avoid it.

For the record:
This PR also updated the cache keys: #8 (merged and released)
And the PR for this issue will likely update the cache keys: #4

@joelwurtz

joelwurtz commented Aug 5, 2016

Copy link
Copy Markdown
Member

I would say the following :

Do not make our keys part of our BC promise, but consider them as it was the case.
So we can make changes to the key in a minor or patch version, but we should try our best to avoid that.

@sagikazarmark

Copy link
Copy Markdown
Member

Actually this specific PR could even be a bugfix: with massive usage it's possible that some requests collide using md5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants