Skip to content

diffJson throws on truthy, non-callable toJSON #699

Description

@aforemendude

diffJson would call toJSON when it's truthy, even if it's not callable. This can cause diffJson to throw on valid JSON objects. JSON.stringify only calls toJSON if it's callable.

Reproduced on the latest v9.0.0. Happening to the latest master as well.

Reproduction:

https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-non-callable-tojson

import { diffJson } from 'diff'

const oldObject = JSON.parse('{"toJSON":"old"}')
const newObject = JSON.parse('{"toJSON":"new"}')

try {
  console.log(diffJson(oldObject, newObject))
} catch (error) {
  console.log(`${error.name}: ${error.message}`)
}

Output:

TypeError: obj.toJSON is not a function

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions