Skip to content

Commit

Permalink
improve code to collect the PostGIS version
Browse files Browse the repository at this point in the history
The SQL contained an unchecked string literal, which may in theory be
used to attack the database.
  • Loading branch information
lonvia committed Nov 16, 2023
1 parent ee556fd commit 6478409
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions nominatim/tools/collect_os_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import subprocess
import sys
from pathlib import Path
from typing import List, Optional, Tuple, Union, cast
from typing import List, Optional, Tuple, Union

import psutil
from psycopg2.extensions import make_dsn, parse_dsn

from nominatim.config import Configuration
from nominatim.db.connection import connect
from nominatim.typing import DictCursorResults
from nominatim.version import NOMINATIM_VERSION


Expand Down Expand Up @@ -107,15 +106,15 @@ def report_system_information(config: Configuration) -> None:
postgresql_ver: str = convert_version(conn.server_version_tuple())

with conn.cursor() as cur:
cur.execute(f"""
SELECT datname FROM pg_catalog.pg_database
WHERE datname='{parse_dsn(config.get_libpq_dsn())['dbname']}'""")
nominatim_db_exists = cast(Optional[DictCursorResults], cur.fetchall())
if nominatim_db_exists:
with connect(config.get_libpq_dsn()) as conn:
postgis_ver: str = convert_version(conn.postgis_version_tuple())
else:
postgis_ver = "Unable to connect to database"
num = cur.scalar("SELECT count(*) FROM pg_catalog.pg_database WHERE datname=%s",
(parse_dsn(config.get_libpq_dsn())['dbname'], ))
nominatim_db_exists = num == 1 if isinstance(num, int) else False

if nominatim_db_exists:
with connect(config.get_libpq_dsn()) as conn:
postgis_ver: str = convert_version(conn.postgis_version_tuple())
else:
postgis_ver = "Unable to connect to database"

postgresql_config: str = get_postgresql_config(int(float(postgresql_ver)))

Expand Down

0 comments on commit 6478409

Please sign in to comment.