@@ -339,6 +339,11 @@ def start(self):
339339 """
340340)
341341
342+ flags ['allow-root' ]= (
343+ {'NotebookApp' : {'allow_root' : True }},
344+ "Allow the notebook to be run from root user."
345+ )
346+
342347# Add notebook manager flags
343348flags .update (boolean_flag ('script' , 'FileContentsManager.save_script' ,
344349 'DEPRECATED, IGNORED' ,
@@ -438,6 +443,10 @@ def _log_format_default(self):
438443 help = "Set the Access-Control-Allow-Credentials: true header"
439444 )
440445
446+ allow_root = Bool (False , config = True ,
447+ help = "Whether to allow the user to run the notebook as root."
448+ )
449+
441450 default_url = Unicode ('/tree' , config = True ,
442451 help = "The default URL to redirect to from `/`"
443452 )
@@ -1194,8 +1203,19 @@ def start(self):
11941203
11951204 This method takes no arguments so all configuration and initialization
11961205 must be done prior to calling this method."""
1206+
11971207 super (NotebookApp , self ).start ()
11981208
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+
11991219 info = self .log .info
12001220 for line in self .notebook_info ().split ("\n " ):
12011221 info (line )
0 commit comments