-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Sanitize URL from URL.createObjectURL()
#5568
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script> | ||
| run(async function () { | ||
| const { | ||
| testHelpers: { createDirectLineEmulator } | ||
| } = window; | ||
|
|
||
| const { directLine, store } = createDirectLineEmulator(); | ||
|
|
||
| WebChat.renderWebChat({ directLine, store }, document.getElementById('webchat')); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
|
|
||
| await directLine.emulateIncomingActivity({ | ||
| attachments: [ | ||
| { | ||
| contentType: 'image/png', | ||
| contentUrl: | ||
| 'data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAK0lEQVQ4T2P8z8Dwn4GKgHHUQIpDczQMKQ5ChtEwHA1DMkJgNNmQEWhoWgBMAiftPRtHngAAAABJRU5ErkJggg==' | ||
| } | ||
| ], | ||
| type: 'message' | ||
| }); | ||
|
|
||
| const imageElement = pageElements.activities()[0].querySelector('img'); | ||
|
|
||
| const imageURL = imageElement.getAttribute('src'); | ||
|
|
||
| // THEN: This test is only relevant it is a blob URL. | ||
| expect(imageURL.startsWith('blob:')).toEqual(true); | ||
|
|
||
| const res = await fetch(imageURL); | ||
|
|
||
| const contentType = res.headers.get('content-type'); | ||
|
|
||
| // THEN: Content type should be "application/octet-stream". | ||
| expect(contentType).toBe('application/octet-stream'); | ||
|
|
||
| // THEN: Should show the image properly. | ||
| await host.snapshot('local'); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,53 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script> | ||
| run(async function () { | ||
| const { | ||
| testHelpers: { createDirectLineEmulator } | ||
| } = window; | ||
|
|
||
| const { directLine, store } = createDirectLineEmulator(); | ||
|
|
||
| WebChat.renderWebChat({ directLine, store }, document.getElementById('webchat')); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
|
|
||
| await directLine.emulateIncomingActivity({ | ||
| attachments: [ | ||
| { | ||
| contentType: 'image/png', | ||
| contentUrl: | ||
| 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAK0lEQVQ4T2P8z8Dwn4GKgHHUQIpDczQMKQ5ChtEwHA1DMkJgNNmQEWhoWgBMAiftPRtHngAAAABJRU5ErkJggg==' | ||
| } | ||
| ], | ||
| type: 'message' | ||
| }); | ||
|
|
||
| const imageElement = pageElements.activities()[0].querySelector('img'); | ||
|
|
||
| const imageURL = imageElement.getAttribute('src'); | ||
|
|
||
| // THEN: This test is only relevant it is a blob URL. | ||
| expect(imageURL.startsWith('blob:')).toEqual(true); | ||
|
|
||
| const res = await fetch(imageURL); | ||
|
|
||
| const contentType = res.headers.get('content-type'); | ||
|
|
||
| // THEN: Content type should be "image/png". | ||
| expect(contentType).toBe('image/png'); | ||
|
|
||
| // THEN: Should show the image properly. | ||
| await host.snapshot('local'); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,53 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script> | ||
| run(async function () { | ||
| const { | ||
| testHelpers: { createDirectLineEmulator } | ||
| } = window; | ||
|
|
||
| const { directLine, store } = createDirectLineEmulator(); | ||
|
|
||
| WebChat.renderWebChat({ directLine, store }, document.getElementById('webchat')); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
|
|
||
| await directLine.emulateIncomingActivity({ | ||
| attachments: [ | ||
| { | ||
| contentType: 'image/png', | ||
| contentUrl: | ||
| 'data:text/plain;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAK0lEQVQ4T2P8z8Dwn4GKgHHUQIpDczQMKQ5ChtEwHA1DMkJgNNmQEWhoWgBMAiftPRtHngAAAABJRU5ErkJggg==' | ||
| } | ||
| ], | ||
| type: 'message' | ||
| }); | ||
|
|
||
| const imageElement = pageElements.activities()[0].querySelector('img'); | ||
|
|
||
| const imageURL = imageElement.getAttribute('src'); | ||
|
|
||
| // THEN: This test is only relevant it is a blob URL. | ||
| expect(imageURL.startsWith('blob:')).toEqual(true); | ||
|
|
||
| const res = await fetch(imageURL); | ||
|
|
||
| const contentType = res.headers.get('content-type'); | ||
|
|
||
| // THEN: Content type should be rectified to "application/octet-stream". | ||
| expect(contentType).toBe('application/octet-stream'); | ||
|
|
||
| // THEN: Should show the image properly. | ||
| await host.snapshot('local'); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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,59 @@ | ||
| import { validateProps } from '@msinternal/botframework-webchat-react-valibot'; | ||
| import React, { memo } from 'react'; | ||
| import { custom, object, optional, pipe, readonly, safeParse, string, union, type InferInput } from 'valibot'; | ||
|
|
||
| import readDataURIToBlob from '../Utils/readDataURIToBlob'; | ||
| import ImageContent from './ImageContent'; | ||
| import { type WebChatAttachment } from './private/types/WebChatAttachment'; | ||
|
|
||
| const imageAttachmentPropsSchema = pipe( | ||
| object({ | ||
| attachment: custom<WebChatAttachment>( | ||
| value => | ||
| safeParse( | ||
| union([ | ||
| object({ | ||
| contentUrl: string(), | ||
| name: optional(string()), | ||
| thumbnailUrl: optional(string()) | ||
| }), | ||
| object({ | ||
| contentUrl: optional(string()), | ||
| name: optional(string()), | ||
| thumbnailUrl: string() | ||
| }) | ||
| ]), | ||
| value | ||
| ).success | ||
| ) | ||
| }), | ||
| readonly() | ||
| ); | ||
|
|
||
| type ImageAttachmentProps = InferInput<typeof imageAttachmentPropsSchema>; | ||
|
|
||
| // React component is better with standard function than arrow function. | ||
| // eslint-disable-next-line prefer-arrow-callback | ||
| const ImageAttachment = memo(function ImageAttachment(props: ImageAttachmentProps) { | ||
| const { attachment } = validateProps(imageAttachmentPropsSchema, props); | ||
|
|
||
| let imageURL = attachment.thumbnailUrl || attachment.contentUrl; | ||
|
|
||
| // To support Content Security Policy, data URI cannot be used. | ||
| // We need to parse the data URI into a blob: URL. | ||
| const blob = readDataURIToBlob(imageURL); | ||
|
|
||
| if (blob) { | ||
| // Only allow image/* for image, otherwise, treat it as binary. | ||
| // eslint-disable-next-line no-restricted-properties | ||
| imageURL = URL.createObjectURL( | ||
| new Blob([blob], { | ||
| type: blob.type.startsWith('image/') ? blob.type : 'application/octet-stream' | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return <ImageContent alt={attachment.name} src={imageURL} />; | ||
| }); | ||
|
|
||
| export default ImageAttachment; |
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
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.
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.