-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidation-example.ts
More file actions
150 lines (125 loc) · 3.68 KB
/
validation-example.ts
File metadata and controls
150 lines (125 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Example: Using Input Validation Middleware
*
* This example demonstrates how to use the input validation middleware
* to protect your API from malicious inputs.
*/
import zero from '0http-bun'
import type { ZeroRequest } from '0http-bun'
import {
validationMiddleware,
createValidationMiddleware,
type ValidationMiddlewareConfig,
} from '../src/security'
// Example 1: Basic validation with default settings
const app1 = zero().router
// Apply validation middleware globally
app1.use(validationMiddleware())
app1.get('/api/users', async (req: ZeroRequest) => {
return new Response(JSON.stringify({ users: [] }), {
headers: { 'Content-Type': 'application/json' },
})
})
// Example 2: Custom validation rules
const app2 = zero().router
const customValidationConfig: ValidationMiddlewareConfig = {
rules: {
maxPathLength: 1024,
maxHeaderSize: 8192,
maxHeaderCount: 50,
},
validatePaths: true,
validateHeaders: true,
validateQueryParams: true,
}
app2.use(createValidationMiddleware(customValidationConfig))
app2.get('/api/search', async (req: ZeroRequest) => {
// Query params are already validated
const query = req.query.q || ''
return new Response(JSON.stringify({ results: [], query }), {
headers: { 'Content-Type': 'application/json' },
})
})
// Example 3: Custom error handler
const app3 = zero().router
app3.use(
createValidationMiddleware({
onValidationError: (errors, req) => {
// Log validation errors
console.error('Validation failed:', {
url: req.url,
errors,
ip: req.headers.get('x-forwarded-for') || 'unknown',
})
// Return custom error response
return new Response(
JSON.stringify({
success: false,
message: 'Invalid request',
errors: errors.map((e) => ({ message: e })),
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
},
)
},
}),
)
app3.post('/api/data', async (req: ZeroRequest) => {
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' },
})
})
// Example 4: Selective validation
const app4 = zero().router
// Only validate paths and query params, skip headers
app4.use(
createValidationMiddleware({
validatePaths: true,
validateHeaders: false,
validateQueryParams: true,
}),
)
app4.get('/api/public', async (req: ZeroRequest) => {
return new Response('Public endpoint')
})
// Start servers
const PORT1 = 3001
const PORT2 = 3002
const PORT3 = 3003
const PORT4 = 3004
console.log('Starting validation middleware examples...\n')
console.log(`Example 1 (Default validation): http://localhost:${PORT1}`)
console.log(` Try: curl "http://localhost:${PORT1}/api/users"`)
console.log(
` Try: curl "http://localhost:${PORT1}/api/users?id=1' OR '1'='1" (should fail)\n`,
)
console.log(`Example 2 (Custom rules): http://localhost:${PORT2}`)
console.log(` Try: curl "http://localhost:${PORT2}/api/search?q=test"`)
console.log(
` Try: curl "http://localhost:${PORT2}/api/search?q=<script>alert(1)</script>" (should fail)\n`,
)
console.log(`Example 3 (Custom error handler): http://localhost:${PORT3}`)
console.log(` Try: curl -X POST "http://localhost:${PORT3}/api/data"`)
console.log(
` Try: curl -X POST "http://localhost:${PORT3}/api/data?cmd=rm -rf /" (should fail)\n`,
)
console.log(`Example 4 (Selective validation): http://localhost:${PORT4}`)
console.log(` Try: curl "http://localhost:${PORT4}/api/public"\n`)
Bun.serve({
port: PORT1,
fetch: app1.fetch,
})
Bun.serve({
port: PORT2,
fetch: app2.fetch,
})
Bun.serve({
port: PORT3,
fetch: app3.fetch,
})
Bun.serve({
port: PORT4,
fetch: app4.fetch,
})