Skip to content
Closed
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
2 changes: 1 addition & 1 deletion data/static/codefixes/changeProductChallenge_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

app.post('/api/Addresss', security.appendUserId())
app.get('/api/Addresss', security.appendUserId(), utils.asyncHandler(address.getAddress()))
app.put('/api/Addresss/:id', security.appendUserId())
app.put('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.updateAddressById()))
app.delete('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.delAddressById()))
app.get('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.getAddressById()))
app.get('/api/Deliverys', utils.asyncHandler(delivery.getDeliveryMethods()))
Expand Down
2 changes: 1 addition & 1 deletion data/static/codefixes/changeProductChallenge_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

app.post('/api/Addresss', security.appendUserId())
app.get('/api/Addresss', security.appendUserId(), utils.asyncHandler(address.getAddress()))
app.put('/api/Addresss/:id', security.appendUserId())
app.put('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.updateAddressById()))
app.delete('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.delAddressById()))
app.get('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.getAddressById()))
app.get('/api/Deliverys', utils.asyncHandler(delivery.getDeliveryMethods()))
Expand Down
2 changes: 1 addition & 1 deletion data/static/codefixes/changeProductChallenge_3_correct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

app.post('/api/Addresss', security.appendUserId())
app.get('/api/Addresss', security.appendUserId(), utils.asyncHandler(address.getAddress()))
app.put('/api/Addresss/:id', security.appendUserId())
app.put('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.updateAddressById()))
app.delete('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.delAddressById()))
app.get('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.getAddressById()))
app.get('/api/Deliverys', utils.asyncHandler(delivery.getDeliveryMethods()))
Expand Down
2 changes: 1 addition & 1 deletion data/static/codefixes/changeProductChallenge_4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

app.post('/api/Addresss', security.appendUserId())
app.get('/api/Addresss', security.appendUserId(), utils.asyncHandler(address.getAddress()))
app.put('/api/Addresss/:id', security.appendUserId())
app.put('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.updateAddressById()))
app.delete('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.delAddressById()))
app.get('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.getAddressById()))
app.get('/api/Deliverys', utils.asyncHandler(delivery.getDeliveryMethods()))
Expand Down
20 changes: 20 additions & 0 deletions routes/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { type Request, type Response } from 'express'
import { AddressModel } from '../models/address'
import * as utils from '../lib/utils'

export function getAddress () {
return async (req: Request, res: Response) => {
Expand All @@ -24,6 +25,25 @@ export function getAddressById () {
}
}

export function updateAddressById () {
return async (req: Request, res: Response) => {
const address = await AddressModel.findOne({ where: { id: req.params.id, UserId: req.body.UserId } })
if (address == null) {
res.status(400).json({ status: 'error', data: 'Malicious activity detected.' })
return
}
const fields = ['fullName', 'mobileNum', 'zipCode', 'streetAddress', 'city', 'state', 'country'] as const
const updateData = Object.fromEntries(fields.filter(field => field in req.body).map(field => [field, req.body[field]]))
try {
await address.update(updateData)
} catch (error: unknown) {
res.status(400).json({ status: 'error', error: utils.getErrorMessage(error) })
return
}
res.status(200).json({ status: 'success', data: address })
}
}

export function delAddressById () {
return async (req: Request, res: Response) => {
const address = await AddressModel.destroy({ where: { id: req.params.id, UserId: req.body.UserId } })
Expand Down
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function configureApp (app: ReturnType<typeof express>, seq: typeof sequelize) {

app.post('/api/Addresss', security.appendUserId())
app.get('/api/Addresss', security.appendUserId(), utils.asyncHandler(address.getAddress()))
app.put('/api/Addresss/:id', security.appendUserId())
app.put('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.updateAddressById()))
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
app.delete('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.delAddressById()))
app.get('/api/Addresss/:id', security.appendUserId(), utils.asyncHandler(address.getAddressById()))
app.get('/api/Deliverys', utils.asyncHandler(delivery.getDeliveryMethods()))
Expand Down
18 changes: 18 additions & 0 deletions test/api/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ void describe('/api/Addresss/:id', () => {
assert.equal(res.status, 400)
})

void it('PUT update address of another user is forbidden', async () => {
const { token } = await login(app, {
email: 'bender@juice-sh.op',
password: 'OhG0dPlease1nsertLiquor!'
})
const res = await request(app)
.put('/api/Addresss/' + addressId)
.set({ Authorization: 'Bearer ' + token, 'content-type': 'application/json' })
.send({ fullName: 'Hijacked' })
assert.equal(res.status, 400)
assert.equal(res.body.data, 'Malicious activity detected.')

const address = await request(app)
.get('/api/Addresss/' + addressId)
.set(authHeader)
assert.equal(address.body.data.fullName, 'Jimy')
})

void it('DELETE address by id', async () => {
const res = await request(app)
.delete('/api/Addresss/' + addressId)
Expand Down
Loading