Skip to content
Open
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
53 changes: 28 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"got": "^14.6.3",
"http-z": "^7.0.0",
"jws": "^4.0.0",
"mongodb": "^6.20.0",
"mongodb": "^7.0.0",
"mqtt": "^5.14.1",
"node-forge": "^1.3.1",
"pg": "^8.16.3",
Expand Down
17 changes: 16 additions & 1 deletion src/data/mongo/collections/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@ export class MongoDeviceTable implements IDeviceTable {
}

async update(item: Device): Promise<WithId<Device>> {
if (!(item as any)._id) {
throw new Error('Device must have an _id for update operation')
}

const itemId = (item as any)._id
let objectId: ObjectId

if (ObjectId.isValid(itemId)) {
objectId = new ObjectId(itemId)
} else if (typeof itemId === 'number') {
objectId = new ObjectId(itemId.toString().padStart(24, '0'))
} else {
throw new Error(`Invalid _id format: ${itemId}`)
Comment on lines +67 to +70
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting a number to an ObjectId by padding with zeros is fragile and could lead to unexpected behavior. This assumes numeric IDs are always less than 24 characters when stringified and that padding them to create a valid ObjectId hex string is meaningful.

Consider one of these approaches instead:

  • Store the original ObjectId format consistently in the database
  • Document why numeric IDs need special handling
  • Validate that the numeric ID is within acceptable bounds before conversion
Suggested change
} else if (typeof itemId === 'number') {
objectId = new ObjectId(itemId.toString().padStart(24, '0'))
} else {
throw new Error(`Invalid _id format: ${itemId}`)
} else {
throw new Error(`Invalid _id format: ${itemId}. Only valid ObjectId strings are supported.`)

Copilot uses AI. Check for mistakes.
}

const result = await this.collection.findOneAndUpdate(
{ _id: new ObjectId((item as any)._id as number), tenantId: item.tenantId },
{ _id: objectId, tenantId: item.tenantId },
{ $set: item },
{ returnDocument: 'after', includeResultMetadata: false }
)
Expand Down
8 changes: 7 additions & 1 deletion src/routes/devices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ deviceRouter.get('/', odataValidator(), metadataQueryValidator(), validateMiddle
deviceRouter.get('/stats', stats)
deviceRouter.get('/tags', getDistinctTags)
deviceRouter.get('/:guid', param('guid').isUUID('loose'), validateMiddleware, getDevice)
deviceRouter.get('/redirectstatus/:guid', param('guid').isUUID('loose'), validateMiddleware, ciraMiddleware, getRedirStatus)
deviceRouter.get(
'/redirectstatus/:guid',
param('guid').isUUID('loose'),
validateMiddleware,
ciraMiddleware,
getRedirStatus
)
deviceRouter.post('/', validator(), validateMiddleware, insertDevice)
deviceRouter.patch('/', validator(), validateMiddleware, updateDevice)
deviceRouter.delete('/refresh/:guid', validator(), validateMiddleware, refreshDevice)
Expand Down
Loading
Loading