Skip to content

refactor(graph): newtype EntityId guard for BFS dynamic SQL #1269

@bug-ops

Description

@bug-ops

Context

Pre-existing code in store.rs bfs_with_depth builds dynamic SQL by formatting integer IDs into a placeholder list.

Problem

The BFS traversal formats frontier IDs into SQL placeholders:

let placeholders = frontier.iter().enumerate().map(|(i, _)| format!("?{}", i + 1))...
let neighbour_sql = format!("... WHERE source_entity_id IN ({placeholders}) ...");

The values are i64 entity IDs from the database (not user input), so there is no current injection risk. However, the pattern is fragile: if the source of IDs ever changes to accept user-supplied values, this becomes vulnerable.

Suggested Fix

Wrap entity IDs in a newtype EntityId(i64) that can only be constructed from trusted internal sources. This enforces the invariant at compile time and prevents accidental misuse.

Source

SEC-CD-02 from Phase 5 security audit. This is a pre-existing pattern, not introduced by Phase 5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorCode refactoring without functional changessecuritySecurity-related issue

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions