-
Notifications
You must be signed in to change notification settings - Fork 7.7k
fix(csrf): Fix SCRF vulnerability in OTA examples and libraries #11530
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
Changes from 2 commits
450c640
a87ee5f
5f74e65
b29b5e8
c94ffcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,16 @@ | |
const char *host = "esp32-webupdate"; | ||
const char *ssid = "........"; | ||
const char *password = "........"; | ||
const char * authUser = "admin"; | ||
const char * authPass = "admin"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same feedback here. Generate a random password, and serial print it. I would suggest avoiding having this hard-coded in this example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are missing the point that this is an Arduino basic example, whose purpose is not to secure every possible scenario, but instead to showcase a particular use (the Updater class). No sane person will use Using |
||
|
||
WebServer server(80); | ||
const char *serverIndex = | ||
"<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>"; | ||
|
||
const char * csrfHeaders[2] = {"Origin", "Host"}; | ||
static bool authenticated = false; | ||
|
||
void setup(void) { | ||
Serial.begin(115200); | ||
Serial.println(); | ||
|
@@ -24,37 +29,63 @@ void setup(void) { | |
WiFi.begin(ssid, password); | ||
if (WiFi.waitForConnectResult() == WL_CONNECTED) { | ||
MDNS.begin(host); | ||
server.collectHeaders(csrfHeaders, 2); | ||
server.on("/", HTTP_GET, []() { | ||
if (!server.authenticate(authUser, authPass)) { | ||
return server.requestAuthentication(); | ||
} | ||
server.sendHeader("Connection", "close"); | ||
server.send(200, "text/html", serverIndex); | ||
}); | ||
server.on( | ||
"/update", HTTP_POST, | ||
[]() { | ||
if (!authenticated) { | ||
return server.requestAuthentication(); | ||
} | ||
server.sendHeader("Connection", "close"); | ||
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); | ||
ESP.restart(); | ||
if (Update.hasError()) { | ||
server.send(200, "text/plain", "FAIL"); | ||
} else { | ||
server.send(200, "text/plain", "Success! Rebooting..."); | ||
delay(500); | ||
ESP.restart(); | ||
} | ||
}, | ||
[]() { | ||
HTTPUpload &upload = server.upload(); | ||
if (upload.status == UPLOAD_FILE_START) { | ||
Serial.setDebugOutput(true); | ||
authenticated = server.authenticate(authUser, authPass); | ||
if (!authenticated) { | ||
Serial.println("Authentication fail!"); | ||
return; | ||
} | ||
String origin = server.header(String(csrfHeaders[0])); | ||
String host = server.header(String(csrfHeaders[1])); | ||
String expectedOrigin = String("http://") + host; | ||
if (origin != expectedOrigin) { | ||
Serial.printf("Wrong origin received! Expected: %s, Received: %s\n", expectedOrigin.c_str(), origin.c_str()); | ||
authenticated = false; | ||
return; | ||
} | ||
|
||
Serial.printf("Update: %s\n", upload.filename.c_str()); | ||
if (!Update.begin()) { //start with max available size | ||
Update.printError(Serial); | ||
} | ||
} else if (upload.status == UPLOAD_FILE_WRITE) { | ||
} else if (authenticated && upload.status == UPLOAD_FILE_WRITE) { | ||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { | ||
Update.printError(Serial); | ||
} | ||
} else if (upload.status == UPLOAD_FILE_END) { | ||
} else if (authenticated && upload.status == UPLOAD_FILE_END) { | ||
if (Update.end(true)) { //true to set the size to the current progress | ||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); | ||
} else { | ||
Update.printError(Serial); | ||
} | ||
Serial.setDebugOutput(false); | ||
} else { | ||
} else if(authenticated) { | ||
Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest generating a random password that you print to the console to let the user know what it is over using a hard-coded password.