-
-
Notifications
You must be signed in to change notification settings - Fork 3k
frontend: handle session transfer request failures #8180
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
Fhatu12
wants to merge
1
commit into
ether:develop
Choose a base branch
from
Fhatu12:fix/8172-session-transfer-errors
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import html10n from './vendors/html10n'; | ||
|
|
||
| const checkmark = '<svg width="28" height="28" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor"><path vector-effect="non-scaling-stroke" stroke-linecap="round" stroke-linejoin="round" d="m4.5 12.75 6 6 9-13.5"/></svg>'; | ||
|
|
||
| function getCookie(name: string) { | ||
|
|
@@ -11,46 +13,137 @@ function getCookie(name: string) { | |
|
|
||
| const cp = (window as any).clientVars?.cookiePrefix || ''; | ||
|
|
||
| const sessionTransferErrorFallback = () => | ||
| html10n.get('index.sessionTransferError') || 'Unable to transfer the session. Please try again.'; | ||
|
|
||
| const safeJson = async (response: Response): Promise<unknown> => { | ||
| try { | ||
| return await response.json(); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const responseErrorMessage = (responseData: unknown): string => { | ||
| const data = responseData as Record<string, unknown>; | ||
| if ( | ||
| responseData && | ||
| typeof responseData === 'object' && | ||
| 'error' in responseData && | ||
| typeof data.error === 'string' && | ||
| data.error.trim() !== '' | ||
| ) { | ||
| return data.error; | ||
| } | ||
| return sessionTransferErrorFallback(); | ||
| }; | ||
|
|
||
| const showSessionTransferError = (element: HTMLElement | null, message: string) => { | ||
| if (!element) return; | ||
| element.textContent = message; | ||
| element.style.display = 'block'; | ||
| }; | ||
|
|
||
| const hideSessionTransferError = (element: HTMLElement | null) => { | ||
| if (!element) return; | ||
| element.textContent = ''; | ||
| element.style.display = 'none'; | ||
| }; | ||
|
|
||
| function handleTransferOfSession() { | ||
| const transferNowButton = document.querySelector('[data-l10n-id="index.transferSessionNow"]')! as HTMLButtonElement; | ||
|
|
||
| transferNowButton.addEventListener('click', async () => { | ||
| const originalButtonContent = transferNowButton.innerHTML; | ||
| const copyLinkSection = document.getElementById('copy-link-section'); | ||
| const errorElement = document.getElementById('transfer-session-error'); | ||
| hideSessionTransferError(errorElement); | ||
| if (copyLinkSection) copyLinkSection.style.display = 'none'; | ||
| transferNowButton.style.display = 'inline-flex'; | ||
| transferNowButton.style.alignItems = 'center'; | ||
| transferNowButton.style.justifyContent = 'center'; | ||
| transferNowButton.innerHTML = `${checkmark}`; | ||
| transferNowButton.disabled = true; | ||
|
|
||
| // The author token is HttpOnly (ether/etherpad#6701 PR3) so we cannot | ||
| // read it via document.cookie. Send only the JS-readable prefsHttp; the | ||
| // server reads the token off the request's own cookie jar. | ||
| const responseWithId = await fetch("./tokenTransfer", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json" | ||
| }, | ||
| body: JSON.stringify({ | ||
| prefsHttp: getCookie(`${cp}prefsHttp`) || getCookie('prefsHttp'), | ||
| }) | ||
| }) | ||
| try { | ||
| // The author token is HttpOnly (ether/etherpad#6701 PR3) so we cannot | ||
| // read it via document.cookie. Send only the JS-readable prefsHttp; the | ||
| // server reads the token off the request's own cookie jar. | ||
| const responseWithId = await fetch("./tokenTransfer", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json" | ||
| }, | ||
| body: JSON.stringify({ | ||
| prefsHttp: getCookie(`${cp}prefsHttp`) || getCookie('prefsHttp'), | ||
| }) | ||
| }); | ||
|
|
||
| const copyLinkSection = document.getElementById('copy-link-section') | ||
| if (!copyLinkSection) return; | ||
| copyLinkSection.style.display = 'block'; | ||
|
|
||
| const copyButton = document.querySelector('#copy-link-section .btn-secondary') as HTMLButtonElement | ||
| const responseData = await responseWithId.json(); | ||
| copyButton.addEventListener('click', async ()=>{ | ||
| await navigator.clipboard.writeText(responseData.id); | ||
| copyButton.style.display = 'inline-flex'; | ||
| copyButton.style.alignItems = 'center'; | ||
| copyButton.style.justifyContent = 'center'; | ||
| copyButton.innerHTML = `${checkmark}`; | ||
| copyButton.disabled = true; | ||
| }) | ||
| const responseData = await safeJson(responseWithId); | ||
| if (!responseWithId.ok) { | ||
| throw new Error(responseErrorMessage(responseData)); | ||
| } | ||
| const transferData = responseData as Record<string, unknown>; | ||
| if (!responseData || typeof responseData !== 'object' || | ||
| !('id' in responseData) || typeof transferData.id !== 'string' || | ||
| transferData.id.trim() === '') { | ||
| throw new Error(sessionTransferErrorFallback()); | ||
| } | ||
|
|
||
| if (!copyLinkSection) throw new Error(sessionTransferErrorFallback()); | ||
| copyLinkSection.style.display = 'block'; | ||
|
|
||
| const copyButton = document.querySelector('#copy-link-section .btn-secondary') as HTMLButtonElement; | ||
| copyButton.disabled = false; | ||
| copyButton.onclick = async () => { | ||
| await navigator.clipboard.writeText(transferData.id as string); | ||
| copyButton.style.display = 'inline-flex'; | ||
| copyButton.style.alignItems = 'center'; | ||
| copyButton.style.justifyContent = 'center'; | ||
| copyButton.innerHTML = `${checkmark}`; | ||
| copyButton.disabled = true; | ||
| }; | ||
| transferNowButton.innerHTML = `${checkmark}`; | ||
| } catch (err) { | ||
| if (copyLinkSection) copyLinkSection.style.display = 'none'; | ||
| transferNowButton.innerHTML = originalButtonContent; | ||
| transferNowButton.disabled = false; | ||
| showSessionTransferError( | ||
| errorElement, | ||
| err instanceof Error && err.message ? err.message : sessionTransferErrorFallback()); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const isValidTransferCode = (code: string) => code.length === 36; | ||
|
|
||
| async function redeemTransferCode( | ||
| code: string, | ||
| transferSessionButton: HTMLButtonElement, | ||
| errorElement: HTMLElement | null) { | ||
| hideSessionTransferError(errorElement); | ||
| transferSessionButton.disabled = true; | ||
|
|
||
| try { | ||
| const response = await fetch("./tokenTransfer/"+code, { | ||
| method: 'GET' | ||
| }); | ||
| const responseData = await safeJson(response); | ||
| if (!response.ok) { | ||
| throw new Error(responseErrorMessage(responseData)); | ||
| } | ||
| const transferData = responseData as Record<string, unknown>; | ||
| if (!responseData || typeof responseData !== 'object' || | ||
| !('ok' in responseData) || transferData.ok !== true) { | ||
| throw new Error(sessionTransferErrorFallback()); | ||
| } | ||
| window.location.reload() | ||
| } catch (err) { | ||
| transferSessionButton.disabled = !isValidTransferCode(code); | ||
|
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. 2. Stale code enables submit When redemption fails, redeemTransferCode() restores the button from the captured submitted code, not the code currently in the editable input. If the user changes the input to an invalid value while the request is pending, the catch block overrides the input listener and enables the button, allowing an invalid redemption request. Agent Prompt
|
||
| showSessionTransferError( | ||
| errorElement, | ||
| err instanceof Error && err.message ? err.message : sessionTransferErrorFallback()); | ||
| } | ||
| } | ||
|
|
||
| const handleSettingsButtonClick = () => { | ||
| const settingsButton = document.querySelector('.settings-button')!; | ||
|
|
@@ -86,24 +179,22 @@ const handleMenuBarClicked = () => { | |
| }); | ||
| }) | ||
|
|
||
| const transferSessionButton = document.getElementById('transferSessionButton') | ||
| const transferSessionButton = document.getElementById('transferSessionButton') as HTMLButtonElement | null; | ||
| const codeInputField = document.getElementById('codeInput') as HTMLInputElement | ||
| if (transferSessionButton) { | ||
| transferSessionButton.addEventListener('click', ()=>{ | ||
| const code = codeInputField.value | ||
| fetch("./tokenTransfer/"+code, { | ||
| method: 'GET' | ||
| }) | ||
| .then(res => res.json()) | ||
| .then(()=>{ | ||
| window.location.reload() | ||
| }) | ||
| const code = codeInputField.value; | ||
| redeemTransferCode( | ||
| code, | ||
| transferSessionButton, | ||
| document.getElementById('receive-session-error')); | ||
| }); | ||
| } | ||
|
|
||
| if (codeInputField) { | ||
| codeInputField.addEventListener('input', (e)=>{ | ||
| if ((e.target as HTMLInputElement).value?.length === 36) { | ||
| hideSessionTransferError(document.getElementById('receive-session-error')); | ||
| if (isValidTransferCode((e.target as HTMLInputElement).value)) { | ||
| transferSessionButton?.removeAttribute('disabled'); | ||
| } else { | ||
| transferSessionButton?.setAttribute('disabled', 'true'); | ||
|
|
||
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.
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.
1. id accepts malformed values
📎 Requirement gap≡ CorrectnessThe create flow treats every non-empty string as a valid transfer ID, so a malformed 2xx payload such as {id: "x"} exposes a copy-success state containing an unusable code. Created IDs are UUIDs and the redeem flow requires 36 characters, but this validation does not enforce that format.Agent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools