Skip to content

Commit dbc22ed

Browse files
committed
fix: sql query adjustment
1 parent 6b31a83 commit dbc22ed

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

cdk/text_generation/src/main.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,20 @@ def get_prompt_for_role(user_role):
152152
cur = connection.cursor()
153153
logger.info("Connected to RDS instance!")
154154

155-
# Validate the role
156-
valid_roles = ["public", "educator", "admin"]
157-
if user_role not in valid_roles:
158-
logger.error(f"Invalid user_role: {user_role}")
159-
return None
155+
# Define a list of allowed role values (whitelist)
156+
VALID_ROLES = ["public", "educator", "admin"] # add all valid column names
157+
if user_role not in VALID_ROLES:
158+
raise ValueError(f"Invalid role specified: {user_role}")
160159

161-
# Query to fetch the most recent prompt for the specified role
162-
query = f"""
163-
SELECT {user_role}
160+
query = """
161+
SELECT {}
164162
FROM prompts
165-
WHERE {user_role} IS NOT NULL
163+
WHERE {} IS NOT NULL
166164
ORDER BY time_created DESC NULLS LAST
167165
LIMIT 1;
168-
"""
166+
""".format(psycopg2.extensions.quote_ident(user_role, cur),
167+
psycopg2.extensions.quote_ident(user_role, cur))
168+
169169
cur.execute(query)
170170
result = cur.fetchone()
171171
logger.info(f"Query result for role {user_role}: {result}")

0 commit comments

Comments
 (0)