-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Cannot find crypto in Cloudflare Worker #5
Comments
Hi @ryan-mars - Thanks again for looking into the cloudflare worker functionality. At this time I can absolutely understand why you forked the library - I don't agree with Cloudflare's implementation of their basic JS APIs and therefore won't be supporting Cloudflare workers. I have updated the PRNG detection, which I hope would now work on Cloudflare workers, but due to the time being irrelevant, somewhat, I wouldn't ever advise anyone to use I've added a statement to the readme to clarify that they're not supported, at least for now. Perhaps in the future if they change their stance this library might function correctly. Cheers. |
And also your comment in this now closed PR:
and
If your concern was with how Cloudflare Workers handle time (time freezes at the time of the request), I would suggest you need to understand why they made that choice and why it was important (and should perhaps be emulated and not derided). This was done to help prevent side-channel attacks and Spectre style attacks. This page has a lot of additional context on security choices as well. Worker time can certainly be relied upon, it just reflects the start of the current request, not time changes during a request. As a result they state:
As a worker is an instance of a V8 isolate, the time is only frozen for the duration of a request, which is typically a small number of Some additional info that might help: https://developers.cloudflare.com/workers/learning/how-workers-works |
@grempe I disagree purely because it's such a fundamental attribute of the environment, and one which this (and many other) libraries rely upon. I am not deriding Cloudflare's efforts to improve security, but their chosen mitigation is one I don't see as being overly friendly to time-sensitive libraries such as this. I'd happily support Cloudflare workers in this fork, if:
Doesn't this mean that, when generating ULIDs during that time period, the date-time portion will remain static? If so, I'm afraid my response is one simply of necessity.. I don't see a way around this limitation. Besides this timing restriction I guess the library should just work after the most recent update (crypto detection).. |
Yes that is correct, which is why only monotonic factories should be used for generating date encoding id's on Cloudflare Workers. Still this is not great in a distributed system for obvious reasons. |
So that people can have a better idea of how this works in the real environment, and the caveat that applies, please see this repo I created this morning. It allows anyone to generate ULIDs (up to 1000 per request) with a simple It also allows you to choose the https://github.com/truestamp/ulid-generator By visiting https://ulid.truestamp.com/?q=10 you can see that the timestamp component of the generated ULIDs reflects the request time (new for each request), but this has no effect on the ability to generate I wrote up a caveat section in the README that describes when this might be a problem for your use case (in which case, please do generate your ULIDs somewhere else). In most use cases I think this is likely irrelevant. In any case, it is not really a concern of the library code which has no control over the time accuracy of the clock in the runtime environment it is running in. NodeJS does not solve this problem for you if e.g. your Docker container or virtual host is reporting an inaccurate or stale time. If you have proposed changes to any of that please do let me know. The code for the whole API server (<50 LOC) can be found here: https://github.com/truestamp/ulid-generator/blob/main/src/handler.ts @perry-mitchell : please feel free to use this if you think its a helpful demo of your library. And please turn on issues for your repo. :-) Cheers. |
Thanks @grempe, I appreciate it. I'll keep an eye on this. I suppose then advising people to use the monotonic factory only on workers is probably enough to ensure somewhat normal operation.
Aren't we discussing in an issue? 😅 |
I'm not sure you even need the monotonic. But it can't hurt I think.
I was referring to @ryan-mars repo issues which is why I tagged him in that line. 😁 |
@grempe Fixed, please add your issues. https://github.com/ryan-mars/ulid-workers/issues |
@perry-mitchell maybe need to update the README.md ? |
@bluet I'd accept a PR for that if you're interested in documenting :) |
I'd be happy for https://github.com/ryan-mars/ulid-workers to no longer be necessary. |
@ryan-mars Its been some time since your project was released- was there anything special regarding Cloudflare worker support you had to implement? I took a brief look at the source and saw there was no standard I'd be willing to have the changes made here to support workers. |
It would also be great to have tests for this, but not sure how doable that is without at least paying for cloudflare workers. Having some kind of safety for not allowing the |
New feature @ #13 for official support. |
@perry-mitchell two major things: First, obviously, non-monotonic generation may result in unpredictable behavior including the potential that ULIDs will lexically sort in a different order than they were generated. Second, an issue without an obvious solution, monotonic generation of ULIDs near max entropy can roll over thereby incrementing the timestamp by 1ms |
This makes sense and we could probably throw in this situation, potentially, or just document this well.
@ryan-mars I'm confused, the last comment by @grempe in that issue seemed to hint that there was no such problem - the time component shouldn't be affected.. Is there something I'm missing here?
There's a lot on conjecture in that issue and I'm not sure I see the whole picture. I know that this library's code is working nominally in standard expected environments, but is it currently working as such within cloudflare workers using monotonic generation, or not? Will the time component roll over if I generate a ton of ulids using the monotonic factory, or will it throw an exception? I haven't had a recent look at this code even in this repo.. But I suspect that it'll just throw - if that's the case it is, in my opinion, operating as expected. Summa summarum I think throwing an error is preferrable to inaccurate/invalid values. If the monotonic factory works otherwise perhaps we could move forward with such a solution here. |
@perry-mitchell For the record, I never quite understood if the concerns in that issue were realistic. I think things are fine as you have them. |
When deploying a Cloudflare worker I get an error.
Somewhere in
detectPRNG()
it's not finding thatcrypto
is on globalThis in the Worker, so it is falling through torequire("crypto")
which doesn't work in Cloudflare Workers.ulidx/source/ulid.ts
Lines 63 to 65 in 578a98b
The text was updated successfully, but these errors were encountered: