Skip to content

Commit

Permalink
Merge branch 'RESTAPI-999-improve-shell-command-security' into 'master'
Browse files Browse the repository at this point in the history
Restapi 999 improve shell command security

See merge request firecrest/firecrest!278
  • Loading branch information
Elia committed Mar 1, 2024
2 parents 8150333 + 7834c22 commit c8c0a35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed check when submitted an empty batch file on `POST /compute/jobs/upload`
- Fixed error message when `GET /status/systems` encounters error in one filesystem
- Fixed SSH connection error catching
- Fixed secured "ssh-keygen" command execution

### Changed

Expand Down
22 changes: 17 additions & 5 deletions src/certificator/certificator.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,21 +446,33 @@ def receive():
app.logger.error(f"Forbidden char on command or option: {force_command} {force_opt}")
return jsonify(description='Invalid command'), 400

force_command = f"-O force-command=\"{force_command} {force_opt}\""
force_command = force_command.replace('$', '\$')

# create temp dir to store certificate for this request
td = tempfile.mkdtemp(prefix = "cert")
os.symlink(PUB_USER_KEY_PATH, f"{td}/user-key.pub") # link on temp dir

command = f"ssh-keygen -s {CA_KEY_PATH} -n {username} -V {ssh_expire} -I {CA_KEY_PATH} {force_command} {td}/user-key.pub "

command = ["ssh-keygen",
"-s",
f"{CA_KEY_PATH}",
"-n",
f"{username}",
"-V",
f"{ssh_expire}",
"-I",
f"{CA_KEY_PATH}",
"-O",
f"force-command={force_command} {force_opt}",
f"{td}/user-key.pub"
]

except Exception as e:
logging.error(e)
return jsonify(description=f"Error creating certificate: {e}", error=-1), 400


try:
result = subprocess.check_output([command], shell=True)
#To prvent shell hijacking don't run commands with shell=True
result = subprocess.run(command, shell=False, check=True)
with open(td + '/user-key-cert.pub', 'r') as cert_file:
cert = cert_file.read()

Expand Down

0 comments on commit c8c0a35

Please sign in to comment.