@@ -339,6 +339,11 @@ def start(self):
339
339
"""
340
340
)
341
341
342
+ flags ['allow-root' ]= (
343
+ {'NotebookApp' : {'allow_root' : True }},
344
+ "Allow the notebook to be run from root user."
345
+ )
346
+
342
347
# Add notebook manager flags
343
348
flags .update (boolean_flag ('script' , 'FileContentsManager.save_script' ,
344
349
'DEPRECATED, IGNORED' ,
@@ -438,6 +443,10 @@ def _log_format_default(self):
438
443
help = "Set the Access-Control-Allow-Credentials: true header"
439
444
)
440
445
446
+ allow_root = Bool (False , config = True ,
447
+ help = "Whether to allow the user to run the notebook as root."
448
+ )
449
+
441
450
default_url = Unicode ('/tree' , config = True ,
442
451
help = "The default URL to redirect to from `/`"
443
452
)
@@ -1194,8 +1203,19 @@ def start(self):
1194
1203
1195
1204
This method takes no arguments so all configuration and initialization
1196
1205
must be done prior to calling this method."""
1206
+
1197
1207
super (NotebookApp , self ).start ()
1198
1208
1209
+ if not self .allow_root :
1210
+ # check if we are running as root, and abort if it's not allowed
1211
+ try :
1212
+ uid = os .geteuid ()
1213
+ except AttributeError :
1214
+ uid = - 1 # anything nonzero here, since we can't check UID assume non-root
1215
+ if uid == 0 :
1216
+ self .log .critical ("Running as root is not recommended. Use --allow-root to bypass." )
1217
+ self .exit (1 )
1218
+
1199
1219
info = self .log .info
1200
1220
for line in self .notebook_info ().split ("\n " ):
1201
1221
info (line )
0 commit comments