What’s a Safe Configuration File Format to Use?

0
16
Asked By CoolBreeze92 On

I'm looking for a configuration file format that feels safe to use without the risk of executing code when sourced. Currently, I'm using JSON alongside jq, but I've been considering YAML. However, I can't really justify installing yq just for that purpose. I could also parse a simple text file if that would work, but I'm not sure what the best approach would be for key-value pairs. Any suggestions?

4 Answers

Answered By NerdlyNerd123 On

Have you thought about using TOML? It allows comments, which JSON lacks. Sure, YAML could get the job done, but its strict whitespace rules can be tricky, and it sometimes feels like an overcomplicated markup language.

TechieTommy88 -

But if you can't install yq, what’s your plan for parsing TOML?

Answered By QuickFix44 On

If all you need is key-value pairs, parsing a text file would work just fine! It’s super straightforward. Here’s a simple approach:

```bash
test.txt:
foo=bar

while IFS='=' read -r key value; do
ary["$key"]=$value
done < test.txt

declare -p ary
```
This gives you a nice associative array, and you can check out a similar method on Stack Overflow.

AnotherCoder99 -

You might want to add a line to skip comments in your loop: `[[ $key == *#* ]] && continue`.

Answered By ScriptingSage77 On

Here’s my version:
```bash
while read KEY VALUE; do
[[ "$KEY" == *#* ]] && continue;
# Handle your data here
done
```

Answered By CuriousCoder456 On

What are you trying to achieve exactly? If you're only sourcing files that you control, there's probably not much risk involved. Could you share a snippet of your script that handles the sourcing?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.