-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ca5c89f
27c38e6
6fc33ad
e069bd2
0fbe374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ THE SOFTWARE. | |
package server | ||
|
||
import ( | ||
"math/rand" | ||
"crypto/rand" | ||
"log" | ||
"math/big" | ||
) | ||
|
||
const ( | ||
|
@@ -35,11 +37,14 @@ const ( | |
|
||
// generate a token | ||
func token(length int) string { | ||
result := "" | ||
result := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
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 { | ||
log.Fatal("Failed to generate token") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just ignore err in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. digging further, that's on unix system: 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can reproduce and this seems to be the reason:
but, please, note:
so I guess on other Unix-like systems it won't work. not sure how to test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
let's check the error and panic with a message with the link to this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What you mean? How? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
nevermind! see my last suggestion please :) |
||
} | ||
result[i] = SYMBOLS[x.Int64()] | ||
} | ||
|
||
return result | ||
return string(result) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it. But error message in logger in this function provide three error messages in log, because token calls several times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aspacca check pls