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

Feat: Add method to extract HTTP Bearer token from Authorisation header. #1258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/pistache/http_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,15 @@ namespace Pistache::Http::Header
// Get decoded user ID and password if basic method was used...
std::string getBasicUser() const;
std::string getBasicPassword() const;
std::string getBearerToken() const;

// Set encoded user ID and password for basic method...
void setBasicUserPassword(const std::string& User,
const std::string& Password);


void setBearertoken(const std::string& token);

void parse(const std::string& data) override;
void write(std::ostream& os) const override;

Expand Down
10 changes: 10 additions & 0 deletions src/common/http_header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ namespace Pistache::Http::Header
return true;
}

std::string Authorization::getBearerToken() const
{
// Verify bearer authorization method was used...
if (!hasMethod<Authorization::Method::Bearer>())
throw std::runtime_error("Authorization header does not use Bearer method.");

const std::string token(value_.begin() + std::string("Bearer ").length(), value_.end());
Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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.

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 the

if (!hasMethod<Authorization::Method::Bearer>()) statement.

  1. Would you prefer that I check for that myself in the method body? It is not a problem at all to do so. I have been unable to cause any crashes with any partiality of the header value.
  2. Would you like that added as testcases?

Copy link
Member

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?

Copy link
Contributor

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!

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.

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.

Would it be illegal to have a null as part of the bearer token?

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.

If so, I would check for that before forming the std::string and reject any token that contains same.

Indeed, but that holds true for any header and not only the Authorization header.

Should we reject attempts to authenticate with bearer token when SSL is not in use?

No. That would make it annoyingly difficult to run a Pistache server behind a SSL terminating reverse proxy.

Not sure what else might be not allowed, but you get the idea.

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

Yes, unit tests for any such validity check would be great.
Will do!

I would like to suggest that

  1. I add additional tests for the token extraction
  2. We create an issue for several of the issues above to be addressed in a central "Header-parsing-place-that-I-dont-know -where-is-or-exists" and solve them for all headers in one implementation
  3. We merge the logic for Bearer extraction in this PR.

Copy link
Contributor

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!

Copy link
Author

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 ;-)

Copy link
Member

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?

return token;
}

// Get decoded user ID if basic method was used...
std::string Authorization::getBasicUser() const
{
Expand Down
12 changes: 12 additions & 0 deletions tests/headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ TEST(headers_test, authorization_basic_test)
// Verify it decoded correctly...
ASSERT_EQ(au.getBasicUser(), "Aladdin");
ASSERT_EQ(au.getBasicPassword(), "OpenSesame");
EXPECT_ANY_THROW( au.getBearerToken() );
}

TEST(headers_test, authorization_bearer_test)
Expand Down Expand Up @@ -458,6 +459,17 @@ TEST(headers_test, authorization_bearer_test)
"eyJleHAiOjE1NzA2MzA0MDcsImlhdCI6MTU3MDU0NDAwNywibmFtZSI6IkFkbWluIE5hbWUi"
"LCJzYW1wbGUiOiJUZXN0In0.zLTAAnBftlqccsU-4mL69P4tQl3VhcglMg-"
"d0131JxqX4xSZLlO5xMRrCPBgn_00OxKJ9CQdnpjpuzblNQd2-A");

EXPECT_ANY_THROW(au.getBasicUser() );
EXPECT_ANY_THROW(au.getBasicPassword() );

ASSERT_EQ(au.getBearerToken(),
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXUyJ9."
"eyJleHAiOjE1NzA2MzA0MDcsImlhdCI6MTU3MDU0NDAwNywibmFtZSI6IkFkbWluIE5hbWUi"
"LCJzYW1wbGUiOiJUZXN0In0.zLTAAnBftlqccsU-4mL69P4tQl3VhcglMg-"
"d0131JxqX4xSZLlO5xMRrCPBgn_00OxKJ9CQdnpjpuzblNQd2-A");


}

TEST(headers_test, expect_test)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.11.20241018
0.4.12.20241028
Loading