Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api-client): sanitize file name thoroughly #15062

Merged
merged 1 commit into from
May 1, 2024
Merged
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
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, '_')
}
Loading