You Ran git Without a Shell and the Repository URL Was Still Read as an Option
The dashboard had two boxes. "Repository URL". "Branch". Before saving them, the server checked the branch was really there: subprocess.run(["git", "ls-remote", url, branch]) No shell. A list of arguments, not one
The dashboard had two boxes.
"Repository URL".
"Branch".
Before saving them,
the server checked
the branch was really there:
subprocess.run(["git", "ls-remote", url, branch])
No shell.
A list of arguments,
not one string.
You had read the advice
about command injection,
and you had followed it.
Semicolons did nothing.
Backticks did nothing.
Pipes did nothing.
Then somebody typed this
into the URL box:
--upload-pack=touch /tmp/owned
and a single dot
into the branch box.
And the file appeared
on your server.
Here is the mechanism.
Avoiding the shell
stops one kind of attack.
It stops the shell
from reading your input
as more commands.
It does nothing
about the program you call.
git received
exactly the arguments you sent.
One of them
started with two dashes,
so git did what every command line tool does
with a word that starts with a dash.
It read it as an option.
And --upload-pack
tells git which program to run
to talk to the repository.
The dot
became the repository,
the folder the server was running in.
For a repository on the same machine,
git runs that program itself.
On your machine.
Nothing was injected
into a shell.
An option was injected
into a tool
that trusted its own arguments.
It is called argument injection,
and git is not special.
Any program
with a powerful flag
will do the same
when your value
lands where a flag can go.
The fixes stack.
First, end the options yourself.
subprocess.run(["git", "ls-remote", "--end-of-options", url, branch])
git added --end-of-options
in version 2.24
for exactly this.
Many other tools
accept a plain --
to mean the same thing.
Second, reject what cannot be valid.
A repository URL
does not start with a dash,
and a branch name
is not a dot.
Allow the schemes you support,
https and maybe ssh,
and refuse the rest.
Third, run the check
somewhere that cannot hurt you,
with no secrets
and no write access
to anything that matters.
Then go and look for every place
your code builds a command line
from something a user typed.
File names.
Branch names.
Host names.
Search terms.
"No shell"
was never the same as "safe".
It only meant
one interpreter fewer
was reading your input.
β Serguey Asael Shinder
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.