Skip to content

Commit 044be7a

Browse files
authored
fix(api-client): sanitize file name thoroughly (#15062)
more thoroughly remove all spaces and special characters from splash file name closes RQA-2668
1 parent b15af5e commit 044be7a

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { sanitizeFileName } from '../utils'
3+
4+
describe('sanitizeFileName', () => {
5+
it('returns original alphanumeric file name', () => {
6+
expect(sanitizeFileName('an0ther_otie_logo.png')).toEqual(
7+
'an0ther_otie_logo.png'
8+
)
9+
})
10+
11+
it('sanitizes a file name', () => {
12+
expect(
13+
sanitizeFileName(
14+
`otie's birthday/party - (& the bouncy castle cost ~$100,000).jpeg`
15+
)
16+
).toEqual(
17+
'otie_s_birthday_party_-____the_bouncy_castle_cost___100_000_.jpeg'
18+
)
19+
})
20+
})

api-client/src/system/createSplash.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { POST, request } from '../request'
2+
import { sanitizeFileName } from './utils'
23
import type { ResponsePromise } from '../request'
34
import type { HostConfig } from '../types'
45

56
export function createSplash(
67
config: HostConfig,
78
file: File
89
): ResponsePromise<void> {
9-
// sanitize file name to ensure no spaces
10-
const renamedFile = new File([file], file.name.replace(' ', '_'), {
10+
// sanitize file name to ensure no spaces or special characters
11+
const newFileName = sanitizeFileName(file.name)
12+
const renamedFile = new File([file], newFileName, {
1113
type: 'image/png',
1214
})
1315

api-client/src/system/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { createRegistration } from './createRegistration'
33
export { createSplash } from './createSplash'
44
export { getConnections } from './getConnections'
55
export * from './types'
6+
export * from './utils'

api-client/src/system/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function sanitizeFileName(fileName: string): string {
2+
return fileName.replace(/[^a-zA-Z0-9-.]/gi, '_')
3+
}

0 commit comments

Comments
 (0)