summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorNataliia Solomko <natalirs1985@gmail.com>2026-11-06 17:46:22 +0200
committerNataliia Solomko <natalirs1985@gmail.com>2026-11-06 18:08:47 +0200
commit5068f3f95cf1117cfe8b2d8df377f9a8b045cb61 (patch)
tree38a98b1417c62dc5e474f23d4cdae1ccf651f1f1 /python
parentc67221dfa4eac5e2f78eea09028a078a9c63cde6 (diff)
downloadvyos-1x-5068f3f95cf1117cfe8b2d8df377f9a8b045cb61.tar.gz
vyos-1x-5068f3f95cf1117cfe8b2d8df377f9a8b045cb61.zip
http-api: T9588: Keep config strings out of world-readable /tmp
The /config-file endpoint wrote config strings to /tmp/config.file, readable by any local user and never removed. A file pre-created at that path by another user made every later request fail. The string now goes to a private temp file that is always removed. The load and merge helpers, and the vyconf session, also remove their own /tmp copy when loading raises, e.g. on a syntax error.
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