Dev.to Security πŸ” Cybersecurity πŸ‘ 1 πŸ“– 1 min read

userId! vs !userId

I never write the first one. But an agent could. userId! tells TypeScript "trust me, this exists." !userId checks whether it actually does. My habit is the check: !userId, then act. The agent's output used the assertio

Le Huy Hiep
Le Huy Hiep

Posted on Sep 7

userId! vs !userId

I never write the first one. But an agent could.

userId! tells TypeScript "trust me, this exists."
!userId checks whether it actually does.

My habit is the check: !userId, then act. The agent's output used
the assertion: userId!, assume it exists. On a route that allowed
guests, a user who was not logged in meant no user ID.

The code called the database with an empty ID. An empty ID means no
filter. The query returned every user's records.

No ID -> all the data.

My tests missed it because they only ran with logged-in users.
I caught it re-reading the code before a migration.

The fix: check for a missing ID and stop before the query runs.

When you review agent output, do not just check the logic. Check the
assumptions it typed as facts. The confident version is the dangerous
one.

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.