Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit 3f8f88a

Browse files
committed
filter imports in safe mode
1 parent f1616b6 commit 3f8f88a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

esp32/main.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,44 @@ STATIC uint8_t mp_task_heap[MP_TASK_HEAP_SIZE];
7676
extern uint32_t reset_cause;
7777
extern bool in_safe_mode;
7878

79+
static const char *import_blacklist[] = {
80+
"/lib/json",
81+
"/lib/os",
82+
"/lib/socket",
83+
"/lib/struct",
84+
"/lib/time",
85+
};
86+
87+
mp_import_stat_t
88+
mp_import_stat(const char *path) {
89+
if (in_safe_mode) {
90+
// be more strict in which modules we would like to load
91+
if (strncmp(path, "/lib/", 5) != 0) {
92+
return MP_IMPORT_STAT_NO_EXIST;
93+
}
94+
95+
/* check blacklist */
96+
int i;
97+
for (i=0; i<sizeof(import_blacklist)/sizeof(const char *); i++) {
98+
if (strcmp(path, import_blacklist[i]) == 0) {
99+
return MP_IMPORT_STAT_NO_EXIST;
100+
}
101+
}
102+
103+
const char *x = index(&path[5], '/');
104+
if (x == NULL) {
105+
// only allow directories
106+
mp_import_stat_t res = mp_vfs_import_stat(path);
107+
if (res != MP_IMPORT_STAT_DIR) {
108+
return MP_IMPORT_STAT_NO_EXIST;
109+
}
110+
return res;
111+
}
112+
}
113+
114+
return mp_vfs_import_stat(path);
115+
}
116+
79117
void mp_task(void *pvParameter) {
80118
volatile uint32_t sp = (uint32_t)get_sp();
81119
#if MICROPY_PY_THREAD

esp32/mpconfigport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@
159159
#define MICROPY_SDMMC_USE_DRIVER (1)
160160
#define MICROPY_SDMMC_SHOW_INFO (1)
161161

162-
// use vfs's functions for import stat and builtin open
163-
#define mp_import_stat mp_vfs_import_stat
162+
// use vfs's functions for builtin open
164163
#define mp_builtin_open mp_vfs_open
165164
#define mp_builtin_open_obj mp_vfs_open_obj
166165

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy