Kaynağa Gözat

PostgreSQL: detect FreshRSS tables by visibility rather than owner (#9535)

* PostgreSQL: the owner was never the story, so stop asking who owns the tables

For as long as the install check has existed, it asked pg_tables who owned
the tables and compared that against the database login. Nobody asked why.
The login is a gateway user, the tables belong to whatever role ran the
install, and whenever those two names diverged the check reported the schema
as missing. Visibility was the question all along.

tablesAreCorrect() now asks pg_class which ordinary and partitioned tables
are visible on the search_path. If PostgreSQL can see it, it counts. A table
parked in a schema outside the path does not hide a missing one, and a view
named like a table is not a table.

The new test builds a role, a visible schema, a hidden schema, and a view
with a suspicious name, then checks that each answer is the true one. It runs
against a real PostgreSQL 18 service in CI. Locally it skips unless
FRESHRSS_TEST_PGSQL_DSN is set.

* Pin the CI Postgres service to a digest, because somebody moved the tag

For the record: nobody announced it. The postgres:18-alpine tag sat in
tests.yml like a loaded gun pointed at every CI run, and zizmor was the
first thing to notice it had no fixed identity at all. Remember the
Denver Airport. Remember that tags are not promises.

This commit replaces the mutable tag with the exact multi-arch digest the
suite was already tested against:

  postgres:18-alpine@sha256:eaef288f54a90a909e49f0121d5d047a5e974e922699de4249625cf7cde34f36

This is not a version bump. Same image, same PostgreSQL 18, same Alpine
base. The only thing that changed is that the image can no longer be
swapped out from under us between Tuesday's green run and Thursday's
mystery failure.

Verification: all pre-commit PHP, test, JS, CSS and Markdown checks pass.
Local pedantic offline zizmor reports no findings. This resolves the
unpinned-service-image blocker on PR #9535.

* Rewrite test a bit

* Fix make test-all

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Corey Quinn 3 saat önce
ebeveyn
işleme
e7fe44bf86

+ 16 - 0
.github/workflows/tests.yml

@@ -20,6 +20,22 @@ jobs:
     # https://github.com/actions/runner-images
     runs-on: ubuntu-26.04
 
+    services:
+      postgres:
+        image: postgres:18-alpine@sha256:eaef288f54a90a909e49f0121d5d047a5e974e922699de4249625cf7cde34f36
+        env:
+          POSTGRES_HOST_AUTH_METHOD: trust
+        ports:
+          - 127.0.0.1:5432:5432
+        options: >-
+          --health-cmd pg_isready
+          --health-interval 5s
+          --health-timeout 5s
+          --health-retries 10
+
+    env:
+      FRESHRSS_TEST_PGSQL_DSN: pgsql:host=127.0.0.1;dbname=postgres;user=postgres
+
     steps:
     - name: Git checkout source code
       uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

+ 1 - 0
CHANGELOG.md

@@ -9,6 +9,7 @@ See also [the FreshRSS releases](https://github.com/FreshRSS/FreshRSS/releases).
 * Features
 	* User queries can define their own sort criterion (field and direction) [#8954](https://github.com/FreshRSS/FreshRSS/pull/8954)
 * Bug fixes
+	* PostgreSQL: detect FreshRSS tables by visibility rather than owner [#9535](https://github.com/FreshRSS/FreshRSS/pull/9535)
 	* Fix HTTP Retry-After cache when proxy is used [#9508](https://github.com/FreshRSS/FreshRSS/pull/9508)
 	* Fix 32-bit systems provide a string for SQL BIGINT [#9486](https://github.com/FreshRSS/FreshRSS/pull/9486)
 	* Keep JSON feed attachments as enclosures [#9510](https://github.com/FreshRSS/FreshRSS/pull/9510)

+ 1 - 0
CREDITS.md

@@ -76,6 +76,7 @@ People are sorted by name so please keep this order.
 * [Clemens Neubauer](https://github.com/cn-tools): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:cn-tools), [Web](https://cn-tools.eu/)
 * [colons](https://github.com/colons): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:colons), [Web](https://colons.co)
 * [Corentin Garcia](https://github.com/corenting): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:corenting), [Web](https://corenting.fr/)
+* [Corey Quinn](https://github.com/quinnypig): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:quinnypig)
 * [Craig Andrews](https://github.com/candrews): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:candrews), [Web](https://candrews.integralblue.com/)
 * [Creak](https://github.com/MightyCreak): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is%3Apr+author%3AMightyCreak)
 * [Cron](https://github.com/featurecreep-cron): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:featurecreep-cron)

+ 3 - 3
app/Models/DatabaseDAOPGSQL.php

@@ -12,11 +12,11 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
 
 	#[\Override]
 	public function tablesAreCorrect(): bool {
-		$db = FreshRSS_Context::systemConf()->db;
 		$sql = <<<'SQL'
-			SELECT tablename FROM pg_catalog.pg_tables where tableowner=:tableowner
+			SELECT relname AS tablename FROM pg_catalog.pg_class
+			WHERE relkind IN ('r', 'p') AND pg_catalog.pg_table_is_visible(oid)
 			SQL;
-		$res = $this->fetchAssoc($sql, [':tableowner' => $db['user']]);
+		$res = $this->fetchAssoc($sql);
 		if ($res == null) {
 			return false;
 		}

+ 1 - 1
composer.json

@@ -71,7 +71,7 @@
 		"phpcbf": "phpcbf . -p -s",
 		"phpstan": "phpstan analyse --memory-limit 512M .",
 		"phpstan-next": "phpstan analyse --memory-limit 512M -c phpstan-next.neon .",
-		"phpunit": "phpunit",
+		"phpunit": "phpunit --do-not-fail-on-skipped",
 		"compile-plurals": "php cli/compile.plurals.php --all",
 		"translations": "cli/manipulate.translation.php --action format && php cli/compile.plurals.php --all && cli/check.translation.php --generate-readme",
 		"test": [

+ 50 - 0
tests/app/Models/DatabaseDAOTest.php

@@ -4,6 +4,56 @@ declare(strict_types=1);
 use PHPUnit\Framework\Attributes\DataProvider;
 
 final class DatabaseDAOTest extends \PHPUnit\Framework\TestCase {
+	public function test_tablesAreCorrect_withPostgreSQL(): void {
+		$dsn = getenv('FRESHRSS_TEST_PGSQL_DSN');
+		if ($dsn === false || $dsn === '') {
+			self::markTestSkipped('FRESHRSS_TEST_PGSQL_DSN is not configured.');
+		}
+
+		// A gateway login need not match the PostgreSQL role that owns the tables:
+		// the DAO relies on grants to the visible tables, not on its own ownership.
+		$pdo = new Minz_PdoPgsql($dsn, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
+		$pdo->setPrefix('test_');
+		$pdo->beginTransaction();
+		try {
+			$pdo->exec(<<<'SQL'
+				CREATE ROLE freshrss_test_app NOLOGIN;
+				CREATE SCHEMA freshrss_test_visible;
+				CREATE SCHEMA freshrss_test_hidden;
+				SET LOCAL search_path TO freshrss_test_visible;
+				GRANT USAGE ON SCHEMA freshrss_test_visible, freshrss_test_hidden TO freshrss_test_app;
+				SQL);
+			$pdo->exec(<<<'SQL'
+				CREATE TABLE test_category (id integer);
+				CREATE TABLE test_feed (id integer);
+				CREATE TABLE test_entry (id integer);
+				CREATE TABLE test_entrytmp (id integer);
+				CREATE TABLE test_tag (id integer);
+				CREATE TABLE test_entrytag (id integer);
+				CREATE TABLE another_users_feed (id integer);
+				GRANT SELECT ON ALL TABLES IN SCHEMA freshrss_test_visible TO freshrss_test_app;
+				SET LOCAL ROLE freshrss_test_app;
+				SQL);
+			$dao = new FreshRSS_DatabaseDAOPGSQL('test', $pdo);
+			self::assertTrue($dao->tablesAreCorrect(), 'Visible tables may belong to another role.');
+
+			$pdo->exec(<<<'SQL'
+				RESET ROLE;
+				ALTER TABLE test_feed SET SCHEMA freshrss_test_hidden;
+				SET LOCAL ROLE freshrss_test_app;
+				SQL);
+			self::assertFalse($dao->tablesAreCorrect(), 'A table outside search_path must not hide a missing table.');
+
+			$pdo->exec(<<<'SQL'
+				RESET ROLE;
+				CREATE VIEW test_feed AS SELECT * FROM freshrss_test_hidden.test_feed;
+				SET LOCAL ROLE freshrss_test_app;
+				SQL);
+			self::assertFalse($dao->tablesAreCorrect(), 'A view must not count as a table.');
+		} finally {
+			$pdo->rollBack();
+		}
+	}
 
 	/** @return list<array{string,string,bool,bool}> */
 	public static function provideStrilikeCommon(): array {