Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bug (v10.x) - Error: Cannot assign to read only property 'lastResponse' of object #1796

Closed
titanism opened this issue May 21, 2023 · 6 comments
Labels

Comments

@titanism
Copy link

Describe the bug

Error: Cannot assign to read only property 'lastResponse' of object

To Reproduce

I think it happens here:

node_modules/stripe/lib/StripeResource.js
102:    // lastResponse.
221:            Object.defineProperty(jsonResponse, 'lastResponse', {

Expected behavior

This should not throw an error.

Code snippets

No response

OS

macOS

Node version

Node v18

Library version

stripe-node 10.17.0

API version

2020-08-27

Additional context

No response

@titanism titanism added the bug label May 21, 2023
@pakrym-stripe
Copy link
Contributor

Hi @titanism,

Does the error reproduce on the latest version of the library? Can you share some code that reproduces the error?

@titanism
Copy link
Author

We cannot reproduce on latest version and it is hard to reproduce, but this definitely happens and has since probably v0.8.

titanism added a commit to forwardemail/forwardemail.net that referenced this issue May 23, 2023
@pakrym-stripe
Copy link
Contributor

I'm sorry @titanism but without a stack trace, a repro and with the fact that it doesn't reproduce on latest there isn't much we can do with the issue you are seeing.

@titanism
Copy link
Author

titanism commented May 23, 2023

We've been able to reproduce this and the exact cause, will report back with complete findings soon.

Note this only applies if you are doing things like creating a new object and re-assigning properties (since Object.defineProperty is used it is read-only and throws an error on assignment).

@titanism
Copy link
Author

The issue occurs when you are trying to assign the property lastResponse, which is an IncomingMessage (stream), e.g. https://github.com/strikeentco/set/blob/b2f942c29652066e484436a8cb0032932f085e9a/main.js#L17.

The issue is fixed by changing this code:

+jsonResponse.lastResponse = rawResponse;
-Object.defineProperty(jsonResponse, 'lastResponse', {
-  enumerable: false,
-  writable: false,
-  value: rawResponse,
});
+// Object.defineProperty(jsonResponse, 'lastResponse', {
+//   enumerable: false,
+//   writable: false, // NOTE: this is already set to `false` when using `defineProperty`
+//   value: rawResponse,
+// });

Per

Object.defineProperty(jsonResponse, 'lastResponse', {
enumerable: false,
writable: false,
value: rawResponse,
. Note that this is the only scenario that uses this behavior in the codebase right now.

Note that writable is already set to false when using defineProperty, so that line could potentially be removed.

@titanism
Copy link
Author

Closing this ticket, but you may want to re-open or patch this per the above diff as it is common practice for loggers to strip/sanitize/parse/stringify objects. The culprit of this bug was from doing logger.info('stripe checkout session created', { session }) where session was an object returned from const session = await stripe.checkout.sessions.create(options);.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants