-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Added redactDatabaseUrl() utility that masks username/password before logging #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import * as path from 'path' | |
| import express from 'express' | ||
| import cors from 'cors' | ||
| import { ZenStackMiddleware } from '@zenstackhq/server/express' | ||
| import { GeneratorConfig, ZModelConfig } from './zmodel-parser' | ||
| import { ZModelConfig } from './zmodel-parser' | ||
| import { getNodeModulesFolder, getPrismaVersion, getZenStackVersion } from './utils/version-utils' | ||
| import { blue, grey } from 'colors' | ||
| import semver from 'semver' | ||
|
|
@@ -46,6 +46,22 @@ function resolveSQLitePath(filePath: string, prismaSchemaDir: string): string { | |
| return path.join(prismaSchemaDir, filePath) | ||
| } | ||
|
|
||
| function redactDatabaseUrl(url: string): string { | ||
| try { | ||
| const parsedUrl = new URL(url) | ||
| if (parsedUrl.password) { | ||
| parsedUrl.password = '***' | ||
| } | ||
| if (parsedUrl.username) { | ||
| parsedUrl.username = '***' | ||
| } | ||
| return parsedUrl.toString() | ||
| } catch { | ||
| // If URL parsing fails, return the original (might be a file path for SQLite) | ||
| return url | ||
| } | ||
| } | ||
|
Comment on lines
+49
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid leaking credentials on parse failure (and in query params). If 🔒 Suggested fix function redactDatabaseUrl(url: string): string {
try {
const parsedUrl = new URL(url)
if (parsedUrl.password) {
parsedUrl.password = '***'
}
if (parsedUrl.username) {
parsedUrl.username = '***'
}
+ for (const key of ['password', 'passwd', 'pwd', 'user', 'username']) {
+ if (parsedUrl.searchParams.has(key)) {
+ parsedUrl.searchParams.set(key, '***')
+ }
+ }
return parsedUrl.toString()
} catch {
- // If URL parsing fails, return the original (might be a file path for SQLite)
- return url
+ // If URL parsing fails, redact userinfo/query credentials conservatively
+ return url
+ .replace(/\/\/([^:@\/?#]+):([^@\/?#]*)@/g, '//***:***@')
+ .replace(/\/\/([^:@\/?#]+)@/g, '//***@')
+ .replace(/([?&](?:password|passwd|pwd|user|username)=)[^&]*/gi, '$1***')
}
}🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
| * Create database adapter based on provider | ||
| */ | ||
|
|
@@ -79,7 +95,7 @@ function createAdapter(config: ZModelConfig, zmodelSchemaDir: string): any { | |
| case 'postgresql': { | ||
| try { | ||
| const { PrismaPg } = require('@prisma/adapter-pg') | ||
| console.log(grey(`Connecting to PostgreSQL database at: ${url}`)) | ||
| console.log(grey(`Connecting to PostgreSQL database at: ${redactDatabaseUrl(url)}`)) | ||
| return new PrismaPg({ connectionString: url }) | ||
| } catch (error) { | ||
| throw new CliError( | ||
|
|
@@ -90,7 +106,7 @@ function createAdapter(config: ZModelConfig, zmodelSchemaDir: string): any { | |
| case 'mysql': { | ||
| try { | ||
| const { PrismaMariaDB } = require('@prisma/adapter-mariadb') | ||
| console.log(grey(`Connecting to MySQL/MariaDB database at: ${url}`)) | ||
| console.log(grey(`Connecting to MySQL/MariaDB database at: ${redactDatabaseUrl(url)}`)) | ||
| return new PrismaMariaDB({ | ||
| url, | ||
| }) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
redactDatabaseUrlfunction is missing a JSDoc comment. All other helper functions in this file (e.g.,resolvePrismaSchemaDiron line 19,resolveSQLitePathon line 37,createAdapteron line 65) have JSDoc documentation. Please add a JSDoc comment describing the function's purpose, parameter, and return value to maintain consistency with the established pattern in this file.