From d3b1897625c5b200724fb087cedd975fce07672f Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 21 Nov 2019 10:34:32 -0500 Subject: [PATCH] Allow adhoc enforcement in storagePath(). The ability to disable enforcement (i.e., the requested absolute path is within the storage path) globally will be removed after the Perl migration. The feature will still be needed occasionally so allow it in an adhoc fashion. --- src/storage/storage.c | 5 +++-- src/storage/storage.h | 12 +++++++++--- test/src/module/storage/posixTest.c | 7 +++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/storage/storage.c b/src/storage/storage.c index 324f31c31..f349d8af2 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -646,11 +646,12 @@ storageNewWrite(const Storage *this, const String *fileExp, StorageNewWriteParam Get the absolute path in the storage ***********************************************************************************************************************************/ String * -storagePath(const Storage *this, const String *pathExp) +storagePath(const Storage *this, const String *pathExp, StoragePathParam param) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(STORAGE, this); FUNCTION_TEST_PARAM(STRING, pathExp); + FUNCTION_TEST_PARAM(BOOL, param.noEnforce); FUNCTION_TEST_END(); ASSERT(this != NULL); @@ -670,7 +671,7 @@ storagePath(const Storage *this, const String *pathExp) // Make sure the base storage path is contained within the path expression if (!strEqZ(this->path, "/")) { - if (this->pathEnforce && (!strBeginsWith(pathExp, this->path) || + if (this->pathEnforce && !param.noEnforce && (!strBeginsWith(pathExp, this->path) || !(strSize(pathExp) == strSize(this->path) || *(strPtr(pathExp) + strSize(this->path)) == '/'))) { THROW_FMT(AssertError, "absolute path '%s' is not in base path '%s'", strPtr(pathExp), strPtr(this->path)); diff --git a/src/storage/storage.h b/src/storage/storage.h index 5d4d983cd..6976fa332 100644 --- a/src/storage/storage.h +++ b/src/storage/storage.h @@ -176,10 +176,16 @@ StorageWrite *storageNewWrite(const Storage *this, const String *fileExp, Storag /*********************************************************************************************************************************** storagePath ***********************************************************************************************************************************/ -#define storagePathP(this, pathExp) \ - storagePath(this, pathExp) +typedef struct StoragePathParam +{ + VAR_PARAM_HEADER; + bool noEnforce; +} StoragePathParam; -String *storagePath(const Storage *this, const String *pathExp); +#define storagePathP(this, pathExp, ...) \ + storagePath(this, pathExp, (StoragePathParam){VAR_PARAM_INIT, __VA_ARGS__}) + +String *storagePath(const Storage *this, const String *pathExp, StoragePathParam param); /*********************************************************************************************************************************** storagePathCreate diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c index 40314e314..257d802f6 100644 --- a/test/src/module/storage/posixTest.c +++ b/test/src/module/storage/posixTest.c @@ -534,11 +534,14 @@ testRun(void) storagePathP(storageTest, strNew("/path/toot")), AssertError, "absolute path '/path/toot' is not in base path '/path/to'"); - // Path enforcement disabled + // Path enforcement disabled globally storagePathEnforceSet(storageTest, false); - TEST_RESULT_STR(strPtr(storagePathP(storageTest, strNew("/bogus"))), "/bogus", "path enforce disabled"); + TEST_RESULT_STR(strPtr(storagePathP(storageTest, strNew("/bogus"))), "/bogus", "path enforce disabled globally"); storagePathEnforceSet(storageTest, true); + // Path enforcement disabled for a single call + TEST_RESULT_STR(strPtr(storagePathP(storageTest, strNew("/bogus"), .noEnforce = true)), "/bogus", "path enforce disabled"); + TEST_ERROR(storagePathP(storageTest, strNew(" not found in path expression '" BOGUS_STR)), AssertError,