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

Use status instead of message for error responses to detect Unauthorized requests #1045

Merged
merged 2 commits into from
May 11, 2021

Conversation

Legion2
Copy link
Contributor

@Legion2 Legion2 commented May 9, 2021

With HTTP2 there is no status code reason-phrase and even in HTTP1 it is optional. Therefore the response status codes must be used to detect unauthorized request errors.

fix #1018

@ghys based on your comment I assume the second argument is the integer status code, I did not test this code, but I think it should work.

Signed-off-by: Leon Kiefer <leon.k97@gmx.de>
@Legion2 Legion2 requested a review from a team as a code owner May 9, 2021 11:08
@Legion2
Copy link
Contributor Author

Legion2 commented May 10, 2021

@ghys ESLint is complaining about "Expected error to be handled node/handle-callback-err", because now I don't use the first argument of the error callback, because it is not useful in this case. Should I disable this lint rule for these two lines?

@ghys
Copy link
Member

ghys commented May 10, 2021

@ghys based on your comment I assume the second argument is the integer status code, I did not test this code, but I think it should work.

I'm absolutely not 100% sure about this, it could be an object with the code inside. Will have to check.

@ghys
Copy link
Member

ghys commented May 10, 2021

Well, actually...
Promise.reject() (and Promise.resolve() for that matter) can only pass one argument (the reason resp. the value) so this code:

f7promise
.then((data) => resolve(data.data, data.status, data.xhr))
.catch((err) => reject(err.message, err.status, err.xhr))
})

is completely wrong 😠. In other terms status and xhr will always be undefined.
Simply rejecting with err isn't ideal because there are lots of instances in catch clauses where the reason is assumed to be the HTTP status message ('Not found', 'Unauthorized', 'Conflict' etc.) and puts it in a toast notification.

So I guess an ugly workaround would be to change the rejection above to reject(err.message || err.status) and then check on both the status and the message (if (err === 'Unauthorized' || err === 401) {) in the catch clauses, so that it fallbacks to status code when there's no message; that way there's no code change needed elsewhere but in your case where there's no HTTP status message err would still contain 401.

@Legion2
Copy link
Contributor Author

Legion2 commented May 11, 2021

@ghys I updated the code according to your comments.

@ghys
Copy link
Member

ghys commented May 11, 2021

Thank you. Shame we have to deal with it like this but it'll do :)
One last thing, may I ask to add || err === 401 similarly in these two places too:

if (err === 'Unauthorized') requireToken = true

These are parts of the logic that will redirect to the authorize page when you enforce authorization for the non-admin ops (Settings > API Security > Show advanced > Implicit user role for unauthenticated requests disabled).

added workaround for empty reason-phrase

Signed-off-by: Leon Kiefer <leon.k97@gmx.de>
@Legion2 Legion2 force-pushed the fix-rest-credentials-retry branch from d801864 to 5f6f0d6 Compare May 11, 2021 14:08
@Legion2
Copy link
Contributor Author

Legion2 commented May 11, 2021

I added the workaround also to these two places.

Copy link
Member

@ghys ghys left a comment

Choose a reason for hiding this comment

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

Excellent, thanks!

@relativeci
Copy link

relativeci bot commented May 11, 2021

Job #113: Bundle Size — 10.43MB (~-0.01%).

c98ce20 vs da07c93

Changed metrics (1/8)
Metric Current Baseline
Initial JS 1.6MB(~+0.01%) 1.6MB
Changed assets by type (1/7)
            Current     Baseline
JS 8.14MB (~-0.01%) 8.14MB

View Job #113 report on app.relative-ci.com

@ghys ghys merged commit 4e402ef into openhab:main May 11, 2021
@Legion2 Legion2 deleted the fix-rest-credentials-retry branch May 11, 2021 15:14
@ghys ghys added this to the 3.1 milestone May 30, 2021
@ghys ghys added main ui Main UI bug Something isn't working labels May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working main ui Main UI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to disable Service Worker
2 participants