Skip to content

Harden RecursiveDirectoryIterator when walking the filesystem.#530

Closed
mjdorma wants to merge 4 commits intopocoproject:developfrom
mjdorma:develop
Closed

Harden RecursiveDirectoryIterator when walking the filesystem.#530
mjdorma wants to merge 4 commits intopocoproject:developfrom
mjdorma:develop

Conversation

@mjdorma
Copy link
Copy Markdown

@mjdorma mjdorma commented Aug 28, 2014

In the implementation for the *Traverse strategies the next method performs an unguarded list directory. If the directory is not accessible an unrecoverable error is raised thus ruining the walk. This changeset adopts and adapts the error handling protocol as defined in Python's os.walk function where errors from listdir are ignored or are reported to an optional on error callback function.

Building a unittest to demonstrate this issue did not seem feasible as it would require the test program to contain privileges necessary to alter the access permissions of a directory then setuid to an alternative user.

To exercise this error condition I defined a folder structure as follows:

sudo ls -la test/
total 0
drwxr-xr-x   4 mjdorma  staff   136 29 Aug 03:10 .
drwxr-xr-x  62 mjdorma  staff  2108 29 Aug 04:48 ..
-rw-r--r--   1 mjdorma  staff     0 29 Aug 03:10 blah
drwxr-x---   3 root     wheel   102 29 Aug 03:10 hidden

I then added the following functions to the Foundation testsuite DirectoryIteratorsTest:

void DirectoryIteratorsTest::onerror(const std::string& path)
{
    std::cout << "Error on: " << path << std::endl;
}

void DirectoryIteratorsTest::testSimpleRecursiveDirectoryIteratorProtectedTestDirs()
{
    Path p("./test");
    SimpleRecursiveDirectoryIterator dirIterator(p);
    dirIterator.setOnError(TraverseErrorCallback<DirectoryIteratorsTest>(*this, &DirectoryIteratorsTest::onerror));
    SimpleRecursiveDirectoryIterator end;
    std::vector<std::string> result;
    std::string file;

    while (dirIterator != end)
    {
        file = dirIterator->path();
        ++dirIterator;
        result.push_back(file);
    }
}

When ran:

$ Foundation/testsuite/bin/Darwin/x86_64/testrunner DirectoryIteratorsTest

testDirectoryIterator:
testSortedDirectoryIterator:
testSimpleRecursiveDirectoryIterator: Error on: test/hidden

testSiblingsFirstRecursiveDirectoryIterator:

OK (4 tests)

Desired result achieved.

…rforms an unguarded list directory. If the directory is not accessible an unrecoverable error is raised thus ruining the walk. This changeset adopts and adapts the error handling protocol as defined in Python's os.walk function where errors from listdir are ignored or are reported to an optional on error callback function.
@mjdorma mjdorma closed this Aug 28, 2014
@mjdorma mjdorma reopened this Aug 28, 2014
@mjdorma
Copy link
Copy Markdown
Author

mjdorma commented Aug 29, 2014

For sure, I'll give it a go... Cheers!

@mjdorma
Copy link
Copy Markdown
Author

mjdorma commented Sep 2, 2014

Hi @aleks-f ,

In the above change set I have included two testcases to exercise the error callback code for both iteration strategies. The tests are exercised in completeness under UNIX. As you will see I have included a posix implementation of a local static function called setReadable.

Thanks for the great set of libraries.

Cheers

@obiltschnig
Copy link
Copy Markdown
Member

obiltschnig commented Sep 17, 2014

Instead of setOnError(), could you use a Poco::BasicEvent<const std::string> traverseError member? Will save a lot of code and be more consistent with the rest of POCO.

@aleks-f
Copy link
Copy Markdown
Member

aleks-f commented Jan 1, 2015

@mjdorma what is the status of this?

@aleks-f
Copy link
Copy Markdown
Member

aleks-f commented Jan 26, 2015

@mkrivos : can you look at this?

@aleks-f
Copy link
Copy Markdown
Member

aleks-f commented Feb 10, 2015

@mjdorma @mkrivos : anyone willing to look into this for 1.7?

@aleks-f
Copy link
Copy Markdown
Member

aleks-f commented Nov 15, 2017

done in #2001

@aleks-f aleks-f closed this Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants