Skip to content

[AdvancedPaste] Harden ToJsonFromXmlOrCsvAsync against clipboard read failures - #48124

Merged
Gordon Lam (yeelam-gordon) merged 1 commit into
mainfrom
dev/yeelam/advancedpaste-jsonhelper-input-handling
May 26, 2026
Merged

Gordon Lam (yeelam-gordon) merged 1 commit into
mainfrom
dev/yeelam/advancedpaste-jsonhelper-input-handling

Conversation

@yeelam-gordon

Copy link
Copy Markdown
Contributor

Summary

ToJsonFromXmlOrCsvAsync in AdvancedPaste/Helpers/JsonHelper.cs documents that it never throws and returns an empty string on any failure. The clipboard read at the top of the method (clipboardData.GetTextAsync()) was not wrapped, so a transient clipboard failure could surface as an exception to callers, contrary to the documented contract.

This PR:

  • Wraps GetTextAsync() in a try/catch and returns string.Empty on failure, matching the pattern already used by the JSON/XML/CSV parsing branches further down in the same method.
  • Updates the matching unit test to decode input bytes as UTF-8 (Encoding.UTF8.GetString(input)) and consume the awaited task via GetAwaiter().GetResult(), for consistency with sibling tests elsewhere in the solution.

Validation

  • Local build of AdvancedPaste.sln. (Note: my machine has a pre-existing NuGet SDK resolver issue unrelated to this change — the same baseline fails on main for me. CI should be the source of truth.)

… failures

- Wrap clipboardData.GetTextAsync() in try/catch and return string.Empty on
  failure, matching the documented `returns empty string on any failure`
  contract of ToJsonFromXmlOrCsvAsync.
- Update the matching unit test to decode input bytes as UTF-8 via
  Encoding.UTF8.GetString(input) and consume the awaited Task via
  GetAwaiter().GetResult(), for consistency with sibling tests in the
  same solution.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

Pull request overview

Hardens AdvancedPaste’s JSON conversion helper to uphold its non-throwing behavior when clipboard text retrieval fails, and updates the associated fuzz test to correctly interpret fuzz inputs as UTF-8 text while preserving exception types.

Changes:

  • Wrap DataPackageView.GetTextAsync() in ToJsonFromXmlOrCsvAsync with try/catch and return string.Empty on failure.
  • Update fuzz test input handling to Encoding.UTF8.GetString(input) to actually exercise the helper with provided bytes.
  • Update fuzz test task consumption to use GetAwaiter().GetResult() to avoid AggregateException wrapping.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/modules/AdvancedPaste/AdvancedPaste/Helpers/JsonHelper.cs Prevents clipboard text read failures from escaping as exceptions by returning string.Empty.
src/modules/AdvancedPaste/AdvancedPaste.FuzzTests/FuzzTests.cs Improves fuzz input decoding and exception surfacing behavior for the JSON helper.

@yeelam-gordon
Gordon Lam (yeelam-gordon) merged commit 85b9191 into main May 26, 2026
17 checks passed
@yeelam-gordon
Gordon Lam (yeelam-gordon) deleted the dev/yeelam/advancedpaste-jsonhelper-input-handling branch May 26, 2026 02:59
@LegendaryBlair Boliang Zhang (LegendaryBlair) added 0.100 Product-Advanced Paste Refers to the Advanced Paste module labels May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0.100 Product-Advanced Paste Refers to the Advanced Paste module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants