Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 53
prevent pasting large input on secure compose#4914
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
db627449646d453349f70b90f9a5f61d1e5fae387d0d2174382782ac8b394b37188655d1f23e7691c5dfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,7 +10,9 @@ import { ParsedRecipients } from '../../../js/common/api/email-provider/email-pr | ||
| import { Str } from '../../../js/common/core/common.js'; | ||
| import { Xss } from '../../../js/common/platform/xss.js'; | ||
| import { ViewModule } from '../../../js/common/view-module.js'; | ||
| import { Ui } from '../../../js/common/browser/ui.js'; | ||
| import { ComposeView } from '../compose.js'; | ||
| import { Lang } from '../../../js/common/lang.js'; | ||
| export class ComposeInputModule extends ViewModule<ComposeView> { | ||
| public squire = new window.Squire(this.view.S.cached('input_text').get(0)); | ||
| @@ -78,12 +80,25 @@ export class ComposeInputModule extends ViewModule<ComposeView> { | ||
| return this.view.sendBtnModule.popover.choices.richtext; | ||
| }; | ||
| public willInputLimitBeExceeded = (textToPaste: string, targetInputField: HTMLElement, selectionLengthGetter: () => number | undefined) => { | ||
| const limit = 50000; | ||
| const toBeRemoved = selectionLengthGetter() || 0; | ||
| const currentLength = targetInputField.innerText.trim().length; | ||
| const isInputLimitExceeded = currentLength - toBeRemoved + textToPaste.length > limit; | ||
| return isInputLimitExceeded; | ||
| }; | ||
| private handlePaste = () => { | ||
| this.squire.addEventListener('willPaste', (e: WillPasteEvent) => { | ||
| this.squire.addEventListener('willPaste', async (e: WillPasteEvent) => { | ||
| const div = document.createElement('div'); | ||
| div.appendChild(e.fragment); | ||
| const html = div.innerHTML; | ||
| const sanitized = this.isRichText() ? Xss.htmlSanitizeKeepBasicTags(html, 'IMG-KEEP') : Xss.htmlSanitizeAndStripAllTags(html, '<br>', false); | ||
| if (this.willInputLimitBeExceeded(sanitized, this.squire.getRoot(), () => this.squire.getSelectedText().length)) { | ||
| e.preventDefault(); | ||
| await Ui.modal.warning(Lang.compose.inputLimitExceededOnPaste); | ||
| return; | ||
| } | ||
Contributor 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.
CollaboratorAuthor 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. right - you are correct. So that | ||
| Xss.setElementContentDANGEROUSLY(div, sanitized); // xss-sanitized | ||
| e.fragment.appendChild(div); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -31,6 +31,7 @@ import { PubLookup } from '../../js/common/api/pub-lookup.js'; | ||
| import { AcctStore } from '../../js/common/platform/store/acct-store.js'; | ||
| import { AccountServer } from '../../js/common/api/account-server.js'; | ||
| import { ComposeReplyBtnPopoverModule } from './compose-modules/compose-reply-btn-popover-module.js'; | ||
| import { Lang } from '../../js/common/lang.js'; | ||
| export class ComposeView extends View { | ||
| public readonly acctEmail: string; | ||
| @@ -222,6 +223,25 @@ export class ComposeView extends View { | ||
| 'click', | ||
| this.setHandler(async () => await this.renderModule.openSettingsWithDialog('help'), this.errModule.handle(`help dialog`)) | ||
| ); | ||
| this.S.cached('input_intro').on( | ||
| 'paste', | ||
| this.setHandler(async (el, ev) => { | ||
| const clipboardEvent = ev.originalEvent as ClipboardEvent; | ||
| if (clipboardEvent.clipboardData) { | ||
| const isInputLimitExceeded = this.inputModule.willInputLimitBeExceeded(clipboardEvent.clipboardData.getData('text/plain'), el, () => { | ||
| const selection = window.getSelection(); | ||
| if (selection && selection.anchorNode === selection.focusNode && selection.anchorNode?.parentElement === el) { | ||
| return Math.abs(selection.anchorOffset - selection.focusOffset); | ||
| } | ||
| return 0; | ||
| }); | ||
| if (isInputLimitExceeded) { | ||
| ev.preventDefault(); | ||
martgil marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| await Ui.modal.warning(Lang.compose.inputLimitExceededOnPaste); | ||
| } | ||
| } | ||
| }) | ||
| ); | ||
| this.attachmentsModule.setHandlers(); | ||
| this.inputModule.setHandlers(); | ||
| this.myPubkeyModule.setHandlers(); | ||
Uh oh!
There was an error while loading. Please reload this page.