Skip to content

check temp path before deleting files in __destruct to avoid Object Injection - #664

Open
mcdruid wants to merge 5 commits into
silverstripe:2from
mcdruid:check_temp_path
Open

check temp path before deleting files in __destruct to avoid Object Injection#664
mcdruid wants to merge 5 commits into
silverstripe:2from
mcdruid:check_temp_path

Conversation

@mcdruid

@mcdruid mcdruid commented Dec 3, 2024

Copy link
Copy Markdown

Description

See #663

Manual testing steps

You can test this manually via cli in various ways, or by doing something crude like adding this to the end of the main index.php

if (isset($_GET['payload'])) {
  unserialize(urldecode($_GET['payload']));
}

This simulates a PHP Object Injection vulnerability.

You can then pass the payload as a query param, and check whether the target file has been deleted.

e.g. here's a urlencoded payload to remove a test file called now_you_see_me.txt in the docroot:

curl -si http://silverstripe.ddev.site/?payload=O%3A39%3A%22SilverStripe%5CAssets%5CInterventionBackend%22%3A1%3A%7Bs%3A49%3A%22%00SilverStripe%5CAssets%5CInterventionBackend%00tempPath%22%3Bs%3A18%3A%22now_you_see_me.txt%22%3B%7D

Issues

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

@emteknetnz emteknetnz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a private const TEMP_FILE_PREFIX = 'interventionimage_'; and use that instead of the hardcoded string, and also update line 261 $path = tempnam($tempPath ?? '', 'interventionimage_'); to use the const

@mcdruid
mcdruid requested a review from emteknetnz February 18, 2025 20:59
Comment thread src/InterventionBackend.php Outdated
Comment thread src/InterventionBackend.php Outdated
mcdruid and others added 2 commits February 18, 2025 21:45
Co-authored-by: Steve Boyd <emteknetnz@gmail.com>
Co-authored-by: Steve Boyd <emteknetnz@gmail.com>
$tempPath = $this->getTempPath() ?? '';
if (str_starts_with(basename($tempPath), InterventionBackend::TEMP_FILE_PREFIX) && file_exists($tempPath)) {
unlink($tempPath);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}

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.

2 participants