Skip to content

Remove unsafe type assertions from card messages and UI helpers - #100798

Merged
blimpich merged 5 commits into
Expensify:mainfrom
KJ21-ENG:seatbelt-v2/replacement-run2-273bdc63
Oct 19, 2026
Merged

blimpich merged 5 commits into
Expensify:mainfrom
KJ21-ENG:seatbelt-v2/replacement-run2-273bdc63

Conversation

@KJ21-ENG

@KJ21-ENG KJ21-ENG commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Use existing card-action and HTML-element guards in card messages, receipt hover, input-focus tracking, and filename measurement. Build progressive-reveal nodes with typed shallow clones. Supported card messages and HTML controls keep their existing behavior; non-HTML targets are ignored.

Fixed Issues

$ #94739
PROPOSAL: #94739 (comment)

Tests

Relevant upstream TypeScript and Jest checks passed. tests/unit/libs/ReportActionFollowupUtils/tokenizeForRevealTest.ts covers progressive text and nested/atomic markup; tests/unit/hooks/usePendingConciergeResponse.test.ts covers reveal completion and original HTML.

Run on supported web browsers. The receipt-hover case requires a pointer-capable desktop.

Receipt hover

Setup: Open an editable expense thread with a receipt and Add additional receipt available.

  1. Hover Add additional receipt. Verify its tooltip appears.
  2. Open the file picker. Verify the tooltip clears.
  3. Select a valid synthetic receipt.
  4. Move the pointer away and back. Verify normal hover and tooltip behavior returns.

Long filename

Setup: Use an account already eligible for Wallet → bank account → Enable global reimbursements → Agreements. Do not accept agreements or submit the document.

  1. Use the bank-statement Choose file control to select a permitted synthetic file with a long filename.
  2. Narrow the viewport and wait at least 250 ms. Verify the filename keeps its start and end around a middle ellipsis.
  3. Widen the viewport and wait at least 250 ms. Verify more filename text appears and the controls remain usable.
  4. Check for new console errors during both cases. Compare any errors with the same flow on base before attributing them to this PR.

Offline tests

Not applicable: these changes guard existing DOM targets and clone parsed HTML; network requests, persistence, and reconnect behavior are unchanged.

QA Steps

Same as tests.

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Recordings (1)

Windows desktop Chrome: receipt-button hover, picker selection and hover recovery were documented as passing. Receipt images loaded unreliably, and console errors were observed without confirmed attribution to this PR. Filename resizing and mobile-web/Safari results are unverified.

01-receipt-hover-single-selection-desktop.mp4

Seatbelt-Operation: sign-273bdc63-e974836a-ffe5-4d41-b642-361af944871a
@codecov

codecov Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...c/components/ReportActionItem/IssueCardMessage.tsx 92.00% <100.00%> (+0.33%) ⬆️
...ibs/ReportActionFollowupUtils/tokenizeForReveal.ts 93.10% <90.00%> (-1.24%) ⬇️
src/hooks/useTextWithMiddleEllipsis.ts 0.00% <0.00%> (ø)
src/hooks/useTackInputFocus/index.ts 0.00% <0.00%> (ø)
...onents/ReportActionItem/receiptHoverUtils/index.ts 0.00% <0.00%> (ø)
... and 219 files with indirect coverage changes

Seatbelt-Operation: sign-repair-273bdc63-5f4a77f5-11f8-4586-b0d5-133db827eda9
@KJ21-ENG KJ21-ENG changed the title Remove unsafe assertions from card messages, DOM hooks, and reveal tokenization Remove unsafe type assertions from card messages and UI helpers Sep 29, 2026
Resolve receipt hover and middle ellipsis ref conflicts by retaining
upstream ComponentRef signatures and the reviewed DOM guards.
Seatbelt-Operation: main-update-100798-20261016-sign-40962e38
@KJ21-ENG
KJ21-ENG marked this pull request as ready for review October 16, 2026 21:22
@KJ21-ENG
KJ21-ENG requested review from a team as code owners October 16, 2026 21:22
@melvin-bot
melvin-bot Bot requested a review from ikevin127 October 16, 2026 21:22
@melvin-bot

melvin-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

@ikevin127 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot requested review from trjExpensify and removed request for a team October 16, 2026 21:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T21:37:39.058216Z 3cacff1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@melvin-bot
melvin-bot Bot removed the request for review from a team October 16, 2026 21:22
if (isLeaf) {
if (stopNode.type === ElementType.Text && anchor.textEndIdx !== undefined) {
const truncated = {...stopNode, data: stopNode.data.slice(0, anchor.textEndIdx)} as unknown as AnyNode;
const truncated = Object.assign(stopNode.cloneNode(false), stopNode);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-18 (docs)

Object.assign(stopNode.cloneNode(false), stopNode) is a non-obvious idiom. A reader cannot tell why the node is cloned and then immediately overwritten with the source node's own properties. The reason, that cloneNode(false) yields a value with the correct domhandler prototype/methods while Object.assign makes it an independent mutable copy so the data/children mutation below does not affect the original node, is not recoverable from the code. The same idiom is repeated at line 91, so a single comment explaining it helps both sites.

Add a short comment above the construct, e.g.:

// cloneNode(false) keeps the correct node prototype/methods; Object.assign copies the
// original's own properties so this is an independent, mutable shallow copy we can edit
// without mutating the shared source node.
const truncated = Object.assign(stopNode.cloneNode(false), stopNode);
truncated.data = stopNode.data.slice(0, anchor.textEndIdx);

Reviewed at: 3cacff1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced Object.assign with direct shallow cloning. Kept an explicit parent link because the installed serializer uses it to preserve escaping and namespace behavior. Added regression coverage for both cases.

@ikevin127

ikevin127 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Screen.Recording.2026-10-17.at.18.26.51.mov

const styles = useThemeStyles();
const session = useSession();
const assigneeAccountID = (getOriginalMessage(action) as IssueNewCardOriginalMessage)?.assigneeAccountID;
const cardIssuedActionOriginalMessage = isCardIssuedAction(action) ? getOriginalMessage(action) : undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 src/components/ReportActionItem/IssueCardMessage.tsx:40

The only caller (ActionContentRouter.tsx:489) already renders this component behind if (isCardIssuedAction(action)), so checking isCardIssuedAction again here repeats a check the caller has already done.

Can we narrow the prop type instead, so getOriginalMessage(action) is typed directly ?

type IssueCardMessageProps = {
    action: ReportAction<
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_ISSUED
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_ISSUED_VIRTUAL
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_MISSING_ADDRESS
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_ASSIGNED
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_REPLACED_VIRTUAL
        | typeof CONST.REPORT.ACTIONS.TYPE.CARD_REPLACED
    >;
    ...
};

const originalMessage = getOriginalMessage(action);
const assigneeAccountID = originalMessage?.assigneeAccountID;
const companyCard = originalMessage ? cardList?.[originalMessage.cardID] : undefined;

This also means a non-card action can't be passed in by mistake: today that would type-check and just render with no card or assignee.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrowed IssueCardMessage's action prop to the six card-issued action variants already checked by its caller. The component now reads originalMessage directly.

if (isLeaf) {
if (stopNode.type === ElementType.Text && anchor.textEndIdx !== undefined) {
const truncated = {...stopNode, data: stopNode.data.slice(0, anchor.textEndIdx)} as unknown as AnyNode;
const truncated = Object.assign(stopNode.cloneNode(false), stopNode);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 src/libs/ReportActionFollowupUtils/tokenizeForReveal.ts:81

Following up on the bot's comment here: rather than adding a comment, I think Object.assign can just go. It copies every own field of stopNode back over the clone (attribs, children, parent, prev, next), which undoes most of what cloneNode(false) just did.

cloneNode already returns a typed Text / Element, so this does the same job:

const truncated = stopNode.cloneNode(false);
truncated.data = stopNode.data.slice(0, anchor.textEndIdx);
...
const partialElem = stopNode.cloneNode(false);
partialElem.children = innerChildren;

dom-serializer only reads type, name, attribs, data and children, so the parent/prev/next links aren't needed. The same applies to :92.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced Object.assign with direct cloneNode calls. I retained each clone's parent link: removing it changes serialization for noscript text and MathML line breaks. Added tests covering those cases at every reveal stage; all 254 selected tests pass.

@ikevin127

Copy link
Copy Markdown
Contributor

@KJ21-ENG Tag me once the (2) code-comments are addressed 🙌

@trjExpensify

Copy link
Copy Markdown
Contributor

PR doesn’t need product input as a refactor PR. Unassigning and unsubscribing myself.

Seatbelt-Operation: reviewer-fix-100798-dd013b03-51e7-45d0-bb8f-222283b35d62
@KJ21-ENG

Copy link
Copy Markdown
Contributor Author

@ikevin127 Could you please check this once you get a chance, Thanks!

@ikevin127 ikevin127 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 LGTM - Both of comments were addressed, and on the parent link the author was right and I wasn't. Thanks for addressing everything!

@melvin-bot
melvin-bot Bot requested a review from blimpich October 19, 2026 23:03
@blimpich
blimpich merged commit 134c7e6 into Expensify:main Oct 19, 2026
38 of 41 checks passed
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

Copy link
Copy Markdown
Contributor

🚧 blimpich has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/blimpich in version: 9.5.15-1 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update is needed. This PR removes unsafe type assertions and adds type guards, so users see no change in behavior or names.


view run

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/grgia in version: 9.5.15-3 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants