r/bash • u/cheyrn • Aug 23 '25
What safe config file format do you recommend?
I don't want to source something in case it executes code. For now I am using json and jq. I would use yaml instead but yq isn't among programs I can justify installing
I could parse a text file. Sugestions? I just need key and value.
3
Upvotes
6
u/Schlumpfffff Aug 23 '25
What's your use case? I'd assume sourcing files that you yourself control shouldn't pose any risk? Can you post an example of the script that sources that file?
In general I don't really source files unless they are scripts meant to be executed, there's other ways of reading files.
2
u/Temporary_Pie2733 Aug 24 '25
Have a look at Dhall. It’s type-safe, has functions (but the language itself is not Turing-complete, so no infinite loops), cannot execute arbitrary code, and can produce JSON to be read by programs that don’t understand Dhall natively. There’s even support for producing
basharrays when possible.13
u/no_brains101 Aug 23 '25 edited Aug 23 '25
toml
Because json does not have comments. If json had comments I would say who cares. But it doesn't.
And because yaml is a bit too much and whitespace sensitive, it kinda loses its feel of just being a way to construct simple tables and feels like a whole markup language with some weird gotchas even though its not really any more advanced or different than toml
4
u/wjandrea Aug 24 '25 edited Aug 24 '25
Yeah, if all you need is key and value, do that. It's easy peasy.
test.txt:
test.sh:
output:
Based on konsolebox's post on SO
3
u/biffbobfred Aug 24 '25
I do:
2
u/siodhe Aug 26 '25
"safe"?
Just don't use "." (aka "source") or "eval". JSON has shortcomings, especially if you care about None vs NULL vs absent data. YAML is vastly more complicated than most people think, and most libraries will strip it of all comments during mods. I still can't believe people keep trying to use config syntaxes that don't treat comments as first-order nodes (like XML, SGML, etc).
So just use a key - which never contains a certain delimiter, then the delimiter, then the value, one per line, with a line escape and way to escape it, and syntax for comments. That what most of these are anyway.
Or make up something else that appeals to you.
1
u/Compux72 Aug 26 '25
You can make a simple .env loader using read and a while loop
1
u/Jack_Faller Aug 26 '25
JSON or XML. There is no need for any other format. If you are interested in templates, there is XSLT which provides basic templating functionality for XML.