about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-09 05:59:08 +0000
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-10-09 09:18:16 -0500
commitddd5d2d8d2830b6233acfebc9a5d84292cf19d6f (patch)
tree9eb0b69fff72fcc9a43c2e3fe1d33fe1c23b25d1
parentdaa2a75142638dd46167931dc1d246cfa679dc60 (diff)
downloadgenenetwork3-ddd5d2d8d2830b6233acfebc9a5d84292cf19d6f.tar.gz
api: check dataset is public before serving sample data
sampledata.get_sampledata and lmdb_sample_data.get_sample_data read
LMDB directly by dataset/trait name with no check that the dataset is
public and non-confidential. Add dataset_is_public() to db_utils,
checked against ProbeSetFreeze/PublishFreeze/GenoFreeze, and use it in
both handlers to return 404 before touching LMDB when the dataset
isn't found or isn't public.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
-rw-r--r--gn3/api/lmdb_sample_data.py5
-rw-r--r--gn3/api/sampledata.py4
-rw-r--r--gn3/db_utils.py19
3 files changed, 28 insertions, 0 deletions
diff --git a/gn3/api/lmdb_sample_data.py b/gn3/api/lmdb_sample_data.py
index eaa71c2..0f08488 100644
--- a/gn3/api/lmdb_sample_data.py
+++ b/gn3/api/lmdb_sample_data.py
@@ -5,6 +5,8 @@ from pathlib import Path
 import lmdb
 from flask import Blueprint, current_app, jsonify
 
+from gn3.db_utils import database_connection, dataset_is_public
+
 lmdb_sample_data = Blueprint("lmdb_sample_data", __name__)
 
 
@@ -19,6 +21,9 @@ def get_sample_data(dataset: str, trait_id: int):
     Returns:
         JSON object mapping sample IDs to their values
     """
+    with database_connection(current_app.config["SQL_URI"]) as conn, conn.cursor() as cursor:
+        if not dataset_is_public(cursor, dataset):
+            return jsonify(error="Dataset not found or not public"), 404
     checksum = hashlib.md5(
         f"{dataset}-{trait_id}".encode()
     ).hexdigest()
diff --git a/gn3/api/sampledata.py b/gn3/api/sampledata.py
index 5b09b52..b529fca 100644
--- a/gn3/api/sampledata.py
+++ b/gn3/api/sampledata.py
@@ -5,6 +5,7 @@ from flask import jsonify
 from flask import current_app
 
 from gn3.db.matrix import get_current_matrix
+from gn3.db_utils import database_connection, dataset_is_public
 
 
 sampledata = Blueprint("sampledata", __name__)
@@ -13,6 +14,9 @@ sampledata = Blueprint("sampledata", __name__)
 @sampledata.route("/dataset/<dataset_name>/trait/<trait_name>", methods=["GET"])
 def get_sampledata(dataset_name, trait_name):
     """Fetch a trait's sampledata as a matrix."""
+    with database_connection(current_app.config["SQL_URI"]) as conn, conn.cursor() as cursor:
+        if not dataset_is_public(cursor, dataset_name):
+            return jsonify(error="Dataset not found or not public"), 404
     return jsonify(
         get_current_matrix(
             os.path.join(
diff --git a/gn3/db_utils.py b/gn3/db_utils.py
index e1816b0..e5a7829 100644
--- a/gn3/db_utils.py
+++ b/gn3/db_utils.py
@@ -73,6 +73,25 @@ def parse_db_url(sql_uri: str) -> dict:
     }
 
 
+def dataset_is_public(cursor, dataset_name: str) -> bool:
+    """Check whether a dataset is public and not confidential.
+
+    `dataset_name` is looked up against every Freeze table (ProbeSetFreeze,
+    PublishFreeze, GenoFreeze) since callers may not know which kind of
+    dataset they were given. An unrecognised name returns False, so callers
+    fail closed rather than serving data for a dataset this check could not
+    find."""
+    cursor.execute(
+        "SELECT 1 FROM ProbeSetFreeze "
+        "WHERE Name = %s AND public > 0 AND confidentiality < 1 "
+        "UNION SELECT 1 FROM PublishFreeze "
+        "WHERE Name = %s AND public > 0 AND confidentiality < 1 "
+        "UNION SELECT 1 FROM GenoFreeze "
+        "WHERE Name = %s AND public > 0 AND confidentiality < 1",
+        (dataset_name, dataset_name, dataset_name))
+    return cursor.fetchone() is not None
+
+
 @contextlib.contextmanager
 def xapian_database(path):
     """Open xapian database read-only."""