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 backslash in environment key freeze app [INS-4157] #7763

Merged
merged 11 commits into from
Aug 7, 2024

Conversation

yaoweiprc
Copy link
Contributor

@yaoweiprc yaoweiprc commented Jul 26, 2024

Close #7286
objectPath.normalize will enter endless loop if last char in key name is backslash, just remove backslashes at the end of key name.
It's just a workaround. It will introduce problems when reference an environment variable which's name ends with backslash.
image
image

return objectPath.normalize(prefix);
// objectPath.normalize will enter endless loop if last char in key name is backslash, just remove backslashes at the end of key name
// workaround for https://github.com/Kong/insomnia/issues/7286
return objectPath.normalize(prefix.replace(/\\+']/g, '\']'));
Copy link
Member

@filfreire filfreire Jul 26, 2024

Choose a reason for hiding this comment

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

(EDIT)

Maybe we prevent users from entering the backslash altogether

filfreire
filfreire previously approved these changes Jul 26, 2024
Copy link
Member

@filfreire filfreire left a comment

Choose a reason for hiding this comment

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

I think this is OK as is.

There's more options:

  • we could prevent users inputting backslash
  • or delete it from the keys when saving environment
  • or we display an error and don't save environment, like when we forget a , or a " on json, not allowing users to save environment until they delete \ present in keys
  • or we keep your approach as is, and accept this as lesser evil - between crashing the app and leaving environment key dangling, this is good enough for now
  • something else?

@filfreire filfreire requested a review from a team July 26, 2024 09:20
@jackkav
Copy link
Contributor

jackkav commented Jul 26, 2024

Could this be done without using a regex, would it be more readable?

@notjaywu notjaywu marked this pull request as draft August 1, 2024 02:38
@yaoweiprc yaoweiprc marked this pull request as ready for review August 5, 2024 15:58
@yaoweiprc
Copy link
Contributor Author

This problem is caused by objectpath library, in its parse function, it could enter endless loop when the last char in key is backslash. It seems to be no longer maintained.
The previous commit was just a workaround to delete trailing backslashes from key.
In this commit I copied its source code to packages\insomnia\src\templating\third_party\objectPath.ts and fixed the bug within line 58 to 80.

Copy link
Contributor

@jackkav jackkav left a comment

Choose a reason for hiding this comment

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

this looks like it would work, but a step deeper would be attempting to eliminate the library completely and just reimplement the two functions use cases with less code, and eliminate the while loop that causes the problem. Not required for me to approve this PR but worth considering if you like a challenge. 🎸

EDIT: Actually there are two things that could push you a little to improve this PR

  • write a couple unit tests for forceBracketNotation and normalizeToDotAndBracketNotation
  • attempt to simplify the object path implementation in the pr

@yaoweiprc yaoweiprc merged commit 633dec9 into develop Aug 7, 2024
8 checks passed
@yaoweiprc yaoweiprc deleted the fix/backslash-in-environment-key-freeze-app branch August 7, 2024 03:38
@yaoweiprc
Copy link
Contributor Author

I need some time to familiarize myself with Vitest. I just merged this PR, I'll improve in other threads.

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.

Double backslash at end of Environment JSON key freezes entire app, corrupts collection
3 participants