-
Notifications
You must be signed in to change notification settings - Fork 77
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
[CHAT-1471] Allowed/blocked file extensions and content types #582
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1c611dc
add tests for file extension and mime configs
miagilepner adccaf3
Merge branch 'master' into feat/CHAT-1471-app-config-upload-types
miagilepner bbda110
Update helloworld.md
miagilepner 96a8666
Merge branch 'master' into feat/CHAT-1471-app-config-upload-types
miagilepner a7f20d3
remove empty file
miagilepner 99feee5
Merge remote-tracking branch 'origin/master' into feat/CHAT-1471-app-…
miagilepner c24e893
Merge branch 'master' into feat/CHAT-1471-app-config-upload-types
miagilepner d73d7ae
Merge branch 'master' into feat/CHAT-1471-app-config-upload-types
miagilepner 42a0e8d
Merge branch 'master' into feat/CHAT-1471-app-config-upload-types
vishalnarkhede 5190d7b
Merge remote-tracking branch 'origin/master' into feat/CHAT-1471-app-…
miagilepner a6c7f4b
use FileUploadConfig type
miagilepner 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 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 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,323 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fs from 'fs'; | ||
import { expectHTTPErrorCode, getServerTestClient, getTestClientForUser } from './utils'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
const expect = chai.expect; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
require('longjohn'); | ||
} | ||
|
||
const Promise = require('bluebird'); | ||
Promise.config({ | ||
longStackTraces: true, | ||
warnings: { | ||
wForgottenReturn: false, | ||
}, | ||
}); | ||
|
||
describe('Uploads', () => { | ||
const serverClient = getServerTestClient(); | ||
const channelType = uuidv4(); | ||
let client; | ||
let channel; | ||
|
||
before(async () => { | ||
client = await getTestClientForUser(uuidv4()); | ||
await serverClient.createChannelType({ | ||
name: channelType, | ||
commands: ['all'], | ||
uploads: true, | ||
}); | ||
channel = client.channel(channelType, uuidv4()); | ||
await channel.watch(); | ||
}); | ||
afterEach(async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_file_extensions: [], | ||
blocked_file_extensions: [], | ||
allowed_mime_types: [], | ||
blocked_mime_types: [], | ||
}, | ||
image_upload_config: { | ||
allowed_file_extensions: [], | ||
blocked_file_extensions: [], | ||
allowed_mime_types: [], | ||
blocked_mime_types: [], | ||
}, | ||
}); | ||
}); | ||
describe('File Extension Restrictions', () => { | ||
it('set file extension allowlist and blocklist', async () => { | ||
await expectHTTPErrorCode( | ||
400, | ||
serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_file_extensions: ['.txt', '.csv'], | ||
blocked_file_extensions: ['.json'], | ||
}, | ||
}), | ||
`StreamChat error code 4: UpdateApp failed with error: "Cannot specify both allowed file extensions and blocked file extensions"`, | ||
); | ||
}); | ||
it('set image file extension allowlist and blocklist', async () => { | ||
await expectHTTPErrorCode( | ||
400, | ||
serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_file_extensions: ['.jpg', '.png'], | ||
blocked_file_extensions: ['.heic'], | ||
}, | ||
}), | ||
`StreamChat error code 4: UpdateApp failed with error: "Cannot specify both allowed file extensions and blocked file extensions"`, | ||
); | ||
}); | ||
it('upload a file with extension on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_file_extensions: ['.txt', '.csv'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.txt'); | ||
const response = await channel.sendFile(file, 'helloworld.txt', 'text/plain'); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload an image with extension on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_file_extensions: ['.jpg', '.png'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.jpg'); | ||
const response = await channel.sendImage( | ||
file, | ||
'helloworld.jpg', | ||
'image/jpeg', | ||
); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload a file with extension not on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
blocked_file_extensions: ['.pdf'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.txt'); | ||
const response = await channel.sendFile(file, 'helloworld.txt', 'text/plain'); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload an image with extension not on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
blocked_file_extensions: ['.heic'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.jpg'); | ||
const response = await channel.sendImage( | ||
file, | ||
'helloworld.jpg', | ||
'image/jpeg', | ||
); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload a file with extension not on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_file_extensions: ['.txt', '.csv'], | ||
}, | ||
}); | ||
const file = Buffer.from('random string'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendFile(file, 'helloworld.md', 'text/markdown'), | ||
`StreamChat error code 4: UploadFile failed with error: "File extension .md is not supported"`, | ||
); | ||
}); | ||
it('upload an image with extension not on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_file_extensions: ['.jpeg'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.heic'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendImage(file, 'helloworld.heic', 'image/heic'), | ||
`StreamChat error code 4: UploadImage failed with error: "File extension .heic is not supported"`, | ||
); | ||
}); | ||
it('upload a file with extension on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
blocked_file_extensions: ['.md'], | ||
}, | ||
}); | ||
const file = Buffer.from('random string'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendFile(file, 'helloworld.md', 'text/markdown'), | ||
`StreamChat error code 4: UploadFile failed with error: "File extension .md is not supported"`, | ||
); | ||
}); | ||
it('upload an image with extension on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
blocked_file_extensions: ['.heic'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.heic'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendImage(file, 'helloworld.heic', 'image/heic'), | ||
`StreamChat error code 4: UploadImage failed with error: "File extension .heic is not supported"`, | ||
); | ||
}); | ||
}); | ||
describe('Content Type Restrictions', () => { | ||
it('set content type allowlist and blocklist', async () => { | ||
await expectHTTPErrorCode( | ||
400, | ||
serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_mime_types: ['text/plain', 'text/csv'], | ||
blocked_mime_types: ['application/json'], | ||
}, | ||
}), | ||
`StreamChat error code 4: UpdateApp failed with error: "Cannot specify both allowed mime types and blocked mime types"`, | ||
); | ||
}); | ||
it('set image mime type allowlist and blocklist', async () => { | ||
await expectHTTPErrorCode( | ||
400, | ||
serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_mime_types: ['text/plain', 'text/csv'], | ||
blocked_mime_types: ['application/json'], | ||
}, | ||
}), | ||
`StreamChat error code 4: UpdateApp failed with error: "Cannot specify both allowed mime types and blocked mime types"`, | ||
); | ||
}); | ||
it('upload a file with mime type on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_mime_types: ['text/plain', 'text/csv'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.txt'); | ||
const response = await channel.sendFile(file, 'helloworld.txt', 'text/plain'); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload an image with mime type on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_mime_types: ['image/jpeg'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.jpg'); | ||
const response = await channel.sendImage( | ||
file, | ||
'helloworld.jpg', | ||
'image/jpeg', | ||
); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload a file with mime type not on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
blocked_mime_types: ['application/json'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.txt'); | ||
const response = await channel.sendFile(file, 'helloworld.txt', 'text/plain'); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload an image with mime type not on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
blocked_mime_types: ['image/heic'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.jpg'); | ||
const response = await channel.sendImage( | ||
file, | ||
'helloworld.jpg', | ||
'image/jpeg', | ||
); | ||
expect(response.file).to.be.an('string'); | ||
expect(response.file).to.be.not.undefined; | ||
expect(response.file).to.be.not.eq(''); | ||
}); | ||
it('upload a file with mime type not on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
allowed_mime_types: ['text/plain', 'text/csv'], | ||
}, | ||
}); | ||
const file = Buffer.from('random string'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendFile(file, 'helloworld.md', 'text/markdown'), | ||
`StreamChat error code 4: UploadFile failed with error: "File type text/markdown is not supported"`, | ||
); | ||
}); | ||
it('upload an image with mime type not on allowlist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
allowed_mime_types: ['image/jpeg'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.heic'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendImage(file, 'helloworld.heic', 'image/heic'), | ||
`StreamChat error code 4: UploadImage failed with error: "File type image/heic is not supported"`, | ||
); | ||
}); | ||
it('upload a file with mime type on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
file_upload_config: { | ||
blocked_mime_types: ['text/markdown'], | ||
}, | ||
}); | ||
const file = Buffer.from('random string'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendFile(file, 'helloworld.md', 'text/markdown'), | ||
`StreamChat error code 4: UploadFile failed with error: "File type text/markdown is not supported"`, | ||
); | ||
}); | ||
it('upload an image with mime type on blocklist', async () => { | ||
await serverClient.updateAppSettings({ | ||
image_upload_config: { | ||
blocked_mime_types: ['image/heic'], | ||
}, | ||
}); | ||
const file = fs.createReadStream('helloworld.heic'); | ||
await expectHTTPErrorCode( | ||
400, | ||
channel.sendImage(file, 'helloworld.heic', 'image/heic'), | ||
`StreamChat error code 4: UploadImage failed with error: "File type image/heic is not supported"`, | ||
); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't we use this type above ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was following the way that it's done for other objects, which repeat fields rather than using the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any example for a reference ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. I thought firebase_config and apn_config did this, but I see now they have slightly different fields in AppSettings. I'll fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they are different slightly but here is exactly same. Thanks for updating.