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

fix(oauth): sort parameters in a standard way as per the specs #721

Merged
merged 1 commit into from
Nov 3, 2019
Merged

fix(oauth): sort parameters in a standard way as per the specs #721

merged 1 commit into from
Nov 3, 2019

Conversation

real34
Copy link
Contributor

@real34 real34 commented Jun 13, 2019

Hi folks!

We have detected what seems to be a flaw in the way the OAuth signature is calculated to ensure the received request is correct.
From our understanding it seems to be a bug, but I’d really appreciate if someone more used to this standard could have a look :)

The symptom is: 401 errors with an invalid_signature error for requests containing numerical indexes.

Example:

Given the following params:

  • limit => 21
  • filter[0][attribute] => sku
  • filter[0][in][0] => msj000
  • filter[0][in][1] => msj001
  • filter[0][in][2] => msj002
  • filter[0][in][3] => msj000xs
  • filter[0][in][4] => msj000xl
  • filter[0][in][5] => msj003xs
  • filter[0][in][6] => msj003xl
  • filter[0][in][7] => msj006
  • filter[0][in][8] => msj007
  • filter[0][in][9] => msj008
  • filter[0][in][10] => msj006xl
  • filter[0][in][11] => msj006xs

Magento will use natsort to sort them before generating the signature. The order will thus be:

  • filter[0][attribute] => sku
  • filter[0][in][0] => msj000
  • filter[0][in][1] => msj001
  • filter[0][in][2] => msj002
  • filter[0][in][3] => msj000xs
  • filter[0][in][4] => msj000xl
  • filter[0][in][5] => msj003xs
  • filter[0][in][6] => msj003xl
  • filter[0][in][7] => msj006
  • filter[0][in][8] => msj007
  • filter[0][in][9] => msj008
  • filter[0][in][10] => msj006xl <------
  • filter[0][in][11] => msj006xs <------
  • limit => 21

The library we use in nodeJS uses a standard sort (see https://github.com/ddo/oauth-1.0a/blob/d2ec1e2820ff60e99da4444deae630d8eac31d91/oauth-1.0a.js#L366) and will generate a signature with the following order:

  • filter[0][attribute] => sku
  • filter[0][in][0] => msj000
  • filter[0][in][1] => msj001
  • filter[0][in][10] => msj006xl <-------
  • filter[0][in][11] => msj006xs <-------
  • filter[0][in][2] => msj002
  • filter[0][in][3] => msj000xs
  • filter[0][in][4] => msj000xl
  • filter[0][in][5] => msj003xs
  • filter[0][in][6] => msj003xl
  • filter[0][in][7] => msj006
  • filter[0][in][8] => msj007
  • filter[0][in][9] => msj008
  • limit => 21

As a consequence, signatures will not match…

Resources

Our researches led us to the OAuth spec which says:

parameters are sorted by name, using lexicographical byte value ordering. If two or more parameters share the same name, they are sorted by their value
(source: https://oauth.net/core/1.0a/#rfc.section.9.1.1)

From our understanding it seems that the Zend_Oauth is the wrong implementation…

Also, it seems that other implementations such as https://github.com/thephpleague/oauth1-client/blob/master/src/Client/Signature/HmacSha1Signature.php#L73 are also sorting parameters as the node library linked above.

Breaking change

This IS a breaking change, also I am not sure whether you think it could be merged or not in its current state.

Tests

This is my first contribution here, so I am relying on the CI process to see if it breaks things. I could add tests later if you think it is necessary.

Please let me know if you need further information.

the signature is now calculated a bit differently to match other
implementations.

BREAKING CHANGE: existing OAuth applications may encounter some issues
leading to `invalid_signature` 401 errors from Magento due to the
removal of natural sorting for parameters when generating the signature
Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

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

It seems "using lexicographical byte value ordering" from the official spec is pretty clear.

@real34
Copy link
Contributor Author

real34 commented Jun 20, 2019

Thanks for the feedback.
Do you think it could/should be merged as is?

@colinmollenhour
Copy link
Member

Yes, thus my approval. :)

It is hard to say if anyone is relying on this incorrect behavior but as this hasn't been reported yet I would say the chances are very slim and it is better to fix it and make it correct than leave it incorrect.

@real34
Copy link
Contributor Author

real34 commented Jun 21, 2019

Awesome. Thanks for your quick feedback!

@real34
Copy link
Contributor Author

real34 commented Nov 1, 2019

Hi! It's been a few months since @colinmollenhour's approval.

Would it be possible to have a 2nd review for this PR please? It is a blocking issue for headless implementations of M1 (that has to be patched in userland code for now)

(ping @sreichel @Flyingmana)

@Flyingmana Flyingmana merged commit 0e90e98 into OpenMage:1.9.4.x Nov 3, 2019
@real34 real34 deleted the standard-oauth-params-sorting-signature branch November 3, 2019 20:07
@real34
Copy link
Contributor Author

real34 commented Nov 3, 2019

Thank you very much! 🎉

edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Aug 20, 2020
BREAKING CHANGE: existing OAuth applications may encounter some issues
leading to `invalid_signature` 401 errors from Magento due to the
removal of natural sorting for parameters when generating the signature
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