Skip to content

Commit e1a73b9

Browse files
committed
Validate domain output
1 parent d6c4148 commit e1a73b9

10 files changed

Lines changed: 194 additions & 204 deletions

File tree

src/config/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { z } from "zod";
22

33
// Environment schema
4-
const EnvironmentSchema = z.enum(["development", "staging", "production", "test"]);
4+
const EnvironmentSchema = z.enum([
5+
"development",
6+
"staging",
7+
"production",
8+
"test",
9+
]);
510

611
// Configuration schema
712
const ConfigSchema = z.object({
@@ -23,11 +28,15 @@ const ConfigSchema = z.object({
2328
// JWT
2429
jwt: z.object({
2530
access: z.object({
26-
secret: z.string().min(32, "JWT_ACCESS_SECRET must be at least 32 characters"),
31+
secret: z
32+
.string()
33+
.min(32, "JWT_ACCESS_SECRET must be at least 32 characters"),
2734
expiresIn: z.string().default("15m"),
2835
}),
2936
refresh: z.object({
30-
secret: z.string().min(32, "JWT_REFRESH_SECRET must be at least 32 characters"),
37+
secret: z
38+
.string()
39+
.min(32, "JWT_REFRESH_SECRET must be at least 32 characters"),
3140
expiresIn: z.string().default("18h"),
3241
}),
3342
}),
@@ -90,7 +99,9 @@ function loadConfig(): Config {
9099

91100
// Warn about missing GitHub OAuth
92101
if (!config.github.clientId || !config.github.clientSecret) {
93-
console.warn("⚠️ GitHub OAuth not configured (GITHUB_CLIENT_ID/GITHUB_CLIENT_SECRET missing)");
102+
console.warn(
103+
"⚠️ GitHub OAuth not configured (GITHUB_CLIENT_ID/GITHUB_CLIENT_SECRET missing)"
104+
);
94105
}
95106

96107
return config;

src/routes/auth.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setCookie } from "hono/cookie";
33
import { verify } from "hono/jwt";
44
import { authService } from "../services/auth";
55
import { github as githubConfig, jwt as jwtConfig } from "../config";
6-
import { LoginResponseSchema, type JWTPayload } from "../types";
6+
import { LoginResponseSchema } from "../types";
77
import { jsonResponse } from "../utils/validate";
88

99
const auth = new Hono();
@@ -86,7 +86,7 @@ auth.get("/callback/github", async (c) => {
8686
// Create JWT tokens
8787
const tokens = await authService.createTokens(user);
8888

89-
// Set access_token cookie (only access token, not refresh)
89+
// Set access_token cookie
9090
setCookie(c, "access_token", tokens.accessToken, {
9191
httpOnly: true,
9292
secure: true,
@@ -127,6 +127,13 @@ auth.post("/refresh", async (c) => {
127127
// Generate new tokens
128128
const tokens = await authService.refreshTokens(payload.sub);
129129

130+
// Set access_token cookie
131+
setCookie(c, "access_token", tokens.accessToken, {
132+
httpOnly: true,
133+
secure: true,
134+
sameSite: "Strict",
135+
});
136+
130137
return jsonResponse(c, LoginResponseSchema, tokens);
131138
} catch (error) {
132139
console.error("Refresh token verification failed:", error);

src/routes/crons.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Hono } from 'hono';
22
import { cronsService } from '../services/crons';
33
import { workersService } from '../services/workers';
4+
import { CronCreateInputSchema, CronUpdateInputSchema, CronSchema } from '../types';
5+
import { jsonResponse } from '../utils/validate';
46

57
const crons = new Hono();
68

@@ -10,16 +12,11 @@ crons.put('/:id', async (c) => {
1012
const id = c.req.param('id');
1113
const body = await c.req.json();
1214

13-
if (!body.value) {
14-
return c.json({ error: 'Missing required field: value' }, 400);
15-
}
16-
1715
try {
18-
const cron = await cronsService.update(userId, id, {
19-
value: body.value,
20-
});
16+
const payload = CronUpdateInputSchema.parse(body);
17+
const cron = await cronsService.update(userId, id, payload);
2118

22-
return c.json(cron);
19+
return jsonResponse(c, CronSchema, cron);
2320
} catch (error) {
2421
console.error('Failed to update cron:', error);
2522
return c.json({
@@ -61,16 +58,10 @@ crons.post('/', async (c) => {
6158
const userId = c.get('userId');
6259
const body = await c.req.json();
6360

64-
if (!body.workerId || !body.value) {
65-
return c.json({ error: 'Missing required fields: workerId, value' }, 400);
66-
}
67-
6861
try {
69-
const cron = await cronsService.create(userId, {
70-
workerId: body.workerId,
71-
value: body.value
72-
});
73-
return c.json(cron, 201);
62+
const payload = CronCreateInputSchema.parse(body);
63+
const cron = await cronsService.create(userId, payload);
64+
return jsonResponse(c, CronSchema, cron, 201);
7465
} catch (error) {
7566
console.error('Failed to create cron:', error);
7667
return c.json({

src/routes/domains.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Hono } from 'hono';
22
import { domainsService } from '../services/domains';
3+
import { DomainFullSchema, DomainCreateInputSchema } from '../types';
4+
import { jsonResponse, jsonArrayResponse } from '../utils/validate';
35

46
const domains = new Hono();
57

@@ -8,7 +10,7 @@ domains.get('/', async (c) => {
810
const userId = c.get('userId');
911
try {
1012
const domains = await domainsService.findAll(userId);
11-
return c.json(domains);
13+
return jsonArrayResponse(c, DomainFullSchema, domains);
1214
} catch (error) {
1315
console.error('Failed to fetch domains:', error);
1416
return c.json({ error: 'Failed to fetch domains' }, 500);
@@ -20,16 +22,10 @@ domains.post('/', async (c) => {
2022
const userId = c.get('userId');
2123
const body = await c.req.json();
2224

23-
if (!body.name || !body.workerId) {
24-
return c.json({ error: 'Missing required fields: name, workerId' }, 400);
25-
}
26-
2725
try {
28-
const domain = await domainsService.create(userId, {
29-
name: body.name,
30-
workerId: body.workerId
31-
});
32-
return c.json(domain, 201);
26+
const payload = DomainCreateInputSchema.parse(body);
27+
const domain = await domainsService.create(userId, payload);
28+
return jsonResponse(c, DomainFullSchema, domain, 201);
3329
} catch (error) {
3430
console.error('Failed to create domain:', error);
3531
return c.json({

0 commit comments

Comments
 (0)