Skip to content

Commit

Permalink
chore: remove reaming panic in crypto and backward package
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 1, 2024
1 parent dcc451a commit 804a164
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
24 changes: 15 additions & 9 deletions registry/backward/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ func (bcr *BackwardCompatibilityRegistry) Fail(message string) (*uint, error) {
//
// map[string]any - a map containing the URL components: "scheme", "host",
// "hostname", "path", "query", "opaque", "fragment", and "userinfo".
// error - an error object if the URL string is invalid.
//
// Example:
//
// {{ "https://example.com/path?query=1#fragment" | urlParse }} // Output: map[fragment:fragment host:example.com hostname:example.com path:path query:query scheme:https]
func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) map[string]any {
func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, error) {
dict := map[string]any{}
parsedURL, err := url.Parse(v)
if err != nil {
panic(fmt.Sprintf("unable to parse url: %s", err))
return dict, fmt.Errorf("unable to parse url: %s", err)

Check warning on line 53 in registry/backward/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/backward/functions.go#L53

Added line #L53 was not covered by tests
}
dict["scheme"] = parsedURL.Scheme
dict["host"] = parsedURL.Host
Expand All @@ -64,7 +65,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) map[string]any {
dict["userinfo"] = ""
}

return dict
return dict, nil
}

// ! DEPRECATED: This should be removed in the next major version.
Expand All @@ -78,11 +79,12 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) map[string]any {
// Returns:
//
// string - the constructed URL string.
// error - an error object if the URL components are invalid.
//
// Example:
//
// {{ dict scheme="https" host="example.com" path="/path" query="query=1" opaque="opaque" fragment="fragment" | urlJoin }} // Output: "https://example.com/path?query=1#fragment"
func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) string {
func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, error) {

resURL := url.URL{
Scheme: bcr.get(d, "scheme").(string),
Expand All @@ -97,13 +99,13 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) string {
if userinfo != "" {
tempURL, err := url.Parse(fmt.Sprintf("proto://%s@host", userinfo))
if err != nil {
panic(fmt.Sprintf("unable to parse userinfo in dict: %s", err))
return "", fmt.Errorf("unable to parse userinfo in dict: %s", err)

Check warning on line 102 in registry/backward/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/backward/functions.go#L102

Added line #L102 was not covered by tests
}
user = tempURL.User
}

resURL.User = user
return resURL.String()
return resURL.String(), nil
}

// ! DEPRECATED: This should be removed in the next major version.
Expand All @@ -116,13 +118,17 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) string {
// Returns:
//
// string - a randomly selected IP address associated with the hostname.
// error - an error object if the hostname cannot be resolved.
//
// Note: This function currently lacks error handling
//
// Example:
//
// {{ getHostByName "example.com" }} // Output: "237.84.2.178"
func (bcr *BackwardCompatibilityRegistry) GetHostByName(name string) string {
addrs, _ := net.LookupHost(name)
return addrs[rand.Intn(len(addrs))]
func (bcr *BackwardCompatibilityRegistry) GetHostByName(name string) (string, error) {
addrs, err := net.LookupHost(name)
if err != nil {
return "", fmt.Errorf("unable to resolve hostname: %s", err)
}

Check warning on line 132 in registry/backward/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/backward/functions.go#L131-L132

Added lines #L131 - L132 were not covered by tests
return addrs[rand.Intn(len(addrs))], nil
}
34 changes: 19 additions & 15 deletions registry/crypto/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (
// Example:
//
// {{ "Hello World" | bcrypt }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"
func (ch *CryptoRegistry) Bcrypt(input string) string {
func (ch *CryptoRegistry) Bcrypt(input string) (string, error) {
hash, err := bcrypt_lib.GenerateFromPassword([]byte(input), bcrypt_lib.DefaultCost)
if err != nil {
return fmt.Sprintf("failed to encrypt string with bcrypt: %s", err)
return "", fmt.Errorf("failed to encrypt string with bcrypt: %s", err)
}

return string(hash)
return string(hash), nil
}

// Htpasswd generates an Htpasswd hash from the given username and password strings.
Expand All @@ -51,11 +51,15 @@ func (ch *CryptoRegistry) Bcrypt(input string) string {
// Example:
//
// {{ htpasswd "username" "password" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"
func (ch *CryptoRegistry) Htpasswd(username string, password string) string {
func (ch *CryptoRegistry) Htpasswd(username string, password string) (string, error) {
if strings.Contains(username, ":") {
return fmt.Sprintf("invalid username: %s", username)
return "", fmt.Errorf("invalid username: %s", username)
}
return fmt.Sprintf("%s:%s", username, ch.Bcrypt(password))
bcryptHash, err := ch.Bcrypt(password)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", username, bcryptHash), nil
}

// DerivePassword derives a password based on the given counter, password type, password, user, and site.
Expand All @@ -70,10 +74,10 @@ func (ch *CryptoRegistry) Htpasswd(username string, password string) string {
// Example:
//
// {{ derivePassword 0 "bcrypt" "password" "user" "site" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"
func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, user, site string) string {
func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, user, site string) (string, error) {
var templates = passwordTypeTemplates[passwordType]
if templates == nil {
return fmt.Sprintf("cannot find password template %s", passwordType)
return "", fmt.Errorf("cannot find password template %s", passwordType)
}

var buffer bytes.Buffer
Expand All @@ -84,7 +88,7 @@ func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password,
salt := buffer.Bytes()
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 2, 64)
if err != nil {
return fmt.Sprintf("failed to derive password: %s", err)
return "", fmt.Errorf("failed to derive password: %s", err)
}

buffer.Truncate(len(masterPasswordSeed))
Expand All @@ -104,7 +108,7 @@ func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password,
buffer.WriteByte(passChar)
}

return buffer.String()
return buffer.String(), nil
}

// GeneratePrivateKey generates a private key of the specified type.
Expand All @@ -115,7 +119,7 @@ func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password,
// Example:
//
// {{ generatePrivateKey "rsa" }} // Output: "-----BEGIN RSA PRIVATE KEY-----"
func (ch *CryptoRegistry) GeneratePrivateKey(typ string) string {
func (ch *CryptoRegistry) GeneratePrivateKey(typ string) (string, error) {
var priv any
var err error
switch typ {
Expand All @@ -126,7 +130,7 @@ func (ch *CryptoRegistry) GeneratePrivateKey(typ string) string {
key := new(dsa.PrivateKey)
// again, good enough for government work
if err = dsa.GenerateParameters(&key.Parameters, cryptorand.Reader, dsa.L2048N256); err != nil {
return fmt.Sprintf("failed to generate dsa params: %s", err)
return "", fmt.Errorf("failed to generate dsa params: %s", err)
}
err = dsa.GenerateKey(key, cryptorand.Reader)
priv = key
Expand All @@ -136,13 +140,13 @@ func (ch *CryptoRegistry) GeneratePrivateKey(typ string) string {
case "ed25519":
_, priv, err = ed25519.GenerateKey(cryptorand.Reader)
default:
return "Unknown type " + typ
return "", fmt.Errorf("Unknown type %s", typ)
}
if err != nil {
return fmt.Sprintf("failed to generate private key: %s", err)
return "", fmt.Errorf("failed to generate private key: %s", err)
}

return string(pem.EncodeToMemory(ch.pemBlockForKey(priv)))
return string(pem.EncodeToMemory(ch.pemBlockForKey(priv))), nil
}

// BuildCustomCertificate builds a custom certificate from a base64 encoded certificate and private key.
Expand Down

0 comments on commit 804a164

Please sign in to comment.