Skip to content

Ensure JSON Key Path is an Actual File or IO Object#134

Merged
sabman merged 5 commits into
decision-labs:masterfrom
cacheflow:prevent-opening-json-key-path-unless-valid-io-object
May 1, 2026
Merged

Ensure JSON Key Path is an Actual File or IO Object#134
sabman merged 5 commits into
decision-labs:masterfrom
cacheflow:prevent-opening-json-key-path-unless-valid-io-object

Conversation

@cacheflow
Copy link
Copy Markdown
Contributor

@cacheflow cacheflow commented Dec 18, 2024

Currently, in the json_key method it assumes that json_key_path will be an IO-like object or filename. This can cause an issue where if someone accidentally passes credentials in plaintext or a non-IO object, the Gem will attempt to open the credentials and display the credentials in plain text.

This PR resolves #133.

@cacheflow cacheflow force-pushed the prevent-opening-json-key-path-unless-valid-io-object branch 2 times, most recently from dcc792e to bb13556 Compare December 18, 2024 07:02
@cacheflow cacheflow changed the title Prevent Opening JSON Key Path unless It's an IO Object Prevent Opening JSON Key Path unless it's an IO Object Dec 18, 2024
@cacheflow cacheflow changed the title Prevent Opening JSON Key Path unless it's an IO Object Ensure JSON Key Path is an Actual File or IO Object Dec 18, 2024
@cacheflow
Copy link
Copy Markdown
Contributor Author

Hey @sabman or @erimicel, do you all think this would be a good addition to the library? On one project, I worked on we encountered an IO error that caused a credential to leak out unintentionally, so I created this PR to ensure the JSON key path is an IO like object. Any feedback would be greatly appreciated.

@cacheflow
Copy link
Copy Markdown
Contributor Author

Hey @sabman or @erimicel, do you all think this would be helpful to integrate? I appreciate any feedback!

Copy link
Copy Markdown
Contributor

@erimicel erimicel left a comment

Choose a reason for hiding this comment

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

Tests look good 👍

Comment thread lib/fcm.rb

max_path_len = 1024
valid_path = path.is_a?(String) && path.length <= max_path_len
valid_path && File.file?(path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: You might be able to simplify this as :

def valid_json_key_path?(path)
    return false unless path.is_a?(String) && path.length <= 1024
                                                                                
    File.file?(path)
  end 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I'm going to keep it the way it is because I think the use of false and unless can be confusing due to the negative return value and negative check.

Comment thread lib/fcm.rb Outdated
elsif valid_json_key_path?(@json_key_path)
File.open(@json_key_path)
else
credentials_error_msg(@json_key_path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: credentials_error_msg is a bit sounds misleading since it raises an exception rather than returning a message

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree - we can probably rename this at some point and/or add an ! to the function name too.

@erimicel
Copy link
Copy Markdown
Contributor

Hey @sabman or @erimicel, do you all think this would be helpful to integrate? I appreciate any feedback!

Hi @cacheflow, changes looks good and make sense for me, but I can't merge or release new patch for it, you have to wait @sabman

@cacheflow
Copy link
Copy Markdown
Contributor Author

@erimicel, I made the change. Tell me what you think about the new method name.

@cacheflow
Copy link
Copy Markdown
Contributor Author

@sabman is there a way we can merge this into master/main and then add it to the next release?

@sabman
Copy link
Copy Markdown
Member

sabman commented Apr 15, 2026

Will review shortly

@sabman
Copy link
Copy Markdown
Member

sabman commented Apr 15, 2026

Running CI

@cacheflow
Copy link
Copy Markdown
Contributor Author

Running CI

Just following up on this. Did the CI pass? If so, I'd love to get this merged and released in the next version so folks don't face the same issue we once did at work.

@cacheflow
Copy link
Copy Markdown
Contributor Author

Hey @sabman was the history erased related to hound bot in this PR? I can't find the comments anymore.

Screenshot 2026-04-30 at 11 57 46 PM Screenshot 2026-04-30 at 11 57 55 PM

@sabman sabman merged commit 60f6988 into decision-labs:master May 1, 2026
5 checks passed
@sabman
Copy link
Copy Markdown
Member

sabman commented May 1, 2026

merged - cutting release now

sabman added a commit that referenced this pull request May 1, 2026
- Ensure JSON key path is an actual file or IO object before opening [#134](#134)
@sabman
Copy link
Copy Markdown
Member

sabman commented May 1, 2026

Hey @sabman was the history erased related to hound bot in this PR? I can't find the comments anymore.

Not sure GH hasn't been its usual self lately. Regardless tests pass so I'm merging. Thanks for the PR and code review (@erimicel also) ❤️ I also just cut a new gem.

@cacheflow
Copy link
Copy Markdown
Contributor Author

Thanks @sabman for the merge and @erimicel for the review. Appreciate the help moving this forward. 🎉

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.

Prevent Opening JSON Key Path unless It's an IO Object

3 participants