-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CSP inline script tag error in Console HTML template
- Loading branch information
1 parent
5e8c422
commit e69f6ff
Showing
6 changed files
with
75 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
) | ||
|
||
// Generate a cryptographically secure random array of bytes. | ||
func RandomBytes(length int) ([]byte, error) { | ||
bytes := make([]byte, length) | ||
_, err := rand.Read(bytes) | ||
return bytes, err | ||
} | ||
|
||
// Generate a cryptographically secure random string. | ||
// Returned string is encoded using [encoding.RawURLEncoding] | ||
// which makes it safe to use in URLs and file names. | ||
func RandomString(length int) (string, error) { | ||
encoding := base64.RawURLEncoding | ||
// each byte (8 bits) gives us 4/3 base64 (6 bits) characters, | ||
// we account for that conversion and add one to handle truncation | ||
b64size := encoding.DecodedLen(length) + 1 | ||
randomBytes, err := RandomBytes(b64size) | ||
// trim down to the original requested size since we added one above | ||
return encoding.EncodeToString(randomBytes)[:length], err | ||
} |