Skip to content
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

fix: TypeError on creating properties on immutable objects #671

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/conform-dom/formdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,37 @@ export function setValue(
: {}
: valueFn(pointer[key]);

pointer[key] = newValue;
if (canAddProperty(pointer, key)) {
pointer[key] = newValue;
}
pointer = pointer[key];
}
}

function canAddProperty(
obj: Object | Array<unknown>,
property: string | number,
): boolean {
const propertyStr =
typeof property === 'number' ? property.toString() : property;
if (Object.prototype.hasOwnProperty.call(obj, propertyStr)) {
const descriptor = Object.getOwnPropertyDescriptor(obj, propertyStr);
if (descriptor && !descriptor.writable) {
return false;
}
}
if (!Object.isExtensible(obj)) {
return false;
}
if (Object.isSealed(obj) && !(propertyStr in obj)) {
return false;
}
if (Object.isFrozen(obj) && !(propertyStr in obj)) {
return false;
}
return true;
}

Comment on lines +138 to +161
Copy link
Owner

Choose a reason for hiding this comment

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

I wanna understand better the impact of this change. Can you explain a bit more what each condition is trying to handle?

Copy link
Author

@novikovfred novikovfred Jun 17, 2024

Choose a reason for hiding this comment

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

As i mentioned in #670 (we can reproduce with FormData as well)currently we can get TypeError if we will try to assign property on immutable object.
Since we validate and get data from those parameters that can be passed by users, including frauders. We wouldn't want to get errors in runtime error because of a user who enters intentionally incorrect data.
If frauder will try to create any param with immutable type at first(example: param=value) and then will try to add some property to it (example: param.child=value) we will get a TypeError.
Of course, I can handle the error, but I would like not to get errors in the application's runtime.

If we wanna support changes of property values i can rework my PR a bit.

With these change I want to check properly if we can add a property to a given object.

  Check if the property already exists and is not writable
  if (Object.prototype.hasOwnProperty.call(obj, propStr)) {
    const descriptor = Object.getOwnPropertyDescriptor(obj, propStr);
    if (descriptor && !descriptor.writable) {
      return false;
    }
  }

  Check if the object/array is extensible, isExtensible returns a value that indicates whether new properties can be added to an object.
  if (!Object.isExtensible(obj)) {
    return false;
  }

  Check if the object/array is sealed and property doesn't exist, isSealed returns true if existing property attributes cannot be modified in an object and new properties cannot be added to the object.
  if (Object.isSealed(obj) && !(propStr in obj)) {
    return false;
  }

  Check if the object/array is frozen and property doesn't exist, isFrozen returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.
  if (Object.isFrozen(obj) && !(propStr in obj)) {
    return false;
  }

/**
* Retrive the value from a target object by following the paths
*/
Expand Down
Loading