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

don't believe sftp when it tells us the mtime is less than we know it is #40105

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions apps/files_external/lib/Lib/Storage/SFTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
*/
namespace OCA\Files_External\Lib\Storage;

use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use Icewind\Streams\RetryWrapper;
use OCP\IRequest;
use OCP\Cache\CappedMemoryCache;
use phpseclib\Net\SFTP\Stream;

/**
Expand All @@ -56,6 +59,7 @@
* @var \phpseclib\Net\SFTP
*/
protected $client;
private CappedMemoryCache $knownMTimes;

/**
* @param string $host protocol://server:port
Expand Down Expand Up @@ -111,6 +115,8 @@

$this->root = '/' . ltrim($this->root, '/');
$this->root = rtrim($this->root, '/') . '/';

$this->knownMTimes = new CappedMemoryCache();
}

/**
Expand Down Expand Up @@ -234,7 +240,7 @@
try {
$keyPath = $this->hostKeysPath();
if ($keyPath && file_exists($keyPath)) {
$fp = fopen($keyPath, 'w');

Check failure on line 243 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedFile

apps/files_external/lib/Lib/Storage/SFTP.php:243:17: TaintedFile: Detected tainted file handling (see https://psalm.dev/255)
foreach ($keys as $host => $key) {
fwrite($fp, $host . '::' . $key . "\n");
}
Expand All @@ -255,7 +261,7 @@
if (file_exists($keyPath)) {
$hosts = [];
$keys = [];
$lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Check failure on line 264 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedFile

apps/files_external/lib/Lib/Storage/SFTP.php:264:19: TaintedFile: Detected tainted file handling (see https://psalm.dev/255)
if ($lines) {
foreach ($lines as $line) {
$hostKeyArray = explode("::", $line, 2);
Expand Down Expand Up @@ -368,6 +374,7 @@
* {@inheritdoc}
*/
public function fopen($path, $mode) {
$path = $this->cleanPath($path);
try {
$absPath = $this->absPath($path);
switch ($mode) {
Expand All @@ -384,7 +391,13 @@
case 'wb':
SFTPWriteStream::register();
$context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
$fh = fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
if ($fh) {
$fh = CallbackWrapper::wrap($fh, null, null, function() use ($path) {
$this->knownMTimes->set($path, time());
});
}
return $fh;
case 'a':
case 'ab':
case 'r+':
Expand Down Expand Up @@ -413,14 +426,13 @@
return false;
}
if (!$this->file_exists($path)) {
$this->getConnection()->put($this->absPath($path), '');
return $this->getConnection()->put($this->absPath($path), '');

Check notice

Code scanning / Psalm

InternalMethod Note

The method phpseclib\Net\SFTP::put is internal to phpseclib but called from OCA\Files_External\Lib\Storage\SFTP::touch
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -454,11 +466,17 @@
*/
public function stat($path) {
try {
$path = $this->cleanPath($path);
$stat = $this->getConnection()->stat($this->absPath($path));

$mtime = $stat ? $stat['mtime'] : -1;
$size = $stat ? $stat['size'] : 0;

// the mtime can't be less than when we last touched it
if ($knownMTime = $this->knownMTimes->get($path)) {
$mtime = max($mtime, $knownMTime);
}

return ['mtime' => $mtime, 'size' => $size, 'ctime' => -1];
} catch (\Exception $e) {
return false;
Expand All @@ -476,4 +494,16 @@
$url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
return $url;
}

public function hasUpdated($path, $time) {
$storageTime = $this->filemtime($path);
$updated = $storageTime > $time;
if ($updated) {
$req = \OC::$server->get(IRequest::class);
if ($req->getHeader('x-debug-updated')) {
header("x-debug-updated: '$path' updated $storageTime > $time", false);

Check failure on line 504 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHeader

apps/files_external/lib/Lib/Storage/SFTP.php:504:12: TaintedHeader: Detected tainted header (see https://psalm.dev/256)

Check failure on line 504 in apps/files_external/lib/Lib/Storage/SFTP.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHeader

apps/files_external/lib/Lib/Storage/SFTP.php:504:12: TaintedHeader: Detected tainted header (see https://psalm.dev/256)

Check failure

Code scanning / Psalm

TaintedHeader Error

Detected tainted header

Check failure

Code scanning / Psalm

TaintedHeader Error

Detected tainted header
}
}
return $updated;
}
}
Loading