Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php 8: strpos $haystack causes exception with integers in function markDirty #1352

Closed
brandonj35578 opened this issue Sep 17, 2021 · 2 comments

Comments

@brandonj35578
Copy link

Hello,

Until now in our usage of sabre/dav the error only occurred in the function 'markDirty' from class Sabre\DAV\Tree and only when using php 8(8.0.10). Under php 7(7.4.21) it works fine.
The use of strpos in markDirty Sabre\DAV\Tree causes errors, because $nodePath can be an integer. This is because php casts all array keys to integers, whenever possible. So, if the key '1' gets set for $this->cache, it will become $this->cache[1] instead of $this->cache['1']. So, when the part of the path is only a number this error occurs.
The function reads this key and uses it in strpos. Because php 8 became more strict for types, it causes an error. We are using sabre/dav 4.1.5, but the error occurs in 4.1.1 as well.
The line "$nodePath = (string)$nodePath;" fixes this error.

public function markDirty($path)
    {
        $path = trim($path, '/');
        foreach ($this->cache as $nodePath => $node) {
            $nodePath = (string)$nodePath; // PHP 8 override bugfix: strpos with integer $haystack causes error
            if ('' === $path || $nodePath == $path || 0 === strpos($nodePath, $path . '/')) {
                unset($this->cache[$nodePath]);
            }
        }
    }

Sabre/dav debug exception:

<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  <s:sabredav-version>4.1.5</s:sabredav-version>
  <s:exception>TypeError</s:exception>
  <s:message>strpos(): Argument #1 ($haystack) must be of type string, int given</s:message>
  <s:file>C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\Tree.php</s:file>
  <s:line>237</s:line>
  <s:code>0</s:code>
  <s:stacktrace>#0 C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\Tree.php(237): strpos(16, '16/85/Kalkulati...')
#1 C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\Server.php(1104): Sabre\DAV\Tree-&gt;markDirty('16/85/Kalkulati...')
#2 C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\CorePlugin.php(504): Sabre\DAV\Server-&gt;createFile('16/85/Kalkulati...', Resource id #181, NULL)
#3 C:\Projekte\np\library\ComposerPackages\vendor\sabre\event\lib\WildcardEmitterTrait.php(89): Sabre\DAV\CorePlugin-&gt;httpPut(Object(Sabre\HTTP\Request), Object(Sabre\HTTP\Response))
#4 C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\Server.php(472): Sabre\DAV\Server-&gt;emit('method:PUT', Array)
#5 C:\Projekte\np\library\ComposerPackages\vendor\sabre\dav\lib\DAV\Server.php(253): Sabre\DAV\Server-&gt;invokeMethod(Object(Sabre\HTTP\Request), Object(Sabre\HTTP\Response))
#6 C:\Projekte\np\projekt\epm\filemanager\dav\Server.php(74): Sabre\DAV\Server-&gt;start()
#7 {main}</s:stacktrace>
</d:error>

Regards

@brandonj35578
Copy link
Author

if ('' === $path || $nodePath == $path || 0 === strpos($nodePath, $path.'/')) {
The strpos parameter type issue has been fixed for php 8 to
if ('' === $path || $nodePath == $path || 0 === strpos((string) $nodePath, $path.'/')) {

@brandonj35578
Copy link
Author

Was fixed in #1385

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

No branches or pull requests

1 participant