5 ms·
The point of TFA is preventing piping curl to bash, not preventing from running bash scripts. The problem with piping is that bash will read line by line, keep
by fathyb 4y ago
The point of TFA is preventing piping curl to bash, not preventing from running bash scripts.
The problem with piping is that bash will read line by line, keeping the connection open while it runs. If the connection fails for any reason, the script will stop, potentially breaking or corrupting things.
This can be prevented by first downloading the file, and then running it.
- codetrotter 4y ago> If the connection fails for any reason, the script will stop, potentially breaking or corrupting things. If the script is meant to be piped from curl, and is well written, it will be written so that it first defines everything as functions and then at the very end makes a single function call. This ensures that the script will only do anything if it has been completely downloaded. For example, the script that rustup.rs tells you to pipe from curl is written in that way.
- vbezhenar 4y agoYou can't be sure that developer did that and it's better to stay on the safe side IMO.
- Arnavion 4y ago>it will be written so that it first defines everything as functions and then at the very end makes a single function call. This ensures that the script will only do anything if it has been completely downloaded. This is not sufficient. More care is required. lsp_init() { ... } lsp_init If the last line gets truncated between the `s` and the `p`, then `ls` gets executed. Of course `ls` is harmless, but I'm sure you can imagine how it could be worse. In other words, not only do you have to wrap your script in functions, but you have to ensure that any top-level function / command invocations are named such that they do not become different commands if truncated. This is unsolvable in general, because the user can have any arbitrary names in their $PATH , such as custom personal utils in ~/.local/bin which can have any unforeseeable name. It's much easier to just wrap the script in `()` to make it run in a subshell. bash will not run anything until it sees the closing `)` so truncated scripts are not a problem, and it also doesn't have the name collision problem.
- rascul 4y agoHow would it become truncated?
- bqmjjx0kac 4y agoA cat unplugs the router?
- layer8 4y agoWhen a dog does it, would it be trundoged instead?
- deleted 4y ago[deleted]
- fathyb 4y agoIn the case of curl it can happen if the server did not use Content-Length and the connection gets closed.
- Arnavion 4y agoAll the presence of the content-length header would do is change curl's exit code when the connection breaks. The part where curl output the truncated script and bash executed it would be unchanged.
- Arnavion 4y agoThe whole context of this conversation is >If the connection fails for any reason, the script will stop, potentially breaking or corrupting things.
- Analemma_ 4y agoI've heard this theory before-- that a command could get cut off halfway and execute something really bad-- and tbh I'm skeptical that this has happened even one time in all the billions of invocations of curl|bash. It's just not worth worrying about, in the same way that a cosmic ray bitflip could cause a kernel bug that erases my entire drive but in reality I don't spend any time worrying about this.
- jesprenj 4y agoYou can fix that by wrapping the entire code in a function and calling it at the end.