-
Notifications
You must be signed in to change notification settings - Fork 0
[PB-1967]: feat/download attachments #56
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b85a51e
feat(attachments): download attachments
xabg2 415cd03
feat(attachments): download attachments
xabg2 1bf755b
Merge branch 'master' into feat/download-attachments
xabg2 8bb45f8
feat: check if the attachment belong s to the email
xabg2 a4e55c4
Merge branch 'master' into feat/download-attachments
xabg2 5261272
fix: spec unsafe types
xabg2 a983339
fix: jmap service test
xabg2 f7793e3
feat: add attachment to the mails
xabg2 f586b02
refactor: update client name and extract business logic from controller
xabg2 16b4328
refactor: extract logic to a function
xabg2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { | ||
| buildContentDisposition, | ||
| sanitizeFilename, | ||
| sanitizeMimeType, | ||
| } from './attachment-headers.js'; | ||
|
|
||
| describe('attachment-headers', () => { | ||
| describe('sanitizeFilename', () => { | ||
| it('when given a regular filename, then it is returned untouched', () => { | ||
| expect(sanitizeFilename('photo.jpg')).toBe('photo.jpg'); | ||
| }); | ||
|
|
||
| it('when no filename is provided, then a generic fallback name is used', () => { | ||
| expect(sanitizeFilename(undefined)).toBe('attachment'); | ||
| expect(sanitizeFilename('')).toBe('attachment'); | ||
| }); | ||
|
|
||
| it('when the filename only contains whitespace, then the generic fallback name is used', () => { | ||
| expect(sanitizeFilename(' ')).toBe('attachment'); | ||
| }); | ||
|
|
||
| it('when the filename contains line breaks or quotes, then those characters are stripped', () => { | ||
| expect(sanitizeFilename('photo\r\n.jpg')).toBe('photo.jpg'); | ||
| expect(sanitizeFilename('a"b\\c.txt')).toBe('abc.txt'); | ||
|
Check warning on line 25 in src/modules/email/attachment-headers.spec.ts
|
||
| }); | ||
|
|
||
| it('when the filename is very long, then it is shortened to a safe length', () => { | ||
| const longName = 'a'.repeat(500) + '.txt'; | ||
| const result = sanitizeFilename(longName); | ||
| expect(result.length).toBe(255); | ||
| }); | ||
|
|
||
| it('when the filename has non-ascii characters, then it preserves them', () => { | ||
| expect(sanitizeFilename('fôto-año.pdf')).toBe('fôto-año.pdf'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sanitizeMimeType', () => { | ||
| it('when given a valid content type, then it is returned untouched', () => { | ||
| expect(sanitizeMimeType('image/jpeg')).toBe('image/jpeg'); | ||
| expect(sanitizeMimeType('application/pdf')).toBe('application/pdf'); | ||
| expect(sanitizeMimeType('application/vnd.ms-excel')).toBe( | ||
| 'application/vnd.ms-excel', | ||
| ); | ||
| }); | ||
|
|
||
| it('when no content type is provided, then no value is returned', () => { | ||
| expect(sanitizeMimeType(undefined)).toBeNull(); | ||
| expect(sanitizeMimeType('')).toBeNull(); | ||
| }); | ||
|
|
||
| it('when the content type is malformed, then it is rejected', () => { | ||
| expect(sanitizeMimeType('not-a-mime')).toBeNull(); | ||
| expect(sanitizeMimeType('image/')).toBeNull(); | ||
| expect(sanitizeMimeType('/jpeg')).toBeNull(); | ||
| expect(sanitizeMimeType('image jpeg')).toBeNull(); | ||
| }); | ||
|
|
||
| it('when the content type contains suspicious characters, then it is rejected', () => { | ||
| expect(sanitizeMimeType('image/jpeg\r\nX-Injected: yes')).toBeNull(); | ||
| expect(sanitizeMimeType('image/jpeg;charset=utf-8')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildContentDisposition', () => { | ||
| it('when given a simple filename, then it is included both as ascii and utf-8', () => { | ||
| const result = buildContentDisposition('photo.jpg'); | ||
| expect(result).toBe( | ||
| `attachment; filename="photo.jpg"; filename*=UTF-8''photo.jpg`, | ||
| ); | ||
| }); | ||
|
|
||
| it('when the filename has non-ascii characters, then the utf-8 part is percent-encoded', () => { | ||
| const result = buildContentDisposition('año.pdf'); | ||
| expect(result).toBe( | ||
| `attachment; filename="año.pdf"; filename*=UTF-8''a%C3%B1o.pdf`, | ||
| ); | ||
| }); | ||
|
|
||
| it('when the filename has spaces, then the utf-8 part escapes them', () => { | ||
| const result = buildContentDisposition('my photo.jpg'); | ||
| expect(result).toBe( | ||
| `attachment; filename="my photo.jpg"; filename*=UTF-8''my%20photo.jpg`, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
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,18 @@ | ||
| const MIME_TYPE_REGEX = /^[a-zA-Z0-9.+-]+\/[a-zA-Z0-9.+-]+$/; | ||
|
|
||
| export function sanitizeFilename(name: string | undefined): string { | ||
| if (!name) return 'attachment'; | ||
| const cleaned = name.replace(/[\r\n"\\]/g, '').trim(); | ||
| if (!cleaned) return 'attachment'; | ||
| return cleaned.slice(0, 255); | ||
| } | ||
|
|
||
| export function sanitizeMimeType(type: string | undefined): string | null { | ||
| if (!type) return null; | ||
| return MIME_TYPE_REGEX.test(type) ? type : null; | ||
| } | ||
|
|
||
| export function buildContentDisposition(filename: string): string { | ||
| const encoded = encodeURIComponent(filename); | ||
| return `attachment; filename="${filename}"; filename*=UTF-8''${encoded}`; | ||
| } |
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
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.
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.
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.
nitpick controller logic should stay thin with no business logic. the res.setHeader stuff does belong here