Skip to content

Commit

Permalink
Added escaping for the placeholder parameters before passing it to th…
Browse files Browse the repository at this point in the history
…e passexec command. pgadmin-org#6794
  • Loading branch information
pravesh-sharma authored Dec 4, 2024
1 parent d8ed75d commit 5e8a75c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export default class ServerSchema extends BaseUISchema {
group: gettext('Advanced'), controlProps: {maxLength: null},
mode: ['properties', 'edit', 'create'],
disabled: pgAdmin.server_mode == 'True' && pgAdmin.enable_server_passexec_cmd == 'False',
helpMessage: gettext('The server hostname, port, and username can be passed as variables by using the placeholders %HOST%, %PORT%, and %USERNAME%, which will be replaced with the corresponding server connection information.')
},
{
id: 'passexec_expiration', label: gettext('Password exec expiration (seconds)'), type: 'int',
Expand Down
13 changes: 10 additions & 3 deletions web/pgadmin/utils/passexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from flask import current_app

import config
from pgadmin.utils.driver import get_driver


class PasswordExec:
Expand All @@ -22,9 +23,9 @@ class PasswordExec:

def __init__(self, cmd, host, port, username, expiration_seconds=None,
timeout=60):
cmd = str(cmd).replace('%HOSTNAME%', host)
cmd = cmd.replace('%PORT%', str(port))
cmd = cmd.replace('%USERNAME%', username)
self.host = host
self.port = port
self.username = username
self.cmd = cmd
self.expiration_seconds = int(expiration_seconds) \
if expiration_seconds is not None else None
Expand All @@ -36,6 +37,12 @@ def get(self):
if config.SERVER_MODE and not config.ENABLE_SERVER_PASS_EXEC_CMD:
# Arbitrary shell execution on server is a security risk
raise NotImplementedError('Passexec not available in server mode')
driver = get_driver(config.PG_DEFAULT_DRIVER)
self.cmd = str(self.cmd)
self.cmd = self.cmd.replace('%HOSTNAME%', self.host)
self.cmd = self.cmd.replace('%PORT%', str(self.port))
self.cmd = self.cmd.replace('%USERNAME%',
driver.qtIdent(None,self.username))
with self.lock:
if not self.password or self.is_expired():
if not self.cmd:
Expand Down

0 comments on commit 5e8a75c

Please sign in to comment.