Skip to content

Conversation

@charsyam
Copy link

earlier version python2.7.7
hmac doesn't support compare_digest
so I implement workaround for this. :)

@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch from 85fe814 to 276e67c Compare October 18, 2016 16:47
@be-hase
Copy link
Member

be-hase commented Oct 19, 2016

Hi, @charsyam
Thanks for your feedback. 😄
But str comparing is not good because of timing attack.
( https://en.wikipedia.org/wiki/Timing_attack )

OK. In the near future, I will fix this issue and release 1.0.2 version.

@darjeeling
Copy link

@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch 4 times, most recently from f44920c to 305adf6 Compare October 19, 2016 04:42
@charsyam
Copy link
Author

Hi, @be-hase

could you review this? I implemented safe_compare_digest func to refer tscmp

@be-hase
Copy link
Member

be-hase commented Oct 19, 2016

Oh... thanks for your patch. 👍

return hmac.compare_digest(
signature.encode('utf-8'), base64.b64encode(gen_signature)
)
if hexversion >= 0x020707F0:
Copy link
Member

Choose a reason for hiding this comment

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

Would you like to use if hasattr(hmac, "compare_digest"): ?
I think that it is readable.

@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch from 305adf6 to f590147 Compare October 19, 2016 05:48
return ret == 0


compare_digest = safe_compare_digest
Copy link
Author

Choose a reason for hiding this comment

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

@be-hase how about this approach?
I applied hasattr and made function reference as compare_digest.

@okdtsk
Copy link
Member

okdtsk commented Oct 19, 2016

One more thing, we adopt Google style docstring but yours is reST format. Can you rewrite your function's docstring as Google one?

Google python style guide
https://google.github.io/styleguide/pyguide.html?showone=Comments#Comments

This SO question will help you :)
http://stackoverflow.com/a/24385103

@okdtsk
Copy link
Member

okdtsk commented Oct 19, 2016

Why does not use len2 directly when comparing to len1?
And the line if len1 != length can be rewritten else statement under if len1 == length, which will have same meaning, same cost to avoid time at track and more readable.

@charsyam
Copy link
Author

@okdtsk Good Catch, I will apply your review to use len2 directlry
and I have a question about docstring.
python webhook.py and other code just follow reST format not Google Style.
could you please check this?

@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch from f590147 to 9450c09 Compare October 19, 2016 06:27
if len1 == len2:
left = str1
ret = 0

Choose a reason for hiding this comment

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

why not else ?

Copy link
Author

Choose a reason for hiding this comment

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

In your hint. There is an answer. so I avoid to use else :)
/* don't use else here to keep the amount of CPU instructions constant,
* volatile forces re-evaluation */

@be-hase
Copy link
Member

be-hase commented Oct 19, 2016

I'm sorry reply late.

docstring

Yes, we use reST format. Sorry @okdtsk 😢

This feature is sensitive, so I want a test code.

@okdtsk
Copy link
Member

okdtsk commented Oct 19, 2016

@charsyam @be-hase
Ah sorry I checked previous code. Please ignore my comment about docstring. 😭

@be-hase
Copy link
Member

be-hase commented Oct 19, 2016

And...
I think that we can simplify like below.

def compare_digest(target1, target2):
    if len(target1) != len(target1):
        return False

    result = 0
    for x, y in zip(target1, target2):
        result |= ord(x) ^ ord(y)

    return result == 0

@charsyam
Copy link
Author

@be-hase Yes, I also worry about that. How we can test it well?
because I think this verify consuming time is equals.

@charsyam
Copy link
Author

@be-hase and We can't just return False when length is different.
(It violates timeing attack.)

@be-hase
Copy link
Member

be-hase commented Oct 19, 2016

@charsyam
At least, I want to check like a below. (very simple checking 😄 )
https://github.com/line/line-bot-sdk-python/blob/master/tests/test_webhook.py#L37-L46

LINE Signature is hashed by SHA-256 algorithm, so bytes length must be 32bytes.
Then we just need constant-time-compare.

(Example)
For the same reason, Django provide as below.
https://github.com/django/django/blob/master/django/utils/crypto.py#L80-L105

@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch 2 times, most recently from af51cb1 to 9827e6d Compare October 19, 2016 08:57
linebot/utils.py Outdated
def safe_compare_digest(val1, val2):
"""safe_compare_digest method.
:param str or bytes val1: string or bytes for compare
Copy link
Member

Choose a reason for hiding this comment

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

OK, looks good to me.

Sorry..., How about this ?

:param val1: string or bytes for compare
:type val1: str | bytes

ref:
https://github.com/line/line-bot-sdk-python/blob/master/linebot/api.py#L79-L83

And Have you signed up ICLA?
( https://feedback.line.me/enquete/public/919-h9Yqmr1u )
If you have completed, I will merge and release 1.0.2

Thanks 😄

fix indent

implement tscmp for preventing timing attack

remove semicolon

apply flake8

apply flake8

refactoring using hasattr

refactoring using len2 directly

add test and refactoring

add test and refactoring

change docstring
@charsyam charsyam force-pushed the bugfix/hmac-compare-digest-for-under-python277 branch from 9827e6d to 29759f7 Compare October 19, 2016 09:34
@charsyam
Copy link
Author

@be-hase I singed ICLA and update your review for changing docstring Thanks

@be-hase be-hase merged commit b763069 into line:master Oct 19, 2016
@be-hase be-hase mentioned this pull request Oct 19, 2016
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.

4 participants