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 integer length overflow in basic auth #6946

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dirkmueller
Copy link
Contributor

The string lenths were added and then stored in "char" which is
typically signed and limited to values up to 127. Using size_t
is a more appropriate type.

Simplify the code a bit as well to avoid the code smell.

@devyte devyte self-requested a review December 26, 2019 03:17
@dirkmueller dirkmueller changed the title Fix length overflow in username/password authentication Fix integer length overflow in basic auth Dec 26, 2019
#include "WiFiServer.h"
#include "WiFiClient.h"
#include "ESP8266WebServer.h"
#include "FS.h"

using namespace esp8266webserver;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't do this, at least not in a header file. This imports everything in esp8266webserver into the global namespace, which defeats the purpose of using a namespace.
It is sometimes ok to do this in a cpp file, but even then it's better to import into the cpp's anonymous namespace and not into the global one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am actually not able to spot something that is outside the namespace in "nm" symbol dump. can you elaborate an example? the using doesn't change namespace, it just avoid me having to put a quadrillion esp8266webserver:: prefixes to all the declarations..

I can do that if your concern is the "using" part.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although I'm not sure I understood the concern, because there are no globals only statics (which are not exported hence don't pollute and don't care in which namespace they're declared), I moved all #include's out of the -impl.h now and have my duplicate-import problem solved this way.

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h Outdated Show resolved Hide resolved
libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h Outdated Show resolved Hide resolved
libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h Outdated Show resolved Hide resolved
Comment on lines 236 to 306
#include "ESP8266WebServer-impl.h"
#include "Parsing-impl.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two were inside the namespace because their implementations are not in a namespace. Pulling them out like this means that the internals are moved to the global namespace, which is undesirable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the issue is that the things that they #include'ed was now being put into that namespace, which causes duplicate instanciations (code bloat) or missing symbols..

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're correct about the last part. In general, it is bad practice to include inside a namespace for this exact reason.
The correct way to handle this is to separate the headers, so a bit more work along the lines of the following. Are you game?

  1. ESP8266WebServer-impl.h:
    • Put namespace esp8266webserver around all the code in there.
    • In ESP8266WebServer.h move the include to after the namespace closing like you had previously proposed.
    • Move the three headers added to ESP8266WebServer.h in your latest proposal back to the top of ESP8266WebServer-impl.h.
  2. RequestHandler.h: Wrapping with a namespace here could cause havoc in multiple dimensions. To avoid that:
    • Fully qualify the template import like so: using WebServerType = esp8266webserver::ESP8266WebServerTemplate<ServerType>;
  3. RequestHandlersImpl.h:
    • Keep your removal of using namespace mime in the global namespace. and its use where specifically used
    • Qualify the using WebServerType same way as above (x2)
  4. Move the above includes out of the esp8266 namespace as you had proposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do that cleanup. does it belong into this PR? I would prefer it as a followup.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, I proposed the above as part of this PR because you started the include changes here. If you prefer to separate, what you could do is keep just the integer overflow fix for the ::authenticate() method here, and do the include cleanup in a different PR. What do you think?

@devyte
Copy link
Collaborator

devyte commented Dec 26, 2019

A side note:
Whether char is signed or unsigned is implementation specific. In the world of gcc, char is considered unsigned by default. It can be forced to be signed with some flag I don't remember at the moment.
We use the default char, so it's unsigned.
In any case, the correct type to hold a "size" is size_t, and not a char, so you're right about the current code being flaky.

@dirkmueller dirkmueller force-pushed the http_authenticate branch 2 times, most recently from e7d2f51 to d38814b Compare December 26, 2019 22:35
Comment on lines 236 to 306
#include "ESP8266WebServer-impl.h"
#include "Parsing-impl.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You're correct about the last part. In general, it is bad practice to include inside a namespace for this exact reason.
The correct way to handle this is to separate the headers, so a bit more work along the lines of the following. Are you game?

  1. ESP8266WebServer-impl.h:
    • Put namespace esp8266webserver around all the code in there.
    • In ESP8266WebServer.h move the include to after the namespace closing like you had previously proposed.
    • Move the three headers added to ESP8266WebServer.h in your latest proposal back to the top of ESP8266WebServer-impl.h.
  2. RequestHandler.h: Wrapping with a namespace here could cause havoc in multiple dimensions. To avoid that:
    • Fully qualify the template import like so: using WebServerType = esp8266webserver::ESP8266WebServerTemplate<ServerType>;
  3. RequestHandlersImpl.h:
    • Keep your removal of using namespace mime in the global namespace. and its use where specifically used
    • Qualify the using WebServerType same way as above (x2)
  4. Move the above includes out of the esp8266 namespace as you had proposed.

@earlephilhower earlephilhower added the merge-conflict PR has a merge conflict that needs manual correction label Feb 26, 2020
@dirkmueller dirkmueller marked this pull request as draft August 29, 2020 00:37
dirkmueller added a commit to dirkmueller/Arduino that referenced this pull request Sep 2, 2020
This is a followup of the discussion in
esp8266#6946 (comment)

to untangle the namespace/double inclusions in this library.
d-a-v pushed a commit that referenced this pull request Sep 17, 2020
untangle the namespace/double inclusions in webserver library.
This is a followup of the discussion in
#6946 (comment)
The string lengths were added and then stored in "char" which is
limited in values at most up to 255. Using size_t is a much more
appropriate type.

In addition the code was using base64 with newlines injected
(easy to fall into that trap as the default is imho wrong), which
means that anything longer than ~ 60 characters never matched and
you had no way to authenticate against the server.
@dirkmueller dirkmueller marked this pull request as ready for review September 17, 2020 17:37
@dirkmueller
Copy link
Contributor Author

@earlephilhower how can I remove the label "merge-conflict" ?

@earlephilhower earlephilhower removed the merge-conflict PR has a merge conflict that needs manual correction label Sep 17, 2020
@earlephilhower earlephilhower added the merge-conflict PR has a merge conflict that needs manual correction label Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict PR has a merge conflict that needs manual correction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants