-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
196 lines (184 loc) · 4.96 KB
/
worker.js
File metadata and controls
196 lines (184 loc) · 4.96 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Cloudflare Worker for OWASP Juice Shop API
// This is a simplified version that provides basic product data
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Handle CORS
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
if (request.method === 'OPTIONS') {
return new Response(null, { status: 200, headers: corsHeaders });
}
// Mock product data for the Juice Shop
const products = [
{
id: 1,
name: "Apple Juice (1000ml)",
description: "The all-time classic.",
price: 1.99,
image: "apple_juice.jpg",
category: "Juices"
},
{
id: 2,
name: "Orange Juice (1000ml)",
description: "Made from the finest hand-picked oranges.",
price: 2.99,
image: "orange_juice.jpg",
category: "Juices"
},
{
id: 3,
name: "Banana Juice (1000ml)",
description: "Monkey-approved!",
price: 1.99,
image: "banana_juice.jpg",
category: "Juices"
},
{
id: 4,
name: "Raspberry Juice (1000ml)",
description: "Exclusive to our store!",
price: 4.99,
image: "raspberry_juice.jpg",
category: "Juices"
},
{
id: 5,
name: "Green Smoothie",
description: "Looks poisonous but fresh!",
price: 3.99,
image: "green_smoothie.jpg",
category: "Smoothies"
},
{
id: 6,
name: "Melon Bike (Comeback-2017 Edition)",
description: "The wheels of this bicycle have been replaced by melons.",
price: 3399.00,
image: "melon_bike.jpg",
category: "Bikes"
},
{
id: 7,
name: "OWASP Juice Shop T-Shirt",
description: "Real fans wear it 24/7.",
price: 20.99,
image: "owasp_tshirt.jpg",
category: "Accessories"
},
{
id: 8,
name: "OWASP Juice Shop Hoodie",
description: "Comfortable and warm.",
price: 49.99,
image: "owasp_hoodie.jpg",
category: "Accessories"
}
];
// Handle different API endpoints
if (url.pathname === '/rest/products' || url.pathname === '/api/products') {
return new Response(JSON.stringify(products), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
if (url.pathname.startsWith('/rest/products/')) {
const productId = parseInt(url.pathname.split('/').pop());
const product = products.find(p => p.id === productId);
if (product) {
return new Response(JSON.stringify(product), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
} else {
return new Response(JSON.stringify({ error: 'Product not found' }), {
status: 404,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
}
if (url.pathname === '/rest/user/login' && request.method === 'POST') {
return new Response(JSON.stringify({
authentication: {
token: "fake-jwt-token-for-demo",
bid: 1,
umail: "admin@juice-sh.op"
}
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
if (url.pathname === '/rest/user/registration' && request.method === 'POST') {
return new Response(JSON.stringify({
status: 'success',
data: {
id: 1,
email: 'user@example.com'
}
}), {
status: 201,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
if (url.pathname === '/rest/basket' && request.method === 'GET') {
return new Response(JSON.stringify({
id: 1,
Products: []
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
if (url.pathname === '/rest/basket' && request.method === 'POST') {
return new Response(JSON.stringify({
status: 'success',
data: {
id: 1,
ProductId: 1,
quantity: 1
}
}), {
status: 201,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
// Default response for unknown endpoints
return new Response(JSON.stringify({
error: 'Endpoint not found',
path: url.pathname,
method: request.method
}), {
status: 404,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
};