Skip to content

Commit

Permalink
fix(api-client): sanitize file name thoroughly (#15062)
Browse files Browse the repository at this point in the history
more thoroughly remove all spaces and special characters from splash file name

closes RQA-2668
  • Loading branch information
brenthagen authored May 1, 2024
1 parent b15af5e commit 044be7a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
20 changes: 20 additions & 0 deletions api-client/src/system/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest'
import { sanitizeFileName } from '../utils'

describe('sanitizeFileName', () => {
it('returns original alphanumeric file name', () => {
expect(sanitizeFileName('an0ther_otie_logo.png')).toEqual(
'an0ther_otie_logo.png'
)
})

it('sanitizes a file name', () => {
expect(
sanitizeFileName(
`otie's birthday/party - (& the bouncy castle cost ~$100,000).jpeg`
)
).toEqual(
'otie_s_birthday_party_-____the_bouncy_castle_cost___100_000_.jpeg'
)
})
})
6 changes: 4 additions & 2 deletions api-client/src/system/createSplash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { POST, request } from '../request'
import { sanitizeFileName } from './utils'
import type { ResponsePromise } from '../request'
import type { HostConfig } from '../types'

export function createSplash(
config: HostConfig,
file: File
): ResponsePromise<void> {
// sanitize file name to ensure no spaces
const renamedFile = new File([file], file.name.replace(' ', '_'), {
// sanitize file name to ensure no spaces or special characters
const newFileName = sanitizeFileName(file.name)
const renamedFile = new File([file], newFileName, {
type: 'image/png',
})

Expand Down
1 change: 1 addition & 0 deletions api-client/src/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { createRegistration } from './createRegistration'
export { createSplash } from './createSplash'
export { getConnections } from './getConnections'
export * from './types'
export * from './utils'
3 changes: 3 additions & 0 deletions api-client/src/system/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sanitizeFileName(fileName: string): string {
return fileName.replace(/[^a-zA-Z0-9-.]/gi, '_')
}

0 comments on commit 044be7a

Please sign in to comment.