Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"eslint.config.ts",
"vitest.config.ts",
"postcss.config.ts",
"tailwind.config.ts"
"tailwind.config.ts",
"src/utils/*.ts"
]
}
81 changes: 45 additions & 36 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig, Plugin, version as viteVersion } from "vite";
import type { PluginContext, ViteDevServer } from "vite";
import type { ViteDevServer } from "vite";
import type { PluginContext } from "rollup";
import react from "@vitejs/plugin-react";
import { simpleGit } from "simple-git";
import os from "os";
Expand Down Expand Up @@ -77,7 +78,7 @@ const getGitInfo = async (): Promise<{
// Fallback to environment variables or defaults
return {
sha: getEnv("VITE_GIT_SHA", "unknown"),
isClean: getEnv("VITE_GIT_CLEAN", "unknown"),
isClean: getEnv("VITE_GIT_CLEAN", "false") === "true",
branch: getEnv("VITE_GIT_BRANCH", "unknown"),
};
}
Expand Down Expand Up @@ -128,8 +129,8 @@ function buildInfoPlugin(): Plugin {
function dataValidationPlugin(dir: string = "src/data") {
return {
name: "data-validation",
apply: "build",
enforce: "pre",
apply: "build" as const,
enforce: "pre" as const,
async buildStart(this: PluginContext) {
const names = (await fs.readdir(dir)).filter((f) => f.endsWith(".json"));
const entries: FileEntry[] = [];
Expand Down Expand Up @@ -184,12 +185,52 @@ function dataValidationPlugin(dir: string = "src/data") {
};
}

// Create a new plugin for serving schema files
// Used to access the schema files with the dev server
function schemaServePlugin(): Plugin {
return {
name: "schema-serve",
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The plugin name uses single quotes while the rest of the file consistently uses double quotes for strings. Change to \"schema-serve\" for consistency with the codebase style.

Copilot uses AI. Check for mistakes.
configureServer(server: ViteDevServer) {
server.middlewares.use(
"/schema",
(
req: IncomingMessage,
res: ServerResponse,
next: (err?: unknown) => void,
): void => {
try {
const baseDir = resolve("src/schema");
const relPath = decodeURIComponent(
(req.url || "").replace(/^\/schema\/?/, ""),
);
if (!relPath) return next(); // no file requested, let Vite handle
const absPath = resolve(baseDir, relPath);
// prevent path traversal
if (!absPath.startsWith(baseDir)) return next();
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path traversal check using startsWith() is vulnerable on Windows due to case-insensitive paths. An attacker could bypass this check with paths like SRC/SCHEMA/../../etc/passwd. Use path.relative() and check if the result starts with .. for a more robust solution: if (path.relative(baseDir, absPath).startsWith('..')) return next();

Copilot uses AI. Check for mistakes.
// only serve JSON files
if (!absPath.endsWith(".json")) return next();
readFile(absPath, "utf8")
.then((json) => {
res.setHeader("Content-Type", "application/json");
res.end(json);
})
.catch(next);
} catch (err) {
next(err);
}
},
);
},
};
}

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
buildInfoPlugin(),
dataValidationPlugin("src/data"),
schemaServePlugin(),
viteStaticCopy({
// Copy the entire schema dir to /schema in the built output
targets: [{ src: "src/schema/**/*", dest: "schema" }],
Expand All @@ -199,36 +240,4 @@ export default defineConfig({
open: true,
port: 3000,
},
configureServer(server: ViteDevServer) {
// Serve any file under src/schema at /schema/* during dev
server.middlewares.use(
"/schema",
(
req: IncomingMessage,
res: ServerResponse,
next: (err?: unknown) => void,
): void => {
try {
const baseDir = resolve("src/schema");
const relPath = decodeURIComponent(
(req.url || "").replace(/^\/schema\/?/, ""),
);
if (!relPath) return next(); // no file requested, let Vite handle
const absPath = resolve(baseDir, relPath);
// prevent path traversal
if (!absPath.startsWith(baseDir)) return next();
// only serve JSON files
if (!absPath.endsWith(".json")) return next();
readFile(absPath, "utf8")
.then((json) => {
res.setHeader("Content-Type", "application/json");
res.end(json);
})
.catch(next);
} catch (err) {
next(err);
}
},
);
},
});