mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-11-09 11:10:08 +01:00
fix(security): make session-cookie hardening work under TLS termination
The passive scan flagged 3x MEDIUM 'PHPSESSID missing Secure' and an
admin-login cookie missing HttpOnly. Root cause was not the cookie code
but scheme detection: every call site read $_SERVER['HTTPS'] directly,
which is unset when TLS is terminated in front of PHP-FPM, so the Secure
flag was silently dropped.
Add RequestUtil::isSecure()/baseUrl() as the single scheme detector:
1. $_SERVER['HTTPS'] (direct TLS)
2. X-Forwarded-Proto — only from a loopback/private peer, so a remote
client cannot spoof it
3. SERVER_PORT == 443
Replace all 8 inline $_SERVER['HTTPS'] idioms (App, AdminAuth, 4x filepond
endpoints, validate-access, admin/acces, SystemController self-ping).
nginx: forward the scheme to php-fpm in all 4 fastcgi blocks
(fastcgi_param HTTPS $https + X-Forwarded-Proto $scheme) and uncomment
server_tokens off. Config validated with nginx -t (real config, alpine
container).
AdminAuth: logout() now clears the session cookie with the same
path/domain/secure/httponly/samesite it was set with, instead of the
legacy positional setcookie() that dropped SameSite.
Tests (+24, 340 total):
- RequestUtilTest: scheme detection incl. proxy trust boundaries
- SessionCookieTest: logout emits a hardened deletion cookie
- PeerTubeIntegrationTest: PeerTube keeps using its absolute instance_url
and is unaffected by scheme changes (Guzzle MockHandler)
- NginxConfigTest: pins HTTPS forwarding, HSTS includeSubDomains,
server_tokens off
PeerTube uses an absolute instance_url from the DB and never derives URLs
from the request, so the integration is unchanged — the regression tests
lock that invariant in.
This commit is contained in:
1 parent
e1f9cc9a9a
commit
554fc57455
17 files changed
+652
-29
No files matched your search
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* PeerTubeIntegrationTest — regression guard for the HTTPS/scheme changes.
|
||||
*
|
||||
* The session-cookie hardening (RequestUtil::isSecure) changed how the app
|
||||
* detects the request scheme. PeerTube must be unaffected: the service talks
|
||||
* to an *absolute* instance_url stored in peertube_settings, and must never
|
||||
* derive or rewrite that URL from $_SERVER.
|
||||
*
|
||||
* These tests pin the exact URLs PeerTubeService builds, so a future refactor
|
||||
* that starts routing PeerTube through request-derived base URLs fails loudly.
|
||||
*
|
||||
* Uses Guzzle's MockHandler so no network is touched.
|
||||
*/
|
||||
class PeerTubeIntegrationTest extends TestCase
|
||||
{
|
||||
private Database $db;
|
||||
private MockHandler $mock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->db = TestDatabase::getInstance();
|
||||
$this->db->getPDO()->exec('DELETE FROM peertube_settings');
|
||||
$this->db->getPDO()->exec('INSERT INTO peertube_settings (id) VALUES (1)');
|
||||
// TestDatabase is a shared singleton — reset the feature flag we toggle.
|
||||
$this->db->getPDO()
|
||||
->exec("DELETE FROM site_settings WHERE key = 'peertube_upload_enabled'");
|
||||
|
||||
$this->mock = new MockHandler();
|
||||
$stack = HandlerStack::create($this->mock);
|
||||
PeerTubeService::setHttpClient(new Client([
|
||||
'handler' => $stack,
|
||||
'http_errors' => false,
|
||||
'allow_redirects' => false,
|
||||
]));
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
PeerTubeService::setHttpClient(null);
|
||||
$this->db->getPDO()
|
||||
->exec("DELETE FROM site_settings WHERE key = 'peertube_upload_enabled'");
|
||||
}
|
||||
|
||||
/** Queue an OAuth client + token handshake, then return the request index. */
|
||||
private function queueAuth(string $instanceUrl): void
|
||||
{
|
||||
$this->mock->append(new Response(200, [], json_encode([
|
||||
'client_id' => 'cid',
|
||||
'client_secret' => 'csec',
|
||||
])));
|
||||
$this->mock->append(new Response(200, [], json_encode([
|
||||
'access_token' => 'tok',
|
||||
])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings are stored and read back verbatim — instance_url is absolute,
|
||||
* independent of any request context.
|
||||
*/
|
||||
public function testInstanceUrlStoredVerbatim(): void
|
||||
{
|
||||
PeerTubeService::updateSettings($this->db, [
|
||||
'instance_url' => 'https://video.erg.be/',
|
||||
'channel_name' => 'tfe@video.erg.be',
|
||||
'privacy' => 2,
|
||||
]);
|
||||
|
||||
$s = PeerTubeService::getSettings($this->db);
|
||||
$this->assertSame('https://video.erg.be', $s['instance_url'], 'trailing slash trimmed');
|
||||
$this->assertSame('tfe@video.erg.be', $s['channel_name']);
|
||||
$this->assertSame(2, $s['privacy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* probeAuth() must hit the absolute instance_url, not a request-derived
|
||||
* host. Changing HTTPS detection must not alter these URLs.
|
||||
*/
|
||||
public function testProbeAuthUsesAbsoluteInstanceUrl(): void
|
||||
{
|
||||
$s = [
|
||||
'instance_url' => 'https://video.erg.be',
|
||||
'username' => 'user',
|
||||
'password' => 'pass',
|
||||
];
|
||||
|
||||
$this->queueAuth('https://video.erg.be');
|
||||
|
||||
$result = PeerTubeService::probeAuth($s);
|
||||
$this->assertTrue($result['ok'], $result['error']);
|
||||
|
||||
// Last request in the handshake is the token exchange; it must target
|
||||
// the absolute configured instance URL.
|
||||
$this->assertSame(
|
||||
'https://video.erg.be/api/v1/users/token',
|
||||
(string) $this->mock->getLastRequest()->getUri()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An HTTP instance_url must be preserved as-is. If URL handling ever
|
||||
* started "upgrading" to https based on request scheme, this fails.
|
||||
*/
|
||||
public function testHttpInstanceUrlPreservedWhenConfigured(): void
|
||||
{
|
||||
PeerTubeService::updateSettings($this->db, [
|
||||
'instance_url' => 'http://localhost:9000',
|
||||
'channel_name' => 'chan',
|
||||
]);
|
||||
|
||||
$s = PeerTubeService::getSettings($this->db);
|
||||
$this->assertSame('http://localhost:9000', $s['instance_url']);
|
||||
}
|
||||
|
||||
/**
|
||||
* isEnabled() is a pure feature-flag read and must not depend on request
|
||||
* scheme or HTTPS detection.
|
||||
*/
|
||||
public function testFeatureFlagIndependentOfRequestScheme(): void
|
||||
{
|
||||
$this->assertFalse(PeerTubeService::isEnabled($this->db));
|
||||
|
||||
$this->db->getPDO()
|
||||
->prepare("INSERT OR REPLACE INTO site_settings (key, value) VALUES ('peertube_upload_enabled', '1')")
|
||||
->execute();
|
||||
|
||||
$this->assertTrue(PeerTubeService::isEnabled($this->db));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user