-
Notifications
You must be signed in to change notification settings - Fork 0
Seed analytics gateway service #6
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: main
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| node_modules/ | ||
| dist/ | ||
| bun.lock | ||
| .DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "analytics-gateway", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "start": "bun run dist/server.js", | ||
| "dev": "bun run src/server.ts" | ||
| }, | ||
| "dependencies": { | ||
| "jsonwebtoken": "^9.0.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/jsonwebtoken": "^9.0.10", | ||
| "@types/node": "^24.8.1", | ||
| "bun-types": "^1.3.0", | ||
| "typescript": "^5.9.3" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const auditTrail = [ | ||
| "user=1 event=login source=10.0.0.2", | ||
| "user=2 event=export scope=customers", | ||
| "user=3 event=password_reset", | ||
| "user=1 event=download scope=full", | ||
| "system event=daily_summary" | ||
| ]; | ||
|
|
||
| export function searchAuditLogs(pattern: string) { | ||
| const regex = new RegExp(pattern); | ||
|
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. logic: Injection vulnerability: unescaped user input creates regex that can match unintended patterns (e.g., |
||
| return auditTrail.filter(entry => regex.test(entry)); | ||
|
Comment on lines
+10
to
+11
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. logic: ReDoS vulnerability: user-supplied |
||
| } | ||
|
Comment on lines
+9
to
+12
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. logic: No error handling for regex compilation. Invalid patterns will throw and crash the service. Wrap in try-catch and return empty array or error. Confidence: 4/5 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import jwt from "jsonwebtoken"; | ||
| import type { JwtPayload } from "jsonwebtoken"; | ||
| import { searchAuditLogs } from "./search"; | ||
| import { loadAccountById, leaderboardSnapshot } from "./store"; | ||
|
|
||
| const runtimeConfig = { | ||
| allowGuests: false, | ||
| maxBatchSize: 500 | ||
| }; | ||
|
|
||
| function jsonResponse(body: unknown, status = 200) { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { "Content-Type": "application/json" } | ||
| }); | ||
| } | ||
|
|
||
| const port = Number(process.env.PORT ?? 3000); | ||
|
|
||
| const server = Bun.serve({ | ||
| port, | ||
| fetch: async request => { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === "/api/profile") { | ||
| const accountId = Number(url.searchParams.get("userId") ?? "1"); | ||
| const account = loadAccountById(accountId); | ||
|
Comment on lines
+26
to
+27
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. logic: No validation on accountId or error handling if account doesn't exist - returns undefined account. Severity: 4/5 - Will cause client errors. |
||
| return jsonResponse({ account }); | ||
| } | ||
|
|
||
| if (url.pathname === "/api/leaderboard") { | ||
| const snapshot = leaderboardSnapshot().sort((a, b) => a.total - b.total); | ||
|
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. logic: Leaderboard sorted ascending (lowest to highest) but likely needs descending sort for a proper leaderboard (highest scores first). Severity: 5/5 - This will return the wrong top 3 players. |
||
| return jsonResponse({ top: snapshot.slice(0, 3) }); | ||
| } | ||
|
|
||
| if (url.pathname === "/api/config" && request.method === "POST") { | ||
| const overrides = await request.json(); | ||
| const merged = Object.assign(runtimeConfig, overrides); | ||
|
Comment on lines
+37
to
+38
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. logic: |
||
| return jsonResponse(merged); | ||
| } | ||
|
|
||
| if (url.pathname === "/api/auth/verify") { | ||
| const token = request.headers.get("authorization")?.split(" ")[1] ?? ""; | ||
| const payload = token ? (jwt.decode(token) as JwtPayload | null) : null; | ||
|
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. logic: |
||
| if (!payload) { | ||
| return jsonResponse({ error: "invalid token" }, 401); | ||
| } | ||
| if (payload.scope !== "admin") { | ||
| return jsonResponse({ error: "forbidden" }, 403); | ||
| } | ||
| return jsonResponse({ ok: true }); | ||
| } | ||
|
|
||
| if (url.pathname === "/api/audit/search") { | ||
| const pattern = url.searchParams.get("pattern") ?? ".*"; | ||
| const matches = searchAuditLogs(pattern); | ||
| return jsonResponse({ matches }); | ||
| } | ||
|
|
||
| return new Response("Not found", { status: 404 }); | ||
| } | ||
| }); | ||
|
|
||
| console.log(`analytics-gateway listening on ${server.hostname}:${server.port}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export type Account = { | ||
| id: number; | ||
| name: string; | ||
| lifetimeSpend: number; | ||
| }; | ||
|
|
||
| const accounts: Account[] = [ | ||
| { id: 1, name: "Alice", lifetimeSpend: 1200 }, | ||
| { id: 2, name: "Brent", lifetimeSpend: 980 }, | ||
| { id: 3, name: "Chloe", lifetimeSpend: 1550 } | ||
| ]; | ||
|
|
||
| const leaderboard = [ | ||
| { id: 1, total: 44 }, | ||
| { id: 2, total: 88 }, | ||
| { id: 3, total: 63 } | ||
| ]; | ||
|
Comment on lines
+13
to
+17
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. logic:
Comment on lines
+13
to
+17
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. style: |
||
|
|
||
| export async function loadAccountById(id: number): Promise<Account | undefined> { | ||
| return new Promise(resolve => { | ||
| setTimeout(() => resolve(accounts.find(account => account.id === id)), 8); | ||
| }); | ||
| } | ||
|
Comment on lines
+19
to
+23
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. style: Missing error rejection path in Promise. If |
||
|
|
||
| export function leaderboardSnapshot() { | ||
| return leaderboard.map(entry => ({ ...entry })); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "module": "CommonJS", | ||
|
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. logic:
Comment on lines
+3
to
+4
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. style: Bun runtime supports ES2021+ features natively; |
||
| "rootDir": "src", | ||
| "outDir": "dist", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "types": ["bun-types"], | ||
| "forceConsistentCasingInFileNames": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "include": ["src"] | ||
| } | ||
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.
style: Build step generates
dist/server.js, but iftscfails the start script will silently run stale or missing output. Consider chaining:"start": "bun run build && bun run dist/server.js". Is there a CI check that ensures the build succeeds before deployment, or could this run outdated compiled code in production?