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

Raising MemcacheIllegalInputError when key contains null byte, newline, or carriage return #138

Merged
merged 10 commits into from
Feb 18, 2017

Conversation

bwalks
Copy link
Contributor

@bwalks bwalks commented Feb 13, 2017

Memcache does not support setting keys with null byte, newline, carriage return. This adds support for checking the key to ensure the key is valid.

Setting keys with these returns the following exception:

>>> client.set('\r\ntest', 'foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/memcache.py", line 139, in set
    return super(Client, self).set(key, value, expire=expire, noreply=noreply)
  File .../env/lib/python2.7/site-packages/pymemcache/client/base.py", line 256, in set
    return self._store_cmd(b'set', key, expire, noreply, value)
  File .../env/lib/python2.7/site-packages/pymemcache/client/base.py", line 747, in _store_cmd
    self._raise_errors(line, name)
  File ".../env/lib/python2.7/site-packages/pymemcache/client/base.py", line 654, in _raise_errors
    raise MemcacheUnknownCommandError(name)
pymemcache.exceptions.MemcacheUnknownCommandError: set

This has been tested using version 1.3.5 on python 2.7.10

@cgordon
Copy link
Collaborator

cgordon commented Feb 14, 2017

With this change, we are walking the entire key string three times to do validation. It's probably worth doing one walk, and checking each character along the way, rather than using "in" to do the work.

@bwalks
Copy link
Contributor Author

bwalks commented Feb 14, 2017

@cgordon I agree. I updated the commit to iterate over the characters once. I also split out the check for newline to enable a more specific error message.

@bwalks
Copy link
Contributor Author

bwalks commented Feb 15, 2017

The tests were failing because the character checks differ between python 2 and 3. Iterating over the binary string in python 2 returns characters, while python 3 returns the ordinal value. I am not that familiar with a better way to handle this. If you have any suggestions, please let me know!

Edit: I converted the key to a byte array instead

@nichochar
Copy link
Collaborator

This looks good to me, thanks for the catch, and the improved optimized traversing. The build is failing for an unrelated issue, which will be fixed in #140

Once that's merged, please rebase and push, and we can go ahead and merge this.

@bwalks
Copy link
Contributor Author

bwalks commented Feb 18, 2017

@nichochar thanks for the update! Pulled in the latest changes.

@nichochar nichochar merged commit dcdea28 into pinterest:master Feb 18, 2017
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.

3 participants