mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
WebServer: use String when working with Basic authentication (#8548)
Avoid blowing up user code when `$user:$password` string is longer than 127 bytes. Use String to both manage the memory and handle concatenation. Also clean-up historical quicks such as - `if(StringObject)` that is always true since we implemented SSO - `authReq = "";` / `authReq = String();`, which will happen anyway - `(String)...` casts that happen anyway, implicitly (and which is also not a 'cast' btw, we do init it)
This commit is contained in:
parent
f149d7b70e
commit
1a49a0449b
@ -102,31 +102,31 @@ bool ESP8266WebServerTemplate<ServerType>::authenticate(const char * username, c
|
|||||||
if(authReq.startsWith(F("Basic"))){
|
if(authReq.startsWith(F("Basic"))){
|
||||||
authReq = authReq.substring(6);
|
authReq = authReq.substring(6);
|
||||||
authReq.trim();
|
authReq.trim();
|
||||||
char toencodeLen = strlen(username)+strlen(password)+1;
|
|
||||||
char *toencode = new (std::nothrow) char[toencodeLen + 1];
|
const size_t username_len = strlen(username);
|
||||||
if(toencode == NULL){
|
const size_t password_len = strlen(password);
|
||||||
authReq = "";
|
|
||||||
|
String raw;
|
||||||
|
raw.reserve(username_len + password_len + 1);
|
||||||
|
raw.concat(username, username_len);
|
||||||
|
raw += ':';
|
||||||
|
raw.concat(password, password_len);
|
||||||
|
if(!raw.length()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
sprintf(toencode, "%s:%s", username, password);
|
|
||||||
String encoded = base64::encode((uint8_t *)toencode, toencodeLen, false);
|
String encoded = base64::encode(raw, false);
|
||||||
if(!encoded){
|
if(!encoded.length()){
|
||||||
authReq = "";
|
|
||||||
delete[] toencode;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(authReq.equalsConstantTime(encoded)) {
|
if(authReq.equalsConstantTime(encoded)) {
|
||||||
authReq = "";
|
|
||||||
delete[] toencode;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
delete[] toencode;
|
|
||||||
} else if(authReq.startsWith(F("Digest"))) {
|
} else if(authReq.startsWith(F("Digest"))) {
|
||||||
String _realm = _extractParam(authReq, F("realm=\""));
|
String _realm = _extractParam(authReq, F("realm=\""));
|
||||||
String _H1 = credentialHash((String)username,_realm,(String)password);
|
String _H1 = credentialHash(username,_realm,password);
|
||||||
return authenticateDigest((String)username,_H1);
|
return authenticateDigest(username,_H1);
|
||||||
}
|
}
|
||||||
authReq = "";
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user