Skip to content

[5.9] _subfolder() and _cfPrefix() pass null to rtrim() when env var resolves to empty string #189

@mtwalsh

Description

@mtwalsh

Description

Fs::_subfolder() and Fs::_cfPrefix() call Craft::parseEnv() and pass the result directly to rtrim(). When the referenced environment variable resolves to an empty string, App::parseEnv() returns null (by design — see App::parseEnv() lines 240–241), causing a PHP TypeError:

TypeError: rtrim(): Argument #1 ($string) must be of type string, null given

The issue is on line 509 of src/Fs.php:

private function _subfolder(): string
{
    if ($this->subfolder && ($subfolder = rtrim(Craft::parseEnv($this->subfolder), '/')) !== '') {
        return $subfolder . '/';
    }
    return '';
}

When subfolder is set to an env var reference (e.g. $MY_SUBFOLDER) in project config, $this->subfolder is the raw string $MY_SUBFOLDER (truthy), so it enters the if branch. Craft::parseEnv('$MY_SUBFOLDER') resolves the env var to "", which parseEnv() converts to null. Then rtrim(null, '/') throws.

The same pattern exists in _cfPrefix() on line 536.

A null-safe fix would be:

if ($this->subfolder && ($subfolder = rtrim(Craft::parseEnv($this->subfolder) ?? '', '/')) !== '') {

Steps to reproduce

  1. Configure an S3 filesystem with subfolder: $MY_SUBFOLDER in project config
  2. Set MY_SUBFOLDER="" in .env
  3. Load any page that generates an asset URL from that filesystem

Additional info

  • Craft version: 5.9.8
  • PHP version: 8.2.30
  • Database driver & version: N/A
  • Plugins & versions: craftcms/aws-s3 2.2.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions