Skip to content

Commit 40480fd

Browse files
committed
fix: added input validator for dnsSuffix
1 parent 46a74cb commit 40480fd

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*********************************************************************
2+
* Copyright (c) Intel Corporation 2026
3+
* SPDX-License-Identifier: Apache-2.0
4+
**********************************************************************/
5+
6+
import { validationResult } from 'express-validator'
7+
import { describe, expect, it } from '@jest/globals'
8+
import { validator } from './deviceValidator.js'
9+
10+
async function getValidationErrors(body: any): Promise<any[]> {
11+
const req: any = { body, query: {}, params: {} }
12+
const chains = validator()
13+
14+
for (const chain of chains) {
15+
await chain.run(req)
16+
}
17+
18+
return validationResult(req).array()
19+
}
20+
21+
describe('device validator dnsSuffix checks', () => {
22+
it('accepts a valid dnsSuffix', async () => {
23+
const errors = await getValidationErrors({
24+
guid: '123e4567-e89b-12d3-a456-426614174000',
25+
dnsSuffix: 'os.suffix.com'
26+
})
27+
28+
expect(errors).toHaveLength(0)
29+
})
30+
31+
it('accepts null dnsSuffix', async () => {
32+
const errors = await getValidationErrors({
33+
guid: '123e4567-e89b-12d3-a456-426614174000',
34+
dnsSuffix: null
35+
})
36+
37+
expect(errors).toHaveLength(0)
38+
})
39+
40+
it("accepts empty string dnsSuffix", async () => {
41+
const errors = await getValidationErrors({
42+
guid: '123e4567-e89b-12d3-a456-426614174000',
43+
dnsSuffix: ''
44+
})
45+
46+
expect(errors).toHaveLength(0)
47+
})
48+
49+
it('rejects dnsSuffix containing query string characters', async () => {
50+
const errors = await getValidationErrors({
51+
guid: '123e4567-e89b-12d3-a456-426614174000',
52+
dnsSuffix: 'None?injected_query_string=123'
53+
})
54+
55+
expect(errors.some((error) => error.path === 'dnsSuffix')).toBeTruthy()
56+
const dnsSuffixError = errors.find((error) => error.path === 'dnsSuffix')
57+
expect(dnsSuffixError).toBeDefined()
58+
expect(dnsSuffixError?.msg).toContain('dnsSuffix')
59+
})
60+
})

src/routes/devices/deviceValidator.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export const validator = (): any => [
1313
.isString()
1414
.isLength({ max: 255 })
1515
.withMessage('Hostname must be less than 256 characters'),
16+
check('dnsSuffix')
17+
.optional({ nullable: true })
18+
.isString()
19+
.bail()
20+
.isLength({ max: 255 })
21+
.withMessage('dnsSuffix must be less than 256 characters')
22+
.bail()
23+
.if((value) => value !== '')
24+
.isFQDN({ require_tld: false, allow_trailing_dot: true })
25+
.withMessage('dnsSuffix must be a valid DNS suffix'),
1626
check('mpsusername').optional({ nullable: true }).isString(),
1727
check('connect').optional({ nullable: true }).isISO8601().toDate(),
1828
check('disconnect').optional({ nullable: true }).isISO8601().toDate(),

0 commit comments

Comments
 (0)