security: remove hardcoded build-time DATABASE_URL and enforce environment-only configuration
This commit is contained in:
@@ -20,7 +20,6 @@ COPY . .
|
|||||||
# Environment variables must be present at build time for Next.js
|
# Environment variables must be present at build time for Next.js
|
||||||
# Coolify will provide these, but we can set defaults
|
# Coolify will provide these, but we can set defaults
|
||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
ENV DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy"
|
|
||||||
|
|
||||||
# Generate Prisma Client
|
# Generate Prisma Client
|
||||||
RUN npx prisma generate
|
RUN npx prisma generate
|
||||||
|
|||||||
@@ -4,21 +4,25 @@ import pg from "pg";
|
|||||||
|
|
||||||
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
||||||
|
|
||||||
const connectionString = process.env.DATABASE_URL || "postgresql://dummy:dummy@localhost:5432/dummy";
|
const connectionString = process.env.DATABASE_URL;
|
||||||
|
|
||||||
// Only throw if we are actually trying to use the DB in a real production/dev runtime
|
// During Next.js build phase (NEXT_PHASE), we allow the connection string to be missing
|
||||||
if (!process.env.DATABASE_URL && process.env.NODE_ENV === "production" && typeof window === "undefined" && !process.env.NEXT_PHASE) {
|
// to prevent the build from failing. In a real runtime, the Prisma client will
|
||||||
// However, Next.js build phase often needs this.
|
// throw an error if the connection fails, which is the expected behavior.
|
||||||
// We'll let it pass with the dummy string above if it's just for static generation.
|
if (!connectionString && process.env.NODE_ENV === "production" && !process.env.NEXT_PHASE) {
|
||||||
|
throw new Error("DATABASE_URL is not set in the environment.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getAdapter = () => {
|
||||||
|
if (!connectionString) return undefined;
|
||||||
const pool = new pg.Pool({ connectionString });
|
const pool = new pg.Pool({ connectionString });
|
||||||
const adapter = new PrismaPg(pool);
|
return new PrismaPg(pool);
|
||||||
|
};
|
||||||
|
|
||||||
export const prisma =
|
export const prisma =
|
||||||
globalForPrisma.prisma ||
|
globalForPrisma.prisma ||
|
||||||
new PrismaClient({
|
new PrismaClient({
|
||||||
adapter,
|
adapter: getAdapter(),
|
||||||
log: ["query"],
|
log: ["query"],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user