Skip to content
File upload Write-up Intermediate

Bypassing file upload filter by source code review in Bolt CMS

The file upload extension validation in Bolt CMS could be bypassed because the filename was normalised after validation, allowing an attacker to upload a PHP file. Chained with CSRF and XSS, this led to remote code execution.

Sivanesh Ashok Follow

Published on blog.stazot.com

I discovered multiple vulnerabilities in an open-source PHP application, Bolt CMS. Chaining them led to a single-click RCE.

Sivanesh Ashok (@sivaneshashok), on blog.stazot.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

Bolt CMS is an open-source PHP application. In version 3.7.0 I found several vulnerabilities that, chained together, gave a single-click remote code execution. This article covers only the file upload bypass that turned the chain into RCE; the full advisory and the exploit are linked at the end.

The feature in question is the admin file upload. Bolt CMS lets an administrator upload files into several folders, and the application validates the extension of every uploaded file before it is stored. The interesting question was whether that validation could be defeated, because an admin upload of a PHP file would mean code execution.

Setup

I already had CSRF and XSS vulnerabilities in the application. Chained as an unauthenticated attacker, they let me perform actions in the context of the Bolt CMS administrator. That gave me the ability to send upload requests as the admin, which is the precondition for everything below.

The tools used for the source review were XDebug and VS Code, attached to the running application so I could step through the code while a request was being processed.

Finding it

Applications commonly allow admins to upload any file, including PHP files. Bolt CMS did not: even as an admin I could not upload PHP files, and I could not upload files with alternate PHP extensions such as php4 or php5 either. So I went to the function that validates the uploaded file, hoping to find a flaw in its logic.

The function was validateFileExtension():

PHP
private function validateFileExtension($filename)
{
    // no UNIX-hidden files
    if ($filename[0] === '.') {
        return false;
    }
    // only whitelisted extensions
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $allowedExtensions = $this->getAllowedUploadExtensions();

    return $extension === '' || in_array(mb_strtolower($extension),
$allowedExtensions);
}

The first check rejects files whose name starts with a dot, so I could not upload a .htaccess file to add additional valid PHP extensions. The function then takes the string after the last dot as the extension and checks whether it is in the array of whitelisted extensions. If it is not, the upload stops. The only way through is a file with no extension at all, or one whose extension is on the whitelist.

I could upload shell.php. with a trailing dot, but the server does not consider that a valid PHP file and does not execute the code. The validation logic looked solid and I could not come up with a way around it, so I moved on to other issues in the same upload functionality.

There were multiple folders an admin could upload to, so path traversal was the next idea. With two directories A and B in the location where the admin was allowed to upload, a file named ./A/text.txt or ./B/text.txt uploaded successfully, but anything outside the current directory, such as ../text.txt, was not allowed. I wanted to see how that protection was implemented, so I started XDebug and VS Code and debugged the code while making a request.

A tip worth repeating: if you are auditing source code and not using XDebug, you probably should give it a try.

While stepping through an upload request I noticed that, after validating the extension, the server has to name the file properly, and it uses a function called rename() for that:

PHP
public function rename($path, $newPath)
{
    $path = $this->normalizePath($path);
    $newPath = $this->normalizePath($newPath);
    $this->assertPresent($path);
    $this->assertAbsent($newPath);

    $this->doRename($path, $newPath);
}

rename() calls another function, normalizePath(), which normalises the path. When the name ./A/../text.txt was passed into normalizePath(), it returned text.txt. The same happens if you pass ./text.txt, because:

Text
./A/../text.txt == ./text.txt == text.txt

This was interesting because the manipulation of the filename happened after the extension validation, and there was no validation after the manipulation. My goal was to upload a PHP file to get RCE, and this ordering gave me a way to do it.

Exploitation

Step 1: choose a filename that passes validation

I uploaded a file with the name:

Text
shell.php/.

This filename is first passed into validateFileExtension(), which considers it valid: there are no characters after the last dot, so the function thinks it is a file with no extension. As noted earlier, on its own this would not execute, because shell.php/. is not a valid PHP filename.

Step 2: let normalisation rewrite the name

The name is then passed into normalizePath(). According to the logic of that function, the last two characters of the filename, /., refer to the current directory:

Text
./A/. == ./A == A

So normalizePath() removes them, and the name becomes shell.php, which is exactly what was needed.

Step 3: chain with CSRF and XSS

I chained the CSRF and XSS vulnerabilities to upload the file named shell.php/. as the admin. It was stored as shell.php and gave me RCE.

Root cause

The extension check and the path normalisation disagree about what the filename is. validateFileExtension() uses pathinfo($filename, PATHINFO_EXTENSION) and treats shell.php/. as having no extension, which is allowed. normalizePath() later resolves the trailing /. as the current directory and strips it, producing shell.php. The validation runs before the normalisation and nothing re-validates the final name, so the file that lands on disk has an extension the whitelist was meant to reject.

The existing defences did not stop this: the dot-prefix check blocks .htaccess, the whitelist blocks php4, php5 and similar extensions, and the traversal protection blocks ../, but none of them applies to a name whose meaning changes after the check.

Impact

In this case the uploaded PHP file was executed, giving remote code execution on the server. In this case the attacker did not need admin credentials: the CSRF and XSS vulnerabilities let an unauthenticated attacker perform the upload in the context of the admin, so the whole chain is single-click. The upload itself requires the admin's browser to make the request, which is what the CSRF and XSS chain provides.

Fix

Two Bolt CMS security advisories are linked: GHSA-2q66-6cc3-6xm8 and GHSA-68q3-7wjp-7q3j. The report does not describe the code changes made in response.

Variants to test

  1. Trailing dot-segments in upload names. Send names such as shell.php/., shell.php/./ or shell.php/a/.. and check whether the stored filename ends in .php. A positive result is a file on disk whose final name has an extension the whitelist rejects.
  2. Validation before normalisation. Wherever an application validates a filename and then normalises or resolves it, compare the name the validator saw with the name that is written. A positive result is any input that is harmless to the validator and dangerous after normalisation.
  3. Alternate PHP extensions. Try php4, php5 and similar names with the same dot-segment trick. A positive result is any of them being stored and executed.
  4. A positive result is a stored .htaccess that changes how PHP files in that directory are handled.
  5. Traversal protection scope. Test names like ./A/text.txt and ../text.txt to map exactly which forms are accepted, then look for forms the normaliser resolves differently from the check. A positive result is a write outside the intended upload directory.

References

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

Bolt CMS is a program that runs a website. It is written in a language called PHP, and it is open source, which means anyone can download its code and read it. In version 3.7.0 I found several security problems. On their own they were serious, but when I put them together they let an attacker take over the server with a single click by the site's administrator. This article is about one of those problems: a way to sneak a dangerous file past the check that is supposed to stop it.

The feature involved is the file upload in the admin area. When you run a website, you often want to put files on it: a picture for a blog post, a document, a logo. Bolt CMS lets an administrator upload files into several folders. Before the file is saved, the program looks at its name and decides whether the type of file is allowed. That decision is the whole game here. If an administrator can upload a PHP file, and the server then runs it, the uploader can make the server do whatever they want. That is called remote code execution, or RCE: the attacker's code runs on someone else's machine.

So the question I asked was simple: can the check that decides which file types are allowed be fooled? The answer turned out to be yes, and the trick was to give the file a name that means one thing to the check and a different thing to the code that saves it.

Background you need first

A few ideas come up again and again below, so here they are from zero.

A server and a browser. Your browser asks a website for a page; the server is the computer that answers. When you upload a file, your browser sends the file's contents and its name to the server, and the server decides what to do with it.

PHP and PHP files. PHP is a programming language used to build websites. A file ending in .php is not just data: when the web server is asked for it, it runs the code inside and sends back the result. That is why uploading a .php file is dangerous. If an attacker can put one on the server and then visit it, their code runs with the server's permissions.

An extension. The extension is the part of a filename after the last dot. In photo.jpg, the extension is jpg. In shell.php, it is php. Programs often use the extension to guess what kind of file something is.

A whitelist. A whitelist is a list of things that are allowed, and everything not on the list is refused. The opposite, a blacklist, lists what is forbidden and allows the rest. Whitelists are usually safer, because you only have to think of the good cases.

Path traversal. Folders on a computer are arranged in a tree. Inside a folder there can be other folders, and .. means "the folder above this one". So a name like ../text.txt means "go up one folder, then take text.txt". If a program lets an uploaded file's name contain .., an attacker might write files outside the folder they were supposed to be limited to. That is path traversal.

Normalisation. Two different names can point at the same place. ./A/../text.txt and text.txt can mean the same file, because ./ means "this folder" and A/.. means "go into A, then back out". Normalisation is the process of rewriting a name into its simplest, canonical form. It is like tidying up a directions list: "go to the shop, come back, go to the shop" becomes "go to the shop".

CSRF. Cross-site request forgery. It is a trick where a malicious page makes your browser send a request to another site where you are already logged in, without you meaning to. Your browser automatically attaches your login, so the other site thinks the request came from you.

XSS. Cross-site scripting. It is a way to get your own code (usually JavaScript) to run inside someone else's web page, in their browser. Among other things, it can be used to make requests as that person.

A cookie. A small piece of data a website stores in your browser so it can recognise you on the next request. Login sessions usually rely on a cookie. If an attacker can make your browser send requests, your cookie goes along with them.

XDebug and VS Code. XDebug is an add-on for PHP that lets a programmer pause a running program and look at what it is doing, line by line. VS Code is a code editor that can talk to XDebug and show you that paused state. Together they are like a slow-motion replay of the server handling your request.

How I got to the upload

Before the upload trick, I had already found CSRF and XSS problems in the application. Chained together, they let someone who is not logged in at all make the administrator's browser perform actions. In practice, that meant I could send upload requests as the administrator, without knowing the administrator's password. That is the starting point for everything below: the upload feature only accepts requests from an admin, and the CSRF and XSS chain is how I got to be treated as one.

For the code reading I used XDebug and VS Code, attached to the running application, so I could step through the code while a request was being handled.

Looking at the check

Many applications simply let admins upload anything, including PHP files. Bolt CMS did not. Even as an admin I could not upload a PHP file, and I could not upload files with other PHP-ish extensions such as php4 or php5 either. So I went to the function that checks the uploaded file, hoping to find a mistake in its logic.

The function was called validateFileExtension():

PHP
private function validateFileExtension($filename)
{
    // no UNIX-hidden files
    if ($filename[0] === '.') {
        return false;
    }
    // only whitelisted extensions
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $allowedExtensions = $this->getAllowedUploadExtensions();

    return $extension === '' || in_array(mb_strtolower($extension),
$allowedExtensions);
}

Line by line, in plain words:

  1. private function validateFileExtension($filename) declares a function that takes one input, the filename, and returns whether it is acceptable. private just means it is only used inside this part of the program.
  2. if ($filename[0] === '.') looks at the very first character of the name. If it is a dot, the next line returns false, meaning "not allowed". The comment explains why: on Unix-like systems a file whose name starts with a dot is hidden from normal directory listings. The author did not want people uploading hidden files.
  3. $extension = pathinfo($filename, PATHINFO_EXTENSION); asks PHP's built-in pathinfo function for the extension: the text after the last dot in the name.
  4. $allowedExtensions = $this->getAllowedUploadExtensions(); fetches the list of extensions the site allows.
  5. return $extension === '' || in_array(...) is the decision. It says: allow the file if the extension is empty (no dot at all in the name), or if the lowercased extension is in the allowed list. mb_strtolower makes the comparison case-insensitive, so PHP and php are treated the same. in_array checks membership in the list.

The first check blocks files whose name starts with a dot, so I could not upload a .htaccess file. That matters because .htaccess is a small configuration file that a web server reads to change how it treats a folder; an attacker could use one to tell the server to run other extensions as PHP. The rest of the function takes the text after the last dot and checks it against the whitelist. If it is not there, the upload stops. The only ways through are a name with no extension at all, or a name whose extension is on the list.

I could upload shell.php. with a dot at the end, but the server does not treat that as a valid PHP file, so the code inside would not run. The check looked solid, and I could not think of a way around it, so I moved on to other parts of the same upload feature.

The next idea: folders

There were several folders an admin could upload to, so the next thing to try was path traversal. Suppose there are two folders, A and B, inside the place where uploads are allowed. A file named ./A/text.txt or ./B/text.txt uploaded fine, but anything pointing outside the current folder, such as ../text.txt, was refused. I wanted to see how that protection was built, so I started XDebug and VS Code and watched the code while sending a request.

A tip I would repeat to anyone reading source code: if you are auditing code and not using XDebug, give it a try. Watching the program run turns guesswork into observation.

While stepping through an upload request, I noticed that after the extension check the server has to give the file its proper name, and it uses a function called rename() for that:

PHP
public function rename($path, $newPath)
{
    $path = $this->normalizePath($path);
    $newPath = $this->normalizePath($newPath);
    $this->assertPresent($path);
    $this->assertAbsent($newPath);

    $this->doRename($path, $newPath);
}

Line by line:

  1. public function rename($path, $newPath) is a function that moves a file from an old location to a new one. public means other parts of the program can call it.
  2. $path = $this->normalizePath($path); tidies up the old name before using it.
  3. $newPath = $this->normalizePath($newPath); does the same for the new name. This is the line that matters for us: the name the file will actually get is rewritten here.
  4. $this->assertPresent($path); checks that the source file exists, and stops if it does not.
  5. $this->assertAbsent($newPath); checks that the destination does not already exist, so nothing is overwritten.
  6. $this->doRename($path, $newPath); performs the actual move.

rename() calls another function, normalizePath(), which normalises the path. When the name ./A/../text.txt was passed into normalizePath(), it returned text.txt. The same happens if you pass ./text.txt, because:

Text
./A/../text.txt == ./text.txt == text.txt

Read that as: go into A, come back out, and you are where you started, so the whole A/.. part cancels out. ./ means the current folder, so it can be dropped too. What is left is just text.txt.

Why was this interesting? Because the filename was being rewritten after the extension check, and nothing checked it again afterwards. The check saw one name; the file that got saved had another. My goal was to upload a PHP file and get code execution, and this ordering gave me a way to do it.

Exploitation

Step 1: choose a filename that passes the check

I uploaded a file with the name:

Text
shell.php/.

This name is first passed to validateFileExtension(), which accepts it. Look at the name: the last dot is the one at the very end, and there is nothing after it. So pathinfo reports an empty extension, and the function's rule "empty extension is allowed" lets it through. As noted earlier, on its own this would not run, because shell.php/. is not a valid PHP filename: the server would not treat it as a PHP script.

Step 2: let normalisation rewrite the name

The name is then passed to normalizePath(). In that function's logic, the last two characters, /., mean the current folder:

Text
./A/. == ./A == A

In words: A/. is the same place as A, because /. just says "this folder". So normalizePath() removes those two characters, and the name becomes shell.php. That is exactly what was needed: a name the extension check had already approved, which ends up on disk as a PHP file.

Step 3: chain with CSRF and XSS

I used the CSRF and XSS problems to make the administrator's browser upload the file named shell.php/.. The server stored it as shell.php, and because it was a PHP file in a place the web server serves, it gave me remote code execution.

Why it worked

The extension check and the path normalisation disagree about what the filename is. validateFileExtension() uses pathinfo($filename, PATHINFO_EXTENSION) and sees shell.php/. as having no extension, which is allowed. normalizePath() later treats the trailing /. as the current folder and strips it, leaving shell.php. The check runs before the normalisation, and nothing re-checks the final name, so the file that lands on disk has an extension the whitelist was meant to reject.

The existing defences did not stop this. The dot-prefix rule blocks .htaccess. The whitelist blocks php4, php5 and similar. The traversal protection blocks ../. But none of them applies to a name whose meaning changes after the check.

What an attacker could do

In this case the uploaded PHP file was executed, which means remote code execution on the server. In this case the attacker did not need admin credentials: the CSRF and XSS problems let an unauthenticated attacker perform the upload in the administrator's context, so the whole chain is single-click. The upload still needs the admin's browser to send the request, and that is exactly what the CSRF and XSS chain provides.

The fix

Two Bolt CMS security advisories are linked: GHSA-2q66-6cc3-6xm8 and GHSA-68q3-7wjp-7q3j. The report does not describe the code changes made in response.

Things to try on your own

If you want to practise this kind of thinking, here are variations worth testing. Each one is a question about whether a check and the code that acts on the name agree.

  1. Trailing dot-segments in upload names. Send names such as shell.php/., shell.php/./ or shell.php/a/.. and see whether the stored filename ends in .php. A positive result is a file on disk whose final name has an extension the whitelist rejects.
  2. Validation before normalisation. Wherever an application checks a filename and then normalises or resolves it, compare the name the checker saw with the name that is written. A positive result is any input that is harmless to the checker and dangerous after normalisation.
  3. Alternate PHP extensions. Try php4, php5 and similar names with the same dot-segment trick. A positive result is any of them being stored and executed.
  4. A positive result is a stored .htaccess that changes how PHP files in that folder are handled.
  5. Traversal protection scope. Test names like ./A/text.txt and ../text.txt to map exactly which forms are accepted, then look for forms the normaliser resolves differently from the check. A positive result is a write outside the intended upload folder.

References

Glossary

  • Bolt CMS: an open-source content management system written in PHP, used to build and run websites.
  • Open source: software whose code is published and can be read by anyone.
  • PHP: a programming language commonly used to build websites; files ending in .php are executed by the web server.
  • Server: the computer that answers your browser's requests and runs the website.
  • Browser: the program you use to visit websites.
  • Extension: the part of a filename after the last dot, such as jpg in photo.jpg.
  • Whitelist: a list of allowed things; anything not on the list is refused.
  • Blacklist: a list of forbidden things; anything not on the list is allowed.
  • Remote code execution (RCE): a vulnerability where an attacker's code runs on someone else's machine.
  • Path traversal: using .. in a filename to reach folders outside the intended one.
  • Normalisation: rewriting a name or path into its simplest, canonical form, so that different spellings of the same place become one.
  • CSRF (cross-site request forgery): a trick that makes your browser send a request to a site where you are logged in, without your intent.
  • XSS (cross-site scripting): getting attacker-controlled code to run inside someone else's web page in their browser.
  • Cookie: a small piece of data stored in your browser so a site can recognise you on later requests.
  • Session: the logged-in state a site keeps for you, usually tied to a cookie.
  • XDebug: an add-on for PHP that lets a programmer pause and inspect a running program.
  • VS Code: a code editor that can connect to XDebug and show the paused program.
  • Function: a named block of code that takes inputs and produces a result.
  • pathinfo: a PHP function that splits a filename into parts, including the extension.
  • in_array: a PHP function that checks whether a value is present in a list.
  • mb_strtolower: a PHP function that converts text to lowercase, so comparisons ignore case.
  • .htaccess: a configuration file a web server reads to change how it treats a folder.
  • Advisory: a public notice describing a security issue and often its fix.

More by Sivanesh Ashok

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Unrestricted file upload

The lesson behind this report.

An upload feature accepts a file that the server or other users' browsers then treat as code or as something trusted.