-
Notifications
You must be signed in to change notification settings - Fork 701
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
Feat: Add method to extract HTTP Bearer token from Authorisation header. #1258
Open
mowijo
wants to merge
1
commit into
pistacheio:master
Choose a base branch
from
mowijo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.4.11.20241018 | ||
0.4.12.20241028 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a problem. You need to make sure that there was a properly formatted token after
Bearer
. Otherwise you could end up with a scenario where the client sends a malformed request, perhaps intentionally, and it takes down the server.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I see the need for security, but I don't understand how to check if the token is properly formatted after
Bearer
since we don't know what sort of token people might want to exchange. All I can think of is, to verify that the value of the header value matches this^Bearer [!-~]+$
. (Only printable ascii chars except space).Do you think that will suffice? If not, what sort of check did you have in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well as it is right now, unless I'm reading it wrong, you're not checking for the presence of anything which could lead to a segfault.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a check that the string starts with
"Bearer "
. That is done by theif (!hasMethod<Authorization::Method::Bearer>())
statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it not checking for anything after "Bearer " though before trying to initialize the std::string's internal buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check for a minimum token size, and maximum, and reject if prospective length falls outside that length.
Minimum - definitely the size needs to be > 0. A common minimum is 16 bytes. To be conservative, I would probably reject any token of less than 4 bytes in length.
Maximum - most web servers won't except more than 8KB of headers, so that forms an effective max length. Again, being conservative, we could reject anything with length > 16KB.
Would it be illegal to have a null as part of the bearer token? If so, I would check for that before forming the std::string and reject any token that contains same.
Should we reject attempts to authenticate with bearer token when SSL is not in use?
Not sure what else might be not allowed, but you get the idea.
Yes, unit tests for any such validity check would be great.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think it shold be up to a framework to enforce a minimum size of a token. Likewise, no minimum size for the password for Basic is implemented in Pistache and
Limiting to a specific length for security reasons sounds like a good idea, but would hold true for all headers.
Yes that would indeedbe illegal. According to RFC7230 section 3.2, the content of a header field must be only visible ASCII chars and optional folds, but I don't see how that logic belongs in the Authorization-specialization.
Indeed, but that holds true for any header and not only the Authorization header.
No. That would make it annoyingly difficult to run a Pistache server behind a SSL terminating reverse proxy.
Honestly, no. I do not get the idea.
It seems like all the issues above are issues that are general for all http headers. Placing such checks in the Authorization header seems like too low in the abstraction to me. If these checks should be performed (and they should) , it should be at a higher level so the individual header specializations
I would like to suggest that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with most of this except that I would have your code at least check for a non-zero token size, i.e. reject a zero-sized token, if you're not willing to reject a token of less than 4 bytes as a stricter test.
I agree that pathologically large headers, or headers containing a null, are applicable to all headers and would best be dealt with at a higher layer, just as you said. Would be great if you would create issues for same - and for any other header validity check you think we might include but which is not covered today.
Thanks again!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. That sounds good. I can do that.
Work just took up a lot of my spare time the couple of days ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mowijo, can you update us on your progress?