Skip to content

fix(audit-log): prevent invalid SQL when projectId is undefined#5264

Open
adionit7 wants to merge 1 commit intoInfisical:mainfrom
adionit7:fix/audit-log-invalid-sql-5160
Open

fix(audit-log): prevent invalid SQL when projectId is undefined#5264
adionit7 wants to merge 1 commit intoInfisical:mainfrom
adionit7:fix/audit-log-invalid-sql-5160

Conversation

@adionit7
Copy link

Context

Fixes #5160

Problem:
When orgId is not provided, the audit log queue service attempts to fetch the project using projectDAL.findById() even when projectId is null or undefined, causing an invalid SQL query error.

Before:

  • The code would call projectDAL.findById(projectId as string) without checking if projectId exists
  • This resulted in SQL errors when projectId was null or undefined

After:

  • Added validation to ensure projectId exists before attempting to fetch the project
  • Throws a BadRequestError with proper logging if both orgId and projectId are undefined
  • Prevents invalid SQL queries from being executed

Screenshots

N/A - Backend fix only, no UI changes

Steps to verify the change

  1. Start the development server: docker compose -f docker-compose.dev.yml up --build --force-recreate
  2. Trigger an audit log creation scenario where orgId is missing and projectId is null/undefined
  3. Verify that:
    • A BadRequestError is thrown with a clear error message
    • Error is logged for debugging purposes
    • No invalid SQL queries are executed

Type

  • Fix
  • Feature
  • Improvement
  • Breaking
  • Docs
  • Chore

Checklist

  • Title follows the conventional commit format: type(scope): short description (scope is optional, e.g., fix: prevent crash on sync or fix(api): handle null response).
  • Tested locally
  • Updated docs (if needed) - No docs update needed for this bug fix
  • Read the contributing guide

@maidul98
Copy link
Collaborator

maidul98 commented Jan 25, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 25, 2026

Greptile Overview

Greptile Summary

Added defensive validation in the audit log queue worker to prevent invalid SQL queries when both orgId and projectId are undefined. The fix adds a check before calling projectDAL.findById() and throws a BadRequestError with proper logging if both identifiers are missing.

Key Changes:

  • Imported BadRequestError and logger modules
  • Added validation check for projectId before database call at line 52-60
  • Removed unsafe type assertion (as string) when calling projectDAL.findById()
  • Added error logging with job data context for debugging

Notes:

  • The service layer validation at backend/src/ee/services/audit-log/audit-log-service.ts:96-98 already prevents this scenario for most cases (except EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH), making this a defensive check for edge cases
  • Consider enhancing the error log with additional context like actor type and event type for easier debugging

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - it adds defensive validation to prevent invalid database queries
  • The fix correctly addresses the SQL error by adding proper validation before the database call. The logic is sound and aligns with existing service-layer validation. The only minor improvement would be enhancing the error logging with more contextual data for debugging purposes.
  • No files require special attention - the single changed file has a straightforward defensive check

Important Files Changed

Filename Overview
backend/src/ee/services/audit-log/audit-log-queue.ts Added validation to prevent invalid SQL when both orgId and projectId are undefined, includes error logging and throws BadRequestError

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Fixes Infisical#5160

When orgId is not provided, the audit log queue service attempts to
fetch the project using projectDAL.findById() even when projectId is
null or undefined, causing an invalid SQL query error.

This fix adds validation to ensure projectId exists before attempting
to fetch the project, and throws a BadRequestError with proper logging
if both orgId and projectId are undefined.

Changes:
- Add validation check for projectId before database query
- Add error logging for debugging edge cases
- Use BadRequestError consistent with codebase patterns
@adionit7 adionit7 force-pushed the fix/audit-log-invalid-sql-5160 branch from 85c6871 to 94442ba Compare January 25, 2026 12:13
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.

Invalid SQL inserting audit logs

2 participants