Skip to content

encode jsobject#102

Merged
tayloraswift merged 1 commit intomasterfrom
encode-jsobject
Jan 23, 2026
Merged

encode jsobject#102
tayloraswift merged 1 commit intomasterfrom
encode-jsobject

Conversation

@tayloraswift
Copy link
Member

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @tayloraswift, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily focuses on extending the JavaScriptPersistence module with robust JSON encoding and decoding capabilities for JSObject and JSValue. It also incorporates Swift's advanced ownership features (borrowing and copy) into the JSON decoding logic, aiming for more efficient and safer data handling. Additionally, the PR includes a structural enhancement to the Swift Package Manager setup to better manage external linker dependencies for code snippets.

Highlights

  • JSON Encoding/Decoding for JSObject and JSValue: The core JSObject and JSValue types now conform to JSONEncodable and JSONDecodable, enabling seamless serialization and deserialization to and from JSON representations.
  • Swift Ownership Model Integration: The JSON decoding framework has been updated to utilize Swift's new borrowing and copy ownership modifiers, enhancing performance and memory safety across various decoding initializers and loops.
  • Refactored JSON Array Encoding: A new encodeElements helper method was introduced for Sequence types, streamlining the implementation of JSONEncodable for collections like Array, ArraySlice, and various lazy sequences.
  • SwiftPM Linker Settings Anchor: A dedicated Swift Package Manager target, _JSON_SnippetsAnchor, was added to provide a clean mechanism for linking libm for code snippets without affecting primary library targets.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces JSONEncodable and JSONDecodable conformance for JSObject and JSValue as part of a substantial refactoring, improving performance through Swift's ownership features and reorganizing code. However, a critical concern is the use of fatalError when encoding non-JSON-representable values like JSValue.undefined or failing to re-parse JSON numbers. This can lead to application crashes, creating a Denial of Service (DoS) vulnerability. It is recommended to handle these cases more gracefully to prevent unexpected crashes for users of the library.

// we wouldn’t want to encode null here, that’s different, and we can’t throw an
// error either. doing nothing would still produce invalid JSON though. so trapping
// is the least bad behavior.
fatalError("undefined is not a valid JSON value")

Choose a reason for hiding this comment

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

security-medium medium

The encode(to:) method triggers a fatalError when it encounters a .undefined value. This presents a Denial of Service (DoS) vulnerability, as JSObject returns .undefined for non-existent properties, and encoding such an object will crash the application. To prevent crashes, consider omitting properties with .undefined values during encoding, aligning with standard JavaScript JSON.stringify behavior. Alternatively, encode(to:) could be made a throwing function to allow for graceful error handling by callers.

return .number(double)
} else {
// this should have never passed parser validation in the first place
fatalError("Unable to reparse JSON number to Double?!")

Choose a reason for hiding this comment

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

security-medium medium

The number(parsing:) method uses fatalError if it fails to parse a string as a Double. While the comment suggests this should be unreachable due to prior parser validation, any inconsistency between the JSON parser's number validation and Double.init(_:) could allow an attacker to provide a specially crafted JSON number that causes the application to crash. It is safer to throw an error or return a default value instead of trapping.

@tayloraswift tayloraswift merged commit aa04304 into master Jan 23, 2026
6 of 8 checks passed
@tayloraswift tayloraswift deleted the encode-jsobject branch January 23, 2026 19:46
tayloraswift added a commit that referenced this pull request Mar 15, 2026
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.

1 participant