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

perf: use crypto and ESM #3

Closed
wants to merge 1 commit into from

Conversation

ThaUnknown
Copy link
Contributor

@ThaUnknown ThaUnknown commented Jul 30, 2022

note: #2 needs to be merged first!

  • replaces the not random randomstring with the crypto API [which was already used by randombytes], a drawback of this is that it doesn't specify the string length, [is this needed?]
    - replaces some deps with the built-in atob/btoa which is deprecated in node, but is supported in browsers and is consistent with CF's implementation
  • drops require making the module itself ESM compliant

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Wow, thanks! Will review this asap, see reply Re: #2

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

The string length doesn’t matter for the session id but we’ll need to confirm the UUIDs comply with the spec requirements for the username frag and password for ICE.

@ThaUnknown
Copy link
Contributor Author

The string length doesn’t matter for the session id but we’ll need to confirm the UUIDs comply with the spec requirements for the username frag and password for ICE.

I didn't test that, didn't think much of it, but that should be looked at considering i havent

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

@ThaUnknown
Copy link
Contributor Author

Here’s the RFC https://datatracker.ietf.org/doc/html/rfc5245#section-15.4

- is rejected, my bad

@ThaUnknown ThaUnknown force-pushed the base-hex-random branch 2 times, most recently from 78e3972 to 24b3048 Compare July 30, 2022 14:43
@ThaUnknown ThaUnknown changed the title fix: use crypto and atob/btoa perf: use crypto and atob/btoa Jul 30, 2022
@ThaUnknown
Copy link
Contributor Author

this is broken, dont merge!

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Noticed the package has process again - I think that is an unintentional revert?

@ThaUnknown
Copy link
Contributor Author

Noticed the package has process again - I think that is an unintentional revert?

yes, which is why the > dont merge
sorry

Copy link
Owner

@gfodor gfodor left a comment

Choose a reason for hiding this comment

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

DTLS fingerprints are hex encoded, so that conversion has to be maintained

src/p2pcf.js Outdated Show resolved Hide resolved
src/p2pcf.js Outdated Show resolved Hide resolved
@ThaUnknown ThaUnknown changed the title perf: use crypto and atob/btoa perf: use crypto and ESM Jul 30, 2022
@ThaUnknown
Copy link
Contributor Author

this now works x)

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Oh, one thing that may have been missed - I was using random-string not randomstring for the reasons you mentioned (the randombytes dep in particular seemed bad.)

https://github.com/valiton/node-random-string/blob/master/lib/random-string.js

In light of that does this PR still make sense? The actual routine has no dependencies afaik.

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Btw that change was made recently so might mean we could drop some of the esbuild browserify bits. I think those get tree shaken out but def worth seeing if that helps to remove the ones not needed by simple-peer.

@ThaUnknown
Copy link
Contributor Author

Oh, one thing that may have been missed - I was using random-string not randomstring for the reasons you mentioned (the randombytes dep in particular seemed bad.)

https://github.com/valiton/node-random-string/blob/master/lib/random-string.js

In light of that does this PR still make sense? The actual routine has no dependencies afaik.

random-string isnt random, simple-peer uses randombytes anyways

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Ah ok - so rn this PR drops that dep and improves the security of the ICE creds?

I’m a little worried about randombytes since it drops into an infinite loop if Buffer is not in the global namespace :/ I figured people will have browserify or esbuild packing it but it is quite a gnarly failure mode. (There is a while(true) with a try/catch!)

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Any idea when simple-peer calls randombytes? I can dig into it later, just wondering if you knew already.

@evan-brass
Copy link

Not sure if this is helpful, but this is what I used to generate the ufrag and password in my tests:

function gen_random_ice_str(byte_len) {
	const bytes = crypto.getRandomValues(new Uint8Array(byte_len));
	const byte_str = bytes.reduce((accum, v) => accum+String.fromCharCode(v), '');
	return btoa(byte_str).replaceAll('=', '');
}

ICE ufrag is a minimum of 3 bytes of entropy and the ICE password is a minimum of 16 bytes of entropy. So

const ufrag = gen_random_ice_str(3);
const password = gen_random_ice_str(16)

@ThaUnknown
Copy link
Contributor Author

Any idea when simple-peer calls randombytes? I can dig into it later, just wondering if you knew already.

when generating random id/channel name

’m a little worried about randombytes since it drops into an infinite loop if Buffer is not in the global namespace :/ I figured people will have browserify or esbuild packing it but it is quite a gnarly failure mode. (There is a while(true) with a try/catch!)

yeah this is where I was heading with all my changes, to drop the need to polyfill anything at all :)

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Ah cool, yeah it would be ideal to drop them and if not avoid calling into them. We can pass those into simple peer.

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

And yep that is a good catch @evan-brass - the per character entropy of the current code isn’t the entire byte so my lengths are too conservative.

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

I think these changes would be good:

  • fix the entropy issue as per @evan-brass ’s approach

  • avoid call paths to randombytes by passing in safely generated ids to simple-peer

  • top comment mentioned something about a require making it a non-esm, didn’t see that here, just checking

  • remove any unnecessary browserify deps for local esbuild (these still may be necessary due to simple-peer)

@ThaUnknown
Copy link
Contributor Author

ThaUnknown commented Jul 30, 2022

avoid call paths to randombytes by passing in safely generated ids to simple-peer

not possible, you'd need to drop simple-peer to do that

remove any unnecessary browserify deps for local esbuild (these still may be necessary due to simple-peer)

they are all necessary for simple-peer as it uses node:event node:buffer node:process and node:stream

this is why i suggested polite-peer instead as it's dependency free, just a little bit more bare bones

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Ah I see that the _id can’t be avoided - too bad

@ThaUnknown
Copy link
Contributor Author

I think these changes would be good:

  • fix the entropy issue as per @evan-brass ’s approach
  • avoid call paths to randombytes by passing in safely generated ids to simple-peer
  • top comment mentioned something about a require making it a non-esm, didn’t see that here, just checking
  • remove any unnecessary browserify deps for local esbuild (these still may be necessary due to simple-peer)

should be done

@gfodor
Copy link
Owner

gfodor commented Jul 30, 2022

Thank you! Headed to airport but will test this tonight and merge if it looks good. Appreciate the PR!

@gfodor
Copy link
Owner

gfodor commented Jul 31, 2022

Pulled into #3, thank you for the PR!

@gfodor gfodor closed this Jul 31, 2022
gfodor added a commit that referenced this pull request Jul 31, 2022
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.

3 participants