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

Handle Base64 Exception #8

Merged
merged 2 commits into from
May 17, 2023
Merged
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
31 changes: 22 additions & 9 deletions source/serverino/interfaces.d
Original file line number Diff line number Diff line change
Expand Up @@ -575,22 +575,35 @@ struct Request

if ("authorization" in _header)
{
import std.base64 : Base64;

import std.base64 : Base64, Base64Exception;
import std.string : indexOf;
auto auth = _header["authorization"];

if (auth.length > 6 && auth[0..6].toLower == "basic ")
{
auth = (cast(char[])Base64.decode(auth[6..$])).to!string;
auto delim = auth.indexOf(":");

if (delim < 0) _user = auth;
else
try
{
_user = auth[0..delim];
auth = (cast(char[])Base64.decode(auth[6..$])).to!string;
auto delim = auth.indexOf(":");

if (delim < 0) _user = auth;
else
{
_user = auth[0..delim];

if (delim < auth.length-1)
_password = auth[delim+1..$];
if (delim < auth.length-1)
_password = auth[delim+1..$];
}

}
catch(Base64Exception e)
{
_user = string.init;
_password = string.init;
debug warning("Authorization header ignored. Error decoding base64.");

m1dok marked this conversation as resolved.
Show resolved Hide resolved

}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test-1/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,28 @@ Content-Disposition: form-data; name=\"field1\"\r
}


///Authorization Base64 Test

{
string content;

auto http = HTTP("http://myuser@127.0.0.1:8080/json/dump/test");
http.method = HTTP.Method.post;
http.addRequestHeader("Authorization", "Basic msnmsknkjs");
http.onReceive = (ubyte[] data) { content ~= data; return data.length; };
http.perform();

auto j = parseJSON(content);
assert(j["method"].str == "POST");
assert(j["uri"].str == "/json/dump/test");
assert(j["host"].str == "127.0.0.1:8080");
assert(j["username"].str == string.init);
assert(j["password"].str == string.init);

assert(http.statusLine.code == 200);


}


}