1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
From: Florent Rougon <f.rougon@frougon.net>
Date: Tue, 21 Jan 2025 00:16:43 +0100
Subject: cppbind: check I/O rules when auto-constructing an SGPath from a
Nasal scalar
- Add static member function SGPath::NasalIORulesChecker as a
PermissionChecker (this is essentially checkIORules() moved from the
flightgear repository).
- Use it in the from_nasal_helper() that creates an SGPath instance from
a Nasal scalar.
---
simgear/misc/sg_path.cxx | 20 ++++++++++++++++++++
simgear/misc/sg_path.hxx | 7 +++++++
simgear/nasal/cppbind/detail/from_nasal_helper.cxx | 3 ++-
3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx
index f0706cd..5709256 100644
--- a/simgear/misc/sg_path.cxx
+++ b/simgear/misc/sg_path.cxx
@@ -288,6 +288,26 @@ void SGPath::set_cached(bool cached)
// * Access permissions for Nasal code *
// ***************************************************************************
+// Static member function
+SGPath::Permissions SGPath::NasalIORulesChecker(const SGPath& path)
+{
+ Permissions perm;
+
+ if (!path.isAbsolute()) {
+ // SGPath caches permissions, which breaks for relative paths if the
+ // current directory changes.
+ SG_LOG(SG_NASAL, SG_ALERT,
+ "SGPath::NasalIORulesChecker(): file operation on '" <<
+ path.utf8Str() << "': access denied (relative paths not "
+ "accepted; use realpath() to obtain an absolute path)");
+ }
+
+ perm.read = path.isAbsolute() && !path.validate(false).isNull();
+ perm.write = path.isAbsolute() && !path.validate(true).isNull();
+
+ return perm;
+}
+
// Static member function
void SGPath::clearListOfAllowedPaths(bool write)
{
diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx
index 84194b6..f98c004 100644
--- a/simgear/misc/sg_path.hxx
+++ b/simgear/misc/sg_path.hxx
@@ -137,6 +137,13 @@ public:
*/
SGPath validate(bool write) const;
+ /**
+ * Normal PermissionChecker for SGPath instances created from Nasal.
+ * @param path an SGPath instance
+ * @return read and write permissions conforming to validate()
+ */
+ static Permissions NasalIORulesChecker(const SGPath& path);
+
/**
* Append another piece to the existing path. Inserts a path
* separator between the existing component and the new component.
diff --git a/simgear/nasal/cppbind/detail/from_nasal_helper.cxx b/simgear/nasal/cppbind/detail/from_nasal_helper.cxx
index 160d69b..e404877 100644
--- a/simgear/nasal/cppbind/detail/from_nasal_helper.cxx
+++ b/simgear/nasal/cppbind/detail/from_nasal_helper.cxx
@@ -61,7 +61,8 @@ namespace nasal
SGPath from_nasal_helper(naContext c, naRef ref, const SGPath*)
{
naRef na_str = naStringValue(c, ref);
- return SGPath(std::string(naStr_data(na_str), naStr_len(na_str)));
+ return SGPath(std::string(naStr_data(na_str), naStr_len(na_str)),
+ &SGPath::NasalIORulesChecker);
}
//----------------------------------------------------------------------------
|