Skip to content

Conversation

@billy4479
Copy link
Contributor

Fixes #879 and #1167.

As I wrote in this comment the previous behavior seemed intentional so if this is not the desired behavior please let me know, I will make another PR just addressing the panic described in #879 (maybe with a nicer error message?)

When decrypting to dotenv we try to escape new line in the values
without taking into account the possibility that the value could be
something different than a string (e.g. an int).

This used to cause a panic when using `decrypt --output-format dotenv`.

Signed-off-by: billy4479 <giachi.ellero@gmail.com>
This was previously forbidden for some reason, however if the value is
not a complex type there should be no issues.

Signed-off-by: billy4479 <giachi.ellero@gmail.com>
@felixfontein
Copy link
Contributor

I'm not sure whether it's a good idea to convert everything to strings implicitly. Especially for booleans this can be dangerous, since everyone has their own idea of how booleans should be represented as strings.

The INI store automatically converts everything to strings. I guess that if we do this for DotEnv as well, it should use the same functionality. (There's a function valToString in stores/ini/store.go doing that; unfortunately it has a bug: #1915.)

@getsops/maintainers any opinion on whether the DotEnv store should automatically convert simple values (floats, bools, integers, timestamps, ...) to strings?

Copy link
Contributor

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

I think I'm OK with merging this if this uses valToString() from stores/ini/store.go. (#1929 fixes the bug I've mentioned in there.) It's probably best to move valToString() to stores/stores.go for that and rename it to ValToString() so it's public.

value = fmt.Sprintf("%v", item.Value)
}

value = strings.ReplaceAll(value, "\n", "\\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is definitely wrong and must not be there.

Suggested change
value = strings.ReplaceAll(value, "\n", "\\n")

Comment on lines +147 to +149
}

value = strings.ReplaceAll(value, "\n", "\\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

The replace only makes sense for strings:

Suggested change
}
value = strings.ReplaceAll(value, "\n", "\\n")
} else {
value = strings.ReplaceAll(value, "\n", "\\n")
}

@billy4479
Copy link
Contributor Author

billy4479 commented Aug 30, 2025

Nice! I will have a look again in the next few days, thanks!

Also, what is this project convention in this case? Can I rebase on top of your PR or should I wait for it to get merged first?

@felixfontein
Copy link
Contributor

Also, what is this project convention in this case? Can I rebase on top of your PR or should I wait for it to get merged first?

I'm not sure we have a convention for that yet. Both is fine for me, or (as a third choice) you could merge my branch into yours.

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.

Decrypt with output-type dotenv panics on numbers

2 participants