Skip to content

add USE_DEMO_PATIENT env variable toggle and safe session handlers#212

Open
louisthaboogie wants to merge 2 commits into
mainfrom
feat/add-demo-patient-toggle
Open

add USE_DEMO_PATIENT env variable toggle and safe session handlers#212
louisthaboogie wants to merge 2 commits into
mainfrom
feat/add-demo-patient-toggle

Conversation

@louisthaboogie

Copy link
Copy Markdown
Collaborator

No description provided.

@louisthaboogie

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a USE_DEMO_PATIENT environment variable to toggle between a hardcoded demo patient and the authenticated user's identity. Feedback identifies a potential SQL injection vulnerability and logic redundancy in agent.py, suggesting that the agent should rely on the session's user ID. Additionally, it is recommended to explicitly handle missing session emails in app.py to prevent unintended fallbacks to the demo account.

Comment on lines +51 to +55
if os.getenv("USE_DEMO_PATIENT", "true").lower() in ["true", "on", "1"]:
patient_key = "john.doe@gmail.com"
else:
patient_key = callback_context.session.user_id
query = f"SELECT profile FROM patients WHERE _key='{patient_key}'"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

This block introduces two concerns:

  1. Redundancy: The USE_DEMO_PATIENT check is already performed in the application layer (app.py), which sets the session's user_id accordingly. The agent should ideally be agnostic of this configuration and rely solely on callback_context.session.user_id to identify the patient.
  2. Security: Constructing the SQL query with f-string interpolation poses a SQL injection risk. Even if the input is expected to be an email address from a trusted session, it is best practice to use parameterized queries if the tool supports them, or ensure the input is properly sanitized.
    patient_key = callback_context.session.user_id
    query = f"SELECT profile FROM patients WHERE _key='{patient_key}'"
References
  1. Avoid hardcoding user identifiers in database queries; use dynamic values from the session or callback context instead.

if os.getenv("USE_DEMO_PATIENT", "true").lower() in ["true", "on", "1"]:
user_email = "john.doe@gmail.com"
else:
user_email = session.get('email', "john.doe@gmail.com")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When USE_DEMO_PATIENT is set to false, falling back to the demo email (john.doe@gmail.com) if the session email is missing might be confusing and lead to unexpected behavior. It is safer to handle the missing email case explicitly to ensure the agent uses the correct user identity.

Suggested change
user_email = session.get('email', "john.doe@gmail.com")
user_email = session.get('email')
if not user_email:
return jsonify({"error": "User email not found in session"}), 400

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a 'Demo Mode' configuration via the USE_DEMO_PATIENT environment variable, allowing the health concierge agent to toggle between a hardcoded demo patient and the authenticated user's email. Documentation and example environment files have been updated to reflect this change. However, a high-severity security vulnerability was identified in backend/agent.py, where the SQL query is constructed using an f-string with a user identifier, making it susceptible to SQL injection.

patient_key = "john.doe@gmail.com"
else:
patient_key = callback_context.session.user_id
query = f"SELECT profile FROM patients WHERE _key='{patient_key}'"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

Constructing SQL queries using f-strings with user identifiers like patient_key is a security risk (SQL injection). Furthermore, avoid hardcoding or manually passing user identifiers in queries. Instead, use dynamic values from the session or callback context (e.g., callback_context.session.user_id) to ensure the application functions correctly and securely for different users.

References
  1. Avoid hardcoding user identifiers, such as email addresses, in database queries. Instead, use dynamic values from the session or callback context (e.g., callback_context.session.user_id) to ensure the application functions correctly for different users.

@louisthaboogie

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a demo mode configuration for the Bigtable AI Health Concierge agent, allowing users to toggle between a hardcoded demo patient and the authenticated user's email via the USE_DEMO_PATIENT environment variable. Key changes include updating the documentation, environment examples, and modifying the backend logic in agent.py and app.py to support dynamic user identification and SQL query escaping. I have no feedback to provide as there were no review comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants