-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Files Service] Additional validation for mime-type allowances #234828
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b63b5fb
Validate mimeType from request body
tsullivan bbda7be
Add mime-type validation on download
tsullivan 581f027
Tests and hardening
tsullivan c62d7f1
update integration tests
tsullivan 3f9cae2
Prevent mime type manipulation from client-sent filename
tsullivan 187abbd
Update tests
tsullivan 5fd0887
cleanup
tsullivan eff63bc
Merge branch 'main' into fix/team-2022
tsullivan 8b94103
simplify getDownloadHeadersForFile
tsullivan c86fe0c
Fix cases api integration tests
tsullivan 52e532c
Merge branch 'main' into fix/team-2022
tsullivan 3fa9324
Minor cleanup
tsullivan fcb65f8
Merge branch 'main' into fix/team-2022
tsullivan cc3311b
Merge branch 'main' into fix/team-2022
tsullivan e581a91
Fix the method of setting res.file with explicit content-type
tsullivan 5e5cf5e
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine 9b571fc
Merge branch 'main' into fix/team-2022
tsullivan 944f678
Fix plain test for plain text with no extension
tsullivan 9d432d0
Merge branch 'fix/team-2022' of github.com:tsullivan/kibana into fix/…
tsullivan b8f3337
Merge branch 'main' into fix/team-2022
tsullivan 92603fe
Merge branch 'main' into fix/team-2022
tsullivan eb4183b
Merge branch 'main' into fix/team-2022
tsullivan ed7d95e
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine d1e4245
[CI] Auto-commit changed files from 'node scripts/styled_components_m…
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/platform/plugins/shared/files/server/routes/file_kind/helpers.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { IKibanaResponse } from '@kbn/core/server'; | ||
| import type { File, FileJSON, FileKind } from '../../../common'; | ||
| import { validateFileNameExtension, validateMimeType } from './helpers'; | ||
|
|
||
| describe('helpers', () => { | ||
| describe('validateMimeType', () => { | ||
| const createFileKind = (allowedMimeTypes?: string[]): FileKind => ({ | ||
| id: 'test-file-kind', | ||
| allowedMimeTypes, | ||
| http: { | ||
| create: { requiredPrivileges: [] }, | ||
| download: { requiredPrivileges: [] }, | ||
| }, | ||
| }); | ||
|
|
||
| it('should return undefined when fileKind has empty allowedMimeTypes array', () => { | ||
| const fileKind = createFileKind([]); | ||
| const result = validateMimeType('image/png', fileKind); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when mimeType is in allowedMimeTypes', () => { | ||
| const fileKind = createFileKind(['image/png', 'image/jpeg']); | ||
| const result = validateMimeType('image/png', fileKind); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return bad request response when mimeType is not in allowedMimeTypes', () => { | ||
| const fileKind = createFileKind(['image/png', 'image/jpeg']); | ||
| const result = validateMimeType('application/pdf', fileKind); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect((result as IKibanaResponse).status).toBe(400); | ||
| expect((result as IKibanaResponse).payload).toEqual({ | ||
| message: 'File type is not supported', | ||
| }); | ||
| }); | ||
|
|
||
| it('should be case sensitive for mime type validation', () => { | ||
| const fileKind = createFileKind(['image/png']); | ||
| const result = validateMimeType('Image/PNG', fileKind); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect((result as IKibanaResponse).status).toBe(400); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validateFileNameExtension', () => { | ||
| const createFile = (mimeType?: string) => | ||
| ({ | ||
| id: 'test-file', | ||
| data: { | ||
| id: 'test-file', | ||
| name: 'test-file', | ||
| mimeType, | ||
| extension: 'txt', | ||
| fileKind: 'test', | ||
| } as FileJSON, | ||
| } as File); | ||
|
|
||
| it('should return undefined when fileName is undefined', () => { | ||
| const file = createFile('image/png'); | ||
| const result = validateFileNameExtension(undefined, file); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when file is undefined', () => { | ||
| const result = validateFileNameExtension('test.png', undefined); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when file has no mimeType', () => { | ||
| const file = createFile(); | ||
| const result = validateFileNameExtension('test.png', file); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when fileName has no extension', () => { | ||
| const file = createFile('text/plain'); | ||
| const result = validateFileNameExtension('README', file); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when extension matches expected extension', () => { | ||
| const file = createFile('image/png'); | ||
| const result = validateFileNameExtension('image.png', file); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should handle mime types with no known extensions', () => { | ||
| const file = createFile('application/x-custom-type'); | ||
| const result = validateFileNameExtension('file.custom', file); | ||
|
|
||
| // Should return undefined since there are no expected extensions for this mime type | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should handle file names with special characters', () => { | ||
| const file = createFile('text/plain'); | ||
|
|
||
| expect(validateFileNameExtension('[email protected]', file)).toBeUndefined(); | ||
| expect(validateFileNameExtension('файл.txt', file)).toBeUndefined(); // Unicode filename | ||
| expect(validateFileNameExtension('file with spaces.txt', file)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should trim whitespace from mime type before validation', () => { | ||
| const file = createFile(' text/plain '); | ||
| const result = validateFileNameExtension('test.txt', file); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should be case insensitive for file extensions', () => { | ||
| const file = createFile('image/png'); | ||
|
|
||
| expect(validateFileNameExtension('image.PNG', file)).toBeUndefined(); | ||
| expect(validateFileNameExtension('image.Png', file)).toBeUndefined(); | ||
| expect(validateFileNameExtension('image.pNG', file)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return bad request when extension does not match mime type', () => { | ||
| const file = createFile('image/png'); | ||
| const result = validateFileNameExtension('document.pdf', file); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect((result as IKibanaResponse).status).toBe(400); | ||
| expect((result as IKibanaResponse).payload).toEqual({ | ||
| message: 'File extension does not match file type', | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.