Fix logic gaps: auth refresh tokens, provider routes, and Dockerfile#1
Fix logic gaps: auth refresh tokens, provider routes, and Dockerfile#1kai-lucky72 wants to merge 1 commit intorootfrom
Conversation
Review Summary by QodoFix database creation, route ordering, and add refresh tokens
WalkthroughsDescription• Add automatic database creation in migration script
- Checks if database exists before running schema
- Creates database if missing, handles connection errors gracefully
• Reorder provider routes for correct Express routing precedence
- Move /me/profile and /me/stats routes before /{id} route
- Prevents parameterized routes from intercepting user-specific endpoints
• Add refresh tokens table to database schema
- Stores hashed tokens with expiration and revocation status
- Includes indexes for efficient token lookups
• Fix Dockerfile entry point from server.js to index.js
• Add TypeScript migration script to package.json
Diagramflowchart LR
A["Migration Script"] -->|"Check/Create DB"| B["Database"]
C["Provider Routes"] -->|"Reorder /me/* before /{id}"| D["Correct Routing"]
E["Schema"] -->|"Add refresh_tokens table"| B
F["Dockerfile"] -->|"Fix entry point"| G["index.js"]
H["package.json"] -->|"Add db:migrate:ts"| I["TypeScript Support"]
File Changes1. src/database/migrate.ts
|
Code Review by Qodo
1. Unsanitized CREATE DATABASE name
|
| const dbName = config.database.name || 'hano_db'; | ||
| console.log(`Checking if database "${dbName}" exists...`); | ||
|
|
||
| const client = new Client({ | ||
| host: config.database.host, | ||
| port: config.database.port, | ||
| user: config.database.user, | ||
| password: config.database.password, | ||
| database: 'postgres', // Connect to default postgres DB first | ||
| }); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| const checkDb = await client.query(`SELECT 1 FROM pg_database WHERE datname = $1`, [dbName]); | ||
|
|
||
| if (checkDb.rows.length === 0) { | ||
| console.log(`Database "${dbName}" not found. Creating it...`); | ||
| // Cannot use parameterized query for CREATE DATABASE | ||
| await client.query(`CREATE DATABASE "${dbName}"`); | ||
| console.log(`✓ Database "${dbName}" created successfully.`); |
There was a problem hiding this comment.
1. Unsanitized create database name 🐞 Bug ⛨ Security
src/database/migrate.ts interpolates config.database.name (DB_NAME) directly into a CREATE DATABASE statement without identifier escaping/validation, so a name containing a double-quote can break the SQL and potentially append unintended DDL. This can cause migrations to fail or execute unexpected statements on the admin connection used for DB creation.
Agent Prompt
### Issue description
`src/database/migrate.ts` constructs `CREATE DATABASE "${dbName}"` using `dbName` from `process.env.DB_NAME` (via `config.database.name`) without validating it as a safe PostgreSQL identifier or escaping embedded quotes. This can break the statement and can enable DDL injection in the migration step.
### Issue Context
PostgreSQL DDL like `CREATE DATABASE` can’t be parameterized like normal value parameters, so the fix should be identifier validation/escaping (not `$1` parameters).
### Fix Focus Areas
- src/database/migrate.ts[17-37]
- src/config/config.ts[78-85]
### What to change
- Add strict validation for `dbName` (e.g., allow only `[A-Za-z_][A-Za-z0-9_]*`), and fail fast with a clear error if invalid.
- Alternatively (or additionally), implement proper identifier escaping (double any embedded `"` to `""`) and still validate length/charset.
- Keep the `SELECT 1 FROM pg_database WHERE datname = $1` check as-is (it’s already parameterized).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
bb075fd to
b36ac9e
Compare
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| ErrorProne | 1 high |
| Security | 1 critical |
| CodeStyle | 2 minor |
🟢 Metrics 7 complexity · 0 duplication
Metric Results Complexity 7 Duplication 0
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
i did some finalising about the backend logic and also completed some workflows for users