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

Construct a ByteArrayInputStream from a ByteString without copying bytes or using UnsafeByteStringOperations #291

Open
mgroth0 opened this issue Apr 5, 2024 · 2 comments
Labels
enhancement integration An issue related to the integration with other libraries or platform-specific APIs

Comments

@mgroth0
Copy link

mgroth0 commented Apr 5, 2024

Creating a ByteArrayInputStream from a ByteString gives us an InputStream, which is used in many contexts in java. My understanding is that it is completely safe, since a ByteArrayInputStream only reads the underlying ByteArray and does not write to it.

Currently, I believe there are only two ways to get a ByteArrayInputStream from a ByteString:

  1. Use UnsafeByteStringOperations
  2. Copy the bytes

I am wondering if the library could provide a method to get an InputStream (it doesn't necessarily have to be a ByteArrayInputStream, but why not) that is declared safe and doesn't copy bytes.

@fzhinkin
Copy link
Collaborator

fzhinkin commented Apr 5, 2024

It was not considered previously, but in the same way Java stdlib provides StringReader for String, we can also provide an InputStream-view over ByteString.

My understanding is that it is completely safe, since a ByteArrayInputStream only reads the underlying ByteArray and does not write to it.

It depends: ByteArrayInputStream itself is safe, but its transferTo method exposes internal byte array to an OutputStream which, if implemented maliciously, can mutate array's content.

@fzhinkin fzhinkin added enhancement integration An issue related to the integration with other libraries or platform-specific APIs labels Apr 5, 2024
@mgroth0 mgroth0 changed the title Construct a ByteArrayInputStream from a ByteString without copying bytes or using Construct a ByteArrayInputStream from a ByteString without copying bytes or using UnsafeByteStringOperations Apr 5, 2024
@mgroth0
Copy link
Author

mgroth0 commented Apr 5, 2024

ByteArrayInputStream itself is safe, but its transferTo method exposes internal byte array

Thanks for pointing that out. I was unsure if there might be some vulnerability like that.

Maybe this library can implement a custom InputStream which wraps around ByteArrayInputStream, but more more strictly preventing the ByteArray from leaking out?

I guess this might mean making transferTo much less efficient, either by copying bytes or by transfering them one at a time. I am curious what is the best solution here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement integration An issue related to the integration with other libraries or platform-specific APIs
Projects
None yet
Development

No branches or pull requests

2 participants