Skip to content

Commit 62ee30d

Browse files
committed
address review: move validator to codechecker_common.util
Per #4830 review feedback: is_valid_postgresql_db_name belongs with the other generic helpers in codechecker_common.util, not in the web server's routing module. The function and its tests are unchanged; only the import paths in product_server.py and test_request_routing.py are adjusted.
1 parent 4e19a75 commit 62ee30d

4 files changed

Lines changed: 26 additions & 33 deletions

File tree

codechecker_common/util.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,26 @@ def format_size(num: float, suffix: str = 'B') -> str:
251251
return f"{num:3.1f} {unit}{suffix}"
252252
num /= 1024.0
253253
return f"{num:.1f} Qi{suffix}"
254+
255+
256+
def is_valid_postgresql_db_name(db_name):
257+
"""
258+
Returns whether or not the given string is a safe PostgreSQL database
259+
name for CodeChecker to use.
260+
261+
CodeChecker quotes the database identifier when issuing CREATE DATABASE,
262+
so dashes, leading digits, and PostgreSQL reserved keywords are all
263+
allowed. However, characters that would break even a quoted
264+
identifier, or that are
265+
plainly dangerous in an SQL context, are rejected here so we fail fast
266+
with a clear error rather than producing broken SQL or an unusable
267+
product.
268+
"""
269+
if not db_name or not isinstance(db_name, str):
270+
return False
271+
272+
if len(db_name.encode('utf-8')) > 63:
273+
return False
274+
275+
forbidden = set('"\'\\;\x00\r\n\t ')
276+
return not any(c in forbidden for c in db_name)

web/server/codechecker_server/api/product_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from .. import permissions
3232
from ..database.config_db_model import IDENTIFIER, Product, ProductPermission
3333
from ..database.database import DBSession, SQLServer, conv, escape_like
34-
from ..routing import is_valid_product_endpoint, is_valid_postgresql_db_name
34+
from ..routing import is_valid_product_endpoint
35+
from codechecker_common.util import is_valid_postgresql_db_name
3536

3637
from .thrift_enum_helper import confidentiality_enum, \
3738
confidentiality_str

web/server/codechecker_server/routing.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,6 @@ def is_valid_product_endpoint(uripart):
6363
return True
6464

6565

66-
def is_valid_postgresql_db_name(db_name):
67-
"""
68-
Returns whether or not the given string is a safe PostgreSQL database
69-
name for CodeChecker to use.
70-
71-
CodeChecker quotes the database identifier when issuing CREATE DATABASE,
72-
so dashes, leading digits, and PostgreSQL reserved keywords are all
73-
allowed (e.g. "test-product", "1team", "user" are accepted). However,
74-
characters that would break even a quoted identifier, or that are
75-
plainly dangerous in an SQL context, are rejected here so we fail fast
76-
with a clear error rather than producing broken SQL or an unusable
77-
product.
78-
"""
79-
if not db_name or not isinstance(db_name, str):
80-
return False
81-
82-
# PostgreSQL identifiers (even quoted) cannot exceed 63 bytes by
83-
# default. Names longer than this are silently truncated by the
84-
# server, which would produce a product that cannot be reconnected
85-
# to under the name the user provided. Reject them outright.
86-
if len(db_name.encode('utf-8')) > 63:
87-
return False
88-
89-
# Forbidden characters: anything that would prematurely terminate
90-
# the quoted identifier, embed a statement separator, or corrupt the
91-
# connection string. Whitespace is also rejected because a name with
92-
# spaces is almost certainly a typo rather than an intent.
93-
forbidden = set('"\'\\;\x00\r\n\t ')
94-
return not any(c in forbidden for c in db_name)
95-
96-
9766
def is_supported_version(version):
9867
"""
9968
Returns whether or not the given version tag is supported by the current

web/server/tests/unit/test_request_routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from codechecker_server.routing import split_client_GET_request
1515
from codechecker_server.routing import split_client_POST_request
16-
from codechecker_server.routing import is_valid_postgresql_db_name
16+
from codechecker_common.util import is_valid_postgresql_db_name
1717

1818

1919
def get(path, host="http://localhost:8001/"):

0 commit comments

Comments
 (0)