Issue Bug Closed

#106002PathUtility::stripPathSitePrefix generates invalid paths for symlinked document root

Kai Lochbaum 01/28/25, 15:05 updated 12/02/25, 14:27

Description

The static method PathUtility::stripPathSitePrefix removes the public path from the passed $path variable regardless whether the variable starts with the public path or not.

*Prerequisites*
The document root must contain a symbolic link somewhere in the hierarchy.
(!) The real path and the link path must have different lengths.

e.g.
Real path: /kunden/A/B/document_root
Environment Public path / Document Root via Apache: /www/A/B/document_root

*Steps to reproduce the problem*
In PHP call PathUtility::stripPathSitePrefix('/kunden/A/B/document_root/image.png');

Expected result: image.png
Actual result: ot/image.png

Our actual use case uses the GifBuilder to create grayscale images.
The GifBuilder creates an ImageInfo object that uses realpath values.
The public url for the image resource ends up with additional characters at the beginning (ot/typo3temp… instead of typo3temp…).

$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $fullFileName);
$imageResource = ImageResource::createFromImageInfo($imageInfo);
// calls PathUtility::stripPathSitePrefix($imageInfo->getRealPath())
$imageResource->getPublicUrl();

*Solution*
From my limited core knowledge ;), I would suggest two changes to the function:
1. Use realpath to resolve symbolic links
2. Check if the path actually starts with the public path before using substring.

A possible implementation could be

    /**
     * Strip first part of a path, equal to the length of public web path including trailing slash
     *
     * @internal
     */
    public static function stripPathSitePrefix(string $path): string
    {
        $realPath = realpath($path) ?: $path;
        $realPublicPath = realpath(Environment::getPublicPath()) ?: Environment::getPublicPath();
        if (str_starts_with($realPath, $realPublicPath)) {
            return substr($realPath, strlen($realPublicPath . '/'));
        }
        return $path;
    }

Reviews 6

Files 1

History 1419

  1. Kai Lochbaum changed subject PathUtility::stripPathSitePrefix generats invalid paths for symlinked document root PathUtility::stripPathSitePrefix generates invalid paths for symlinked document root <span data-processor="relativetime" data-value="2025-01-28T15:06:56+00:00" > 01/28/25, 15:06 </span>

  2. Garvin Hicking commented and changed status New Needs Feedback <span data-processor="relativetime" data-value="2025-01-28T20:59:10+00:00" > 01/28/25, 20:59 </span>

    Could you add whether you’re using composer or classic mode?

  3. Jo Hasenau commented <span data-processor="relativetime" data-value="2025-03-15T00:33:36+00:00" > 03/15/25, 00:33 </span>

    Can confirm this behaviour. In our case the path gets stripped too far:

    /typo3temp/assets/images/1.0037_csm_werkstoffbild_b_535_7fb89db9f0_5a80befb89b8166f2912674d974051e4.jpg
    becomes
    /emp/assets/images/1.0037_csm_werkstoffbild_b_535_7fb89db9f0_5a80befb89b8166f2912674d974051e4.jpg

    and the image will of course not be found, because it was generated in the correct path.

    Installation is in composer mode and deployed via deployer using symlinks for current and releases

  4. Jo Hasenau changed status Needs Feedback Accepted priority Should have Must have <span data-processor="relativetime" data-value="2025-03-15T00:33:53+00:00" > 03/15/25, 00:33 </span>

  5. Garvin Hicking commented <span data-processor="relativetime" data-value="2025-08-28T10:32:21+00:00" > 08/28/25, 10:32 </span>

    No, no activity/contributions yet to my knowledge.

  6. Claus Harup commented and changed attached gifbuilder.patch <span data-processor="relativetime" data-value="2025-09-02T09:49:22+00:00" > 09/02/25, 09:49 </span>

    I have fixed it using the attached patch :-)

  7. Garvin Hicking commented <span data-processor="relativetime" data-value="2025-09-02T17:31:28+00:00" > 09/02/25, 17:31 </span>

    That looks quite good, would you like to add this as a patch to be properly attributed and so we can review?

    (Else I could do that)

  8. Ralph Brugger commented <span data-processor="relativetime" data-value="2025-09-25T15:29:50+00:00" > 09/25/25, 15:29 </span>

    We were searching for hours through the code with this problem.

    GIFBUILDER creates image-urls like this:

    temp/assets/images/_830d1ab8316873169bf75578f01cb17c.png

    correct would be:

    typo3temp/assets/images/_830d1ab8316873169bf75578f01cb17c.png

    The absolute filename is:

    /srv/www/bst-update1/website/shared/public/typo3temp/assets/images/_830d1ab8316873169bf75578f01cb17c.png

    and because the PathUtility::stripPathSitePrefix removes “the base” /srv/www/bst-update1/website/shared/public/typo3 the result becomes:

    1) absolute filename:
    /srv/www/bst-update1/website/shared/public/typo3temp/assets/images/_830d1ab8316873169bf75578f01cb17c.png
    
    2) remove 
    /srv/www/bst-update1/website/shared/public/typo3
    
    3) becomes
    temp/assets/images/_830d1ab8316873169bf75578f01cb17c.png

    The above gifbuilder.patch works fine for us.

  9. Helmut Hummel commented <span data-processor="relativetime" data-value="2025-09-25T20:52:49+00:00" > 09/25/25, 20:52 </span>

    Kai Lochbaum wrote:

    > The GifBuilder creates an ImageInfo object that uses realpath values.

    This is the bug and what needs to be changed. Under no circumstances, the realpath can be used. We must always work with paths calculated and are provided in the Environment class

  10. Gerrit Code Review pushed patch set 1 for main to change 90805 status Accepted Under Review <span data-processor="relativetime" data-value="2025-09-25T21:33:55+00:00" > 09/25/25, 21:33 </span>

  11. Helmut Hummel commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2025-10-01T09:20:09+00:00" > 10/01/25, 09:20 </span>

    Applied in changeset commit:7d0c5e88d5f9d3ba2a2b15240b70c5c61add0a5d.