From 58ca5cc2a8196a3274de78685f6c62ffd4619557 Mon Sep 17 00:00:00 2001 From: jveh Date: Mon, 31 Jan 2022 15:08:48 +0100 Subject: [PATCH 1/2] add path_prefix_from_file option, adding security by obscurity. This option is helpful on multi-user systems. --- tensorboard/plugins/core/core_plugin.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tensorboard/plugins/core/core_plugin.py b/tensorboard/plugins/core/core_plugin.py index 39e673daa8..1cc39a7b48 100644 --- a/tensorboard/plugins/core/core_plugin.py +++ b/tensorboard/plugins/core/core_plugin.py @@ -533,6 +533,18 @@ def define_flags(self, parser): optional and has no effect. The path_prefix can be leveraged for path based routing of an ELB when the website base_url is not available e.g. "example.site.com/path/to/tensorboard/".\ +""", + ) + +parser.add_argument( + "--path_prefix_from_file", + metavar="PATH", + type=str, + default="", + help="""\ +Read the path_prefix from the specified file; avoids the path being +visible by ps or top - and, thus, is useful to improve security on +multi-user systems.\ """, ) @@ -684,6 +696,16 @@ def fix_flags(self, flags): elif flags.host is not None and flags.bind_all: raise FlagsError("Must not specify both --host and --bind_all.") + if flags.path_prefix_from_file and flags.path_prefix is None: + try: + with open(flags.path_prefix_from_file, 'r') as f: + flags.path_prefix = (f.read()).rstrip() + print("NOTE: using path_prefix=" + flags.path_prefix + " as read from file") + except IOError: + raise FlagsError("Cannot read prefix_from_file") + elif: + print("NOTE: Skipping prefix_from_file input due to overwrite by path_prefix") + flags.path_prefix = flags.path_prefix.rstrip("/") if flags.path_prefix and not flags.path_prefix.startswith("/"): raise FlagsError( From ddd9aec4465ce45b3886478515d94613dea3725c Mon Sep 17 00:00:00 2001 From: jveh Date: Wed, 16 Feb 2022 09:33:20 +0100 Subject: [PATCH 2/2] new option gen_path_prefix; this will generate a hash similar to jupyter notebooks --- tensorboard/plugins/core/core_plugin.py | 34 ++++++++++--------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/tensorboard/plugins/core/core_plugin.py b/tensorboard/plugins/core/core_plugin.py index 56fdf0bb80..e3c551867a 100644 --- a/tensorboard/plugins/core/core_plugin.py +++ b/tensorboard/plugins/core/core_plugin.py @@ -22,6 +22,8 @@ import mimetypes import posixpath import zipfile +import os +import binascii from werkzeug import utils from werkzeug import wrappers @@ -536,19 +538,15 @@ def define_flags(self, parser): """, ) -parser.add_argument( - "--path_prefix_from_file", - metavar="PATH", - type=str, - default="", + parser.add_argument( + "--gen_path_prefix", + action="store_true", help="""\ -Read the path_prefix from the specified file; avoids the path being -visible by ps or top - and, thus, is useful to improve security on -multi-user systems.\ +automatically generate path_prefix and print it to STDOUT; in case --path_prefix is also given on the command line, the auto-generated path_prefix will have priority and overwrite the value from the --path_prefix command line\ """, ) - - parser.add_argument( + + parser.add_argument( "--window_title", metavar="TEXT", type=str, @@ -723,17 +721,11 @@ def fix_flags(self, flags): "--detect_file_replacement=true" ) - if flags.path_prefix_from_file and flags.path_prefix is None: - try: - with open(flags.path_prefix_from_file, 'r') as f: - flags.path_prefix = (f.read()).rstrip() - print("NOTE: using path_prefix=" + flags.path_prefix + " as read from file") - except IOError: - raise FlagsError("Cannot read prefix_from_file") - elif: - print("NOTE: Skipping prefix_from_file input due to overwrite by path_prefix") - - flags.path_prefix = flags.path_prefix.rstrip("/") + if flags.gen_path_prefix: + flags.path_prefix = "/" + binascii.hexlify(os.urandom(32)).decode() + print("NOTE: using auto-generated path_prefix=" + flags.path_prefix) + + flags.path_prefix = flags.path_prefix.rstrip("/") if flags.path_prefix and not flags.path_prefix.startswith("/"): raise FlagsError( "Path prefix must start with slash, but got: %r."