summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-11-08 14:09:30 +0100
committerGitHub <noreply@github.com>2026-11-08 14:09:30 +0100
commit214892611945f1f7727bda1c9419cc37f40c9f94 (patch)
treeed0a7dd377145be4f8c6f3144b4fbf66c9c9453d /python
parentc66d680071ee31e1ea3aa8f99a60f7ad24e3096f (diff)
parent5068f3f95cf1117cfe8b2d8df377f9a8b045cb61 (diff)
downloadvyos-1x-214892611945f1f7727bda1c9419cc37f40c9f94.tar.gz
vyos-1x-214892611945f1f7727bda1c9419cc37f40c9f94.zip
Merge pull request #5634 from natali-rs1985/T9588
http-api: T9588: Keep config strings out of world-readable /tmp
Diffstat (limited to 'python')
-rw-r--r--python/vyos/vyconf_session.py77
1 files changed, 42 insertions, 35 deletions
diff --git a/python/vyos/vyconf_session.py b/python/vyos/vyconf_session.py
index 932c87964..0c9cea8f1 100644
--- a/python/vyos/vyconf_session.py
+++ b/python/vyos/vyconf_session.py
@@ -251,25 +251,27 @@ class VyconfSession:
from vyos.remote import get_config_file
file_path = tempfile.NamedTemporaryFile(delete=False).name
- err = get_config_file(file_name, file_path)
- if err:
- os.remove(file_path)
- return str(err), Errnum.INVALID_VALUE
- if not cached:
- if migrate:
- config_migrate = ConfigMigrate(file_path)
- try:
- config_migrate.run()
- except ConfigMigrateError as e:
- os.remove(file_path)
- return repr(e), 1
-
- out = vyconf_client.send_request(
- 'load', token=self.__token, location=file_path, cached=cached
- )
-
- if not cached:
- os.remove(file_path)
+ # Remove the temp copy in finally so an exception can't leave the
+ # config behind. Cached loads too: vyconfd reads the whole file into
+ # memory before it replies and never opens it again.
+ try:
+ err = get_config_file(file_name, file_path)
+ if err:
+ return str(err), Errnum.INVALID_VALUE
+ if not cached:
+ if migrate:
+ config_migrate = ConfigMigrate(file_path)
+ try:
+ config_migrate.run()
+ except ConfigMigrateError as e:
+ return repr(e), 1
+
+ out = vyconf_client.send_request(
+ 'load', token=self.__token, location=file_path, cached=cached
+ )
+ finally:
+ if os.path.exists(file_path):
+ os.remove(file_path)
return self.output(out), out.status
@@ -285,23 +287,28 @@ class VyconfSession:
from vyos.remote import get_config_file
file_path = tempfile.NamedTemporaryFile(delete=False).name
- err = get_config_file(file_name, file_path)
- if err:
- os.remove(file_path)
- return str(err), Errnum.INVALID_VALUE
- if migrate:
- config_migrate = ConfigMigrate(file_path)
- try:
- config_migrate.run()
- except ConfigMigrateError as e:
- os.remove(file_path)
- return repr(e), 1
-
- out = vyconf_client.send_request(
- 'merge', token=self.__token, location=file_path, destructive=destructive
- )
+ # Remove the temp copy in finally so an exception can't leave the
+ # config behind
+ try:
+ err = get_config_file(file_name, file_path)
+ if err:
+ return str(err), Errnum.INVALID_VALUE
+ if migrate:
+ config_migrate = ConfigMigrate(file_path)
+ try:
+ config_migrate.run()
+ except ConfigMigrateError as e:
+ return repr(e), 1
- os.remove(file_path)
+ out = vyconf_client.send_request(
+ 'merge',
+ token=self.__token,
+ location=file_path,
+ destructive=destructive,
+ )
+ finally:
+ if os.path.exists(file_path):
+ os.remove(file_path)
return self.output(out), out.status