-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: attachments with # in the filename fail to download #7563
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
Open
OtavioStasiak
wants to merge
4
commits into
develop
Choose a base branch
from
fix.raw-in-attachment-filemae-truncates-downloadurl
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5fa0c1
fix: attachments with # in the filename fail to download
OtavioStasiak d8a9e68
fix: test
OtavioStasiak 88e5f91
fix: broken attachment urls from a raw `#` and double-encoding
OtavioStasiak 7da8df6
fix: detect gifs when #or%23 follows the extension
OtavioStasiak 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,86 @@ | ||
| import { formatAttachmentUrl } from './formatAttachmentUrl'; | ||
| import { store as reduxStore } from '../../store/auxStore'; | ||
|
|
||
| jest.mock('../../store/auxStore', () => ({ | ||
| store: { | ||
| getState: jest.fn() | ||
| } | ||
| })); | ||
|
|
||
| const mockedGetState = reduxStore.getState as jest.Mock; | ||
|
|
||
| const SERVER = 'https://open.rocket.chat'; | ||
| const USER_ID = 'userId'; | ||
| const TOKEN = 'token'; | ||
|
|
||
| const mockSettings = ({ protectFiles = false, cdnPrefix = '' } = {}) => | ||
| mockedGetState.mockReturnValue({ settings: { FileUpload_ProtectFiles: protectFiles, CDN_PREFIX: cdnPrefix } }); | ||
|
|
||
| describe('formatAttachmentUrl', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| // The server builds the path with `encodeURI(file.name)`, which encodes the spaces but leaves `#` raw — the url | ||
| // would otherwise be cut at the fragment and the server would receive `/file-upload/1/a%20video%20`. | ||
| describe('raw `#` in the filename', () => { | ||
| const rawUrl = '/file-upload/1/a%20video%20#2.mov'; | ||
| const escapedUrl = `${SERVER}/file-upload/1/a%20video%20%232.mov`; | ||
|
|
||
| test('escapes it on a relative url', () => { | ||
| mockSettings(); | ||
| expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe(escapedUrl); | ||
| }); | ||
|
|
||
| test('escapes it on an absolute url', () => { | ||
| mockSettings(); | ||
| expect(formatAttachmentUrl(`${SERVER}${rawUrl}`, USER_ID, TOKEN, SERVER)).toBe(escapedUrl); | ||
| }); | ||
|
|
||
| test('keeps the whole path when the auth params are appended', () => { | ||
| mockSettings({ protectFiles: true }); | ||
| expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe(`${escapedUrl}?rc_token=${TOKEN}&rc_uid=${USER_ID}`); | ||
| }); | ||
|
|
||
| // The `%20`s the server already applied must survive as-is — encoding them again would give `%2520`. | ||
| test('escapes it without re-encoding the rest of the path', () => { | ||
| mockSettings(); | ||
| expect(formatAttachmentUrl(`${SERVER}${rawUrl}?rc_token=${TOKEN}&rc_uid=${USER_ID}`, USER_ID, TOKEN, SERVER)).toBe( | ||
| `${escapedUrl}?rc_token=${TOKEN}&rc_uid=${USER_ID}` | ||
| ); | ||
| }); | ||
|
|
||
| test('escapes it behind a cdn prefix', () => { | ||
| mockSettings({ cdnPrefix: 'https://cdn.example.com/' }); | ||
| expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe( | ||
| 'https://cdn.example.com/file-upload/1/a%20video%20%232.mov' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| test('leaves an already-escaped `#` untouched', () => { | ||
| mockSettings(); | ||
| expect(formatAttachmentUrl('/file-upload/1/a%20video%20%232.mov', USER_ID, TOKEN, SERVER)).toBe( | ||
| `${SERVER}/file-upload/1/a%20video%20%232.mov` | ||
| ); | ||
| }); | ||
|
|
||
| // An external url is not a file-upload path, so a `#` there can be a genuine fragment. | ||
| test('returns an external original url verbatim', () => { | ||
| mockSettings(); | ||
| const externalUrl = 'https://example.com/page#section'; | ||
| expect(formatAttachmentUrl(`${SERVER}/file-upload/1/file.mov`, USER_ID, TOKEN, SERVER, externalUrl)).toBe(externalUrl); | ||
| }); | ||
|
|
||
| test('returns a base64 data uri untouched', () => { | ||
| mockSettings(); | ||
| const base64 = 'data:image/png;base64,ABC123'; | ||
| expect(formatAttachmentUrl(base64, USER_ID, TOKEN, SERVER)).toBe(base64); | ||
| }); | ||
|
|
||
| test('returns a local file uri untouched', () => { | ||
| mockSettings(); | ||
| const fileUri = 'file:///var/app/Documents/server/msg1/video.mov'; | ||
| expect(formatAttachmentUrl(fileUri, USER_ID, TOKEN, SERVER)).toBe(fileUri); | ||
| }); | ||
| }); |
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 |
|---|---|---|
|
|
@@ -10,6 +10,9 @@ function setParamInUrl({ url, token, userId }: { url: string; token: string; use | |
| return urlObj.toString(); | ||
| } | ||
|
|
||
| // The server encodes the path with `encodeURI(file.name)`, which leaves `#` raw and truncates the url at the fragment. | ||
| const escapeFragmentDelimiter = (url: string) => url.replace(/#/g, '%23'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What other symbols should be escaped as well? |
||
|
|
||
| export const formatAttachmentUrl = ( | ||
| attachmentUrl: string | undefined, | ||
| userId: string, | ||
|
|
@@ -27,18 +30,21 @@ export const formatAttachmentUrl = ( | |
| return _originalUrl; | ||
| } | ||
|
|
||
| if (attachmentUrl.includes('rc_token')) { | ||
| return encodeURI(attachmentUrl); | ||
| const url = escapeFragmentDelimiter(attachmentUrl); | ||
|
|
||
| if (url.includes('rc_token')) { | ||
| return url; | ||
| } | ||
|
|
||
| if (protectFiles) return setParamInUrl({ url: attachmentUrl, token, userId }); | ||
| return attachmentUrl; | ||
| if (protectFiles) return setParamInUrl({ url, token, userId }); | ||
| return url; | ||
| } | ||
| let cdnPrefix = store?.getState().settings.CDN_PREFIX as string; | ||
| cdnPrefix = cdnPrefix?.trim(); | ||
| if (cdnPrefix && cdnPrefix.startsWith('http')) { | ||
| server = cdnPrefix.replace(/\/+$/, ''); | ||
| } | ||
| if (protectFiles) return setParamInUrl({ url: `${server}${attachmentUrl}`, token, userId }); | ||
| return `${server}${attachmentUrl}`; | ||
| const url = escapeFragmentDelimiter(`${server}${attachmentUrl}`); | ||
| if (protectFiles) return setParamInUrl({ url, token, userId }); | ||
| return url; | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,9 +59,9 @@ const RenderContent = ({ | |
| }, [navigation]); | ||
|
|
||
| if (attachment.image_url) { | ||
| const url = formatAttachmentUrl(attachment.title_link || attachment.image_url, user.id, user.token, baseUrl); | ||
| const uri = encodeURI(url); | ||
| const isAnimated = attachment.image_type === 'image/gif' || /\.gif(\?|$)/i.test(url); | ||
| const uri = formatAttachmentUrl(attachment.title_link || attachment.image_url, user.id, user.token, baseUrl); | ||
| // `#` is escaped to `%23` before it reaches here, so both spellings can follow the extension. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
| const isAnimated = attachment.image_type === 'image/gif' || /\.gif(\?|#|%23|$)/i.test(uri); | ||
| return ( | ||
| <ImageViewer | ||
| uri={uri} | ||
|
|
@@ -74,8 +74,7 @@ const RenderContent = ({ | |
| ); | ||
| } | ||
| if (attachment.video_url) { | ||
| const url = formatAttachmentUrl(attachment.title_link || attachment.video_url, user.id, user.token, baseUrl); | ||
| const uri = encodeURI(url); | ||
| const uri = formatAttachmentUrl(attachment.title_link || attachment.video_url, user.id, user.token, baseUrl); | ||
| return ( | ||
| <Video | ||
| source={{ uri }} | ||
|
|
||
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.
The server does that? Are you sure? What version started doing that?