@@ -350,41 +350,40 @@ static bool _endswith(const char *str, const char *suffix) {
350
350
return strcmp (str + (strlen (str ) - strlen (suffix )), suffix ) == 0 ;
351
351
}
352
352
353
- const char * ok_hosts [] = {"code.circuitpython.org" };
353
+ const char * ok_hosts [] = {
354
+ "code.circuitpython.org" ,
355
+ "127.0.0.1" ,
356
+ "localhost" ,
357
+ };
354
358
355
359
static bool _origin_ok (const char * origin ) {
356
360
const char * http = "http://" ;
357
361
const char * local = ".local" ;
358
362
359
363
// note: redirected requests send an Origin of "null" and will be caught by this
360
- if (memcmp (origin , http , strlen (http )) != 0 ) {
364
+ if (strncmp (origin , http , strlen (http )) != 0 ) {
361
365
return false;
362
366
}
363
367
// These are prefix checks up to : so that any port works.
364
368
const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
365
369
const char * end = origin + strlen (http ) + strlen (hostname ) + strlen (local );
366
- if (memcmp (origin + strlen (http ), hostname , strlen (hostname )) == 0 &&
367
- memcmp (origin + strlen (http ) + strlen (hostname ), local , strlen (local )) == 0 &&
370
+ if (strncmp (origin + strlen (http ), hostname , strlen (hostname )) == 0 &&
371
+ strncmp (origin + strlen (http ) + strlen (hostname ), local , strlen (local )) == 0 &&
368
372
(end [0 ] == '\0' || end [0 ] == ':' )) {
369
373
return true;
370
374
}
371
375
372
376
end = origin + strlen (http ) + strlen (_our_ip_encoded );
373
- if (memcmp (origin + strlen (http ), _our_ip_encoded , strlen (_our_ip_encoded )) == 0 &&
377
+ if (strncmp (origin + strlen (http ), _our_ip_encoded , strlen (_our_ip_encoded )) == 0 &&
374
378
(end [0 ] == '\0' || end [0 ] == ':' )) {
375
379
return true;
376
380
}
377
381
378
- const char * localhost = "127.0.0.1" ;
379
- end = origin + strlen (http ) + strlen (localhost );
380
- if (memcmp (origin + strlen (http ), localhost , strlen (localhost )) == 0
381
- && (end [0 ] == '\0' || end [0 ] == ':' )) {
382
- return true;
383
- }
384
-
385
382
for (size_t i = 0 ; i < MP_ARRAY_SIZE (ok_hosts ); i ++ ) {
386
- // This checks exactly.
387
- if (strcmp (origin + strlen (http ), ok_hosts [i ]) == 0 ) {
383
+ // Allows any port
384
+ end = origin + strlen (http ) + strlen (ok_hosts [i ]);
385
+ if (strncmp (origin + strlen (http ), ok_hosts [i ], strlen (ok_hosts [i ])) == 0
386
+ && (end [0 ] == '\0' || end [0 ] == ':' )) {
388
387
return true;
389
388
}
390
389
}
@@ -911,7 +910,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
911
910
} else if (strlen (request -> origin ) > 0 && !_origin_ok (request -> origin )) {
912
911
ESP_LOGE (TAG , "bad origin %s" , request -> origin );
913
912
_reply_forbidden (socket , request );
914
- } else if (memcmp (request -> path , "/fs/" , 4 ) == 0 ) {
913
+ } else if (strncmp (request -> path , "/fs/" , 4 ) == 0 ) {
915
914
if (strcasecmp (request -> method , "OPTIONS" ) == 0 ) {
916
915
// OPTIONS is sent for CORS preflight, unauthenticated
917
916
_reply_access_control (socket , request );
@@ -1032,7 +1031,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
1032
1031
}
1033
1032
}
1034
1033
}
1035
- } else if (memcmp (request -> path , "/cp/" , 4 ) == 0 ) {
1034
+ } else if (strncmp (request -> path , "/cp/" , 4 ) == 0 ) {
1036
1035
const char * path = request -> path + 3 ;
1037
1036
if (strcasecmp (request -> method , "OPTIONS" ) == 0 ) {
1038
1037
// handle preflight requests to /cp/
@@ -1177,7 +1176,7 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
1177
1176
request -> state = STATE_HEADER_KEY ;
1178
1177
if (strcasecmp (request -> header_key , "Authorization" ) == 0 ) {
1179
1178
const char * prefix = "Basic " ;
1180
- request -> authenticated = memcmp (request -> header_value , prefix , strlen (prefix )) == 0 &&
1179
+ request -> authenticated = strncmp (request -> header_value , prefix , strlen (prefix )) == 0 &&
1181
1180
strcmp (_api_password , request -> header_value + strlen (prefix )) == 0 ;
1182
1181
} else if (strcasecmp (request -> header_key , "Host" ) == 0 ) {
1183
1182
request -> redirect = strcmp (request -> header_value , "circuitpython.local" ) == 0 ;
0 commit comments