I have program that lets users parse JSON data with jq. I am shelling out to jq rather than using a library because the ones I've found have weird and inconsistent behaviour. The problem I have is that jq often has |
(pipe) characters and, that could potentially let users run non-jq commands. e.g.:
jq . | rm file.txt
How do I safely let users run this?
Right now, I invoke it as this in Go (where file.txt contains the raw json):
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat file.txt | %s", cmd))
Thanks!
EDIT: As pointed out several times, this question isn't about piping commands together. It is about how to safely execute a jq command that has pipes in it. I don't want users to do this: curl 'api.icndb.com/jokes/random/3' | jq -r '.value[] | .joke | rm file.txt'. Please re-open.