Skip to content

Commit 1a49a04

Browse files
authored
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)
1 parent f149d7b commit 1a49a04

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,31 @@ bool ESP8266WebServerTemplate<ServerType>::authenticate(const char * username, c
102102
if(authReq.startsWith(F("Basic"))){
103103
authReq = authReq.substring(6);
104104
authReq.trim();
105-
char toencodeLen = strlen(username)+strlen(password)+1;
106-
char *toencode = new (std::nothrow) char[toencodeLen + 1];
107-
if(toencode == NULL){
108-
authReq = "";
105+
106+
const size_t username_len = strlen(username);
107+
const size_t password_len = strlen(password);
108+
109+
String raw;
110+
raw.reserve(username_len + password_len + 1);
111+
raw.concat(username, username_len);
112+
raw += ':';
113+
raw.concat(password, password_len);
114+
if(!raw.length()) {
109115
return false;
110116
}
111-
sprintf(toencode, "%s:%s", username, password);
112-
String encoded = base64::encode((uint8_t *)toencode, toencodeLen, false);
113-
if(!encoded){
114-
authReq = "";
115-
delete[] toencode;
117+
118+
String encoded = base64::encode(raw, false);
119+
if(!encoded.length()){
116120
return false;
117121
}
118122
if(authReq.equalsConstantTime(encoded)) {
119-
authReq = "";
120-
delete[] toencode;
121123
return true;
122124
}
123-
delete[] toencode;
124125
} else if(authReq.startsWith(F("Digest"))) {
125126
String _realm = _extractParam(authReq, F("realm=\""));
126-
String _H1 = credentialHash((String)username,_realm,(String)password);
127-
return authenticateDigest((String)username,_H1);
127+
String _H1 = credentialHash(username,_realm,password);
128+
return authenticateDigest(username,_H1);
128129
}
129-
authReq = "";
130130
}
131131
return false;
132132
}

0 commit comments

Comments
 (0)