@@ -356,6 +356,7 @@ static bool _origin_ok(const char *origin) {
356
356
const char * http = "http://" ;
357
357
const char * local = ".local" ;
358
358
359
+ // note: redirected requests send an Origin of "null" and will be caught by this
359
360
if (memcmp (origin , http , strlen (http )) != 0 ) {
360
361
return false;
361
362
}
@@ -374,8 +375,11 @@ static bool _origin_ok(const char *origin) {
374
375
return true;
375
376
}
376
377
377
- const char * localhost = "127.0.0.1:" ;
378
- if (memcmp (origin + strlen (http ), localhost , strlen (localhost )) == 0 ) {
378
+ // Port or no port
379
+ const char * localhost = "127.0.0.1" ;
380
+ const int locallen = 9 ;
381
+ if (memcmp (origin + strlen (http ), localhost , locallen ) == 0
382
+ && (localhost [locallen ] == '\0' || localhost [locallen ] == ':' )) {
379
383
return true;
380
384
}
381
385
@@ -909,7 +913,8 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
909
913
ESP_LOGE (TAG , "bad origin %s" , request -> origin );
910
914
_reply_forbidden (socket , request );
911
915
} else if (memcmp (request -> path , "/fs/" , 4 ) == 0 ) {
912
- if (!request -> authenticated ) {
916
+ // OPTIONS is sent for CORS preflight, unauthenticated
917
+ if (!request -> authenticated && strcmp (request -> method , "OPTIONS" ) != 0 ) {
913
918
if (_api_password [0 ] != '\0' ) {
914
919
_reply_unauthorized (socket , request );
915
920
} else {
@@ -1030,7 +1035,10 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
1030
1035
}
1031
1036
} else if (memcmp (request -> path , "/cp/" , 4 ) == 0 ) {
1032
1037
const char * path = request -> path + 3 ;
1033
- if (strcmp (request -> method , "GET" ) != 0 ) {
1038
+ if (strcmp (request -> method , "OPTIONS" ) == 0 ) {
1039
+ // handle preflight requests to /cp/
1040
+ _reply_access_control (socket , request );
1041
+ } else if (strcmp (request -> method , "GET" ) != 0 ) {
1034
1042
_reply_method_not_allowed (socket , request );
1035
1043
} else if (strcmp (path , "/devices.json" ) == 0 ) {
1036
1044
_reply_with_devices_json (socket , request );
0 commit comments