T09 · Insecure Skill Coding Practices
- Location
references/testing-migrations.md:8- Finding
Test cleanup can destructively target a non-test database
- Content
View full analysis
{ // Use a separate test database process.env.DATABASE_URL = process.env.TEST_DATABASE_URL execSync('npx prisma migrate deploy', { stdio: 'inherit' }) }) afterEach(async () => { // Clean tables in dependency order const tableNames = ['post', 'profile', 'user'] for (const table of tableNames) { await prisma.$executeRawUnsafe(`DELETE FROM "${table}"`) } }) ``` ### Technical Analysis The `PrismaClient` instance is created before `DATABASE_URL` is replaced with `TEST_DATABASE_URL`. This ordering does not reliably guarantee that the client performing cleanup is configured for the intended test database. The example also fails to verify that `TEST_DATABASE_URL`: - Is defined. - Is distinct from the production or development database URL. - Refers to an explicitly approved test host and database. - Uses credentials restricted to a disposable test schema. The cleanup operation uses `$executeRawUnsafe` to issue unrestricted table-wide `DELETE` statements. Although the table names are currently hardcoded and therefore do not directly introduce SQL injection, the unsafe API is unnecessary and makes future changes more likely to introduce injection. More importantly, the statements have no `WHERE` clause and will delete all records from the selected tables. ### Attack Path 1. A developer or CI worker starts the test suite with a production or shared database configured in `DATABASE_URL`. 2. `new PrismaClient()` is evaluated before the setup hook changes the environment variable. 3. `TEST_DATABASE_URL` is absent, incorrect, equal to the production URL, or ...[truncated 1252 chars]- Remediation
View remediation
{ await prisma.$transaction([ prisma.post.deleteMany(), prisma.profile.deleteMany(), prisma.user.deleteMany(), ]) }) ``` 5. Add a fail-closed CI guard that aborts if production indicators are present, such as a production hostname, cloud project identifier, or production database name. 6. Restrict the test database account to the isolated test database and ensure backups and recovery procedures exist for shared environments. ]]>
