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

Generate secure token using crypto rand #605

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rumanzo
Copy link
Contributor

@rumanzo rumanzo commented Mar 6, 2024

rand package https://pkg.go.dev/math/rand notice us
Package rand implements pseudo-random number generators suitable for tasks such as simulation, but it should not be used for security-sensitive work.
I purpose use crypto/rand function that's more secure

result = string(SYMBOLS[x]) + result
x, err := rand.Int(rand.Reader, big.NewInt(int64(len(SYMBOLS))))
if err != nil {
log.Fatal("Failed to generate token")
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should return an error and gracefully fail the request with a reason to the user

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we just ignore err in this case?
I really doubt that we can get error in this case ever using rand.Reader
https://cs.opensource.google/go/go/+/master:src/crypto/rand/util.go;l=85
And we defenetly won't panic there

Copy link
Collaborator

Choose a reason for hiding this comment

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

digging further, that's on unix system:
https://cs.opensource.google/go/go/+/master:src/crypto/rand/rand_unix.go;l=62
https://cs.opensource.google/go/go/+/master:src/crypto/rand/rand_unix.go;l=72

It's not excluded 100% that they won't return an error.

Indeed I guess that on our docker image (that's stratch based) the first will always return an error: do you mind trying to build the docker image from your branch and test?

thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed I guess that on our docker image (that's stratch based) the first will always return an error: do you mind trying to build the docker image from your branch and test?
I tried run scratch image with dropped all capabilites, and it works. But i can do something like fallback

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can reproduce and this seems to be the reason:

// On Linux, FreeBSD, Dragonfly, NetBSD and Solaris, Reader uses getrandom(2) if
// available, /dev/urandom otherwise.

but, please, note:

// On other Unix-like systems, Reader reads from /dev/urandom.

so I guess on other Unix-like systems it won't work.

not sure how to test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can reproduce and this seems to be the reason:
how?

so I guess on other Unix-like systems it won't work.
on other unix-like system should be /dev/urandom, but what another unix-like system? HP-UX may be?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can reproduce and this seems to be the reason: how?

so I guess on other Unix-like systems it won't work. on other unix-like system should be /dev/urandom, but what another unix-like system? HP-UX may be?

let's check the error and panic with a message with the link to this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can reproduce and this seems to be the reason: how?
so I guess on other Unix-like systems it won't work. on other unix-like system should be /dev/urandom, but what another unix-like system? HP-UX may be?

let's check the error and panic with a message with the link to this PR

What you mean? How?

Copy link
Collaborator

Choose a reason for hiding this comment

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

What you mean? How?

nevermind!

see my last suggestion please :)

@aspacca
Copy link
Collaborator

aspacca commented Mar 7, 2024

hello @rumanzo , thanks for the PR, please see my comment

Copy link
Collaborator

@aspacca aspacca left a comment

Choose a reason for hiding this comment

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

please, see my comment

result = string(SYMBOLS[x]) + result
x, err := rand.Int(rand.Reader, big.NewInt(int64(len(SYMBOLS))))
if err != nil {
log.Fatal("Failed to generate token")
Copy link
Collaborator

Choose a reason for hiding this comment

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

digging further, that's on unix system:
https://cs.opensource.google/go/go/+/master:src/crypto/rand/rand_unix.go;l=62
https://cs.opensource.google/go/go/+/master:src/crypto/rand/rand_unix.go;l=72

It's not excluded 100% that they won't return an error.

Indeed I guess that on our docker image (that's stratch based) the first will always return an error: do you mind trying to build the docker image from your branch and test?

thanks!

@rumanzo
Copy link
Contributor Author

rumanzo commented Mar 29, 2024

please see my comment in code review

x := rand.Intn(len(SYMBOLS) - 1)
result = string(SYMBOLS[x]) + result
x, err := rand.Int(rand.Reader, big.NewInt(int64(len(SYMBOLS))))
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if err != nil {
if err != nil {
// log an error entry and fallback to `math/rand`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, will do it later. Should I pass logger into function?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, thanks

you should pass a null logger in the test: https://github.com/dutchcoders/transfer.sh/blob/main/server/token_test.go

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.

None yet

2 participants