-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitize the string to avoid a connection string injection #532
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,55 @@ | ||
import os | ||
import re | ||
|
||
_SANITIZE_WORD_REGEX = r"[^\w]" # A-Za-z0-9_ | ||
_SANITIZE_HOST_REGEX = r"[^\w.-]" | ||
_SANITIZE_PWD_REGEX = r"[\"\s%+~`#$&*()|\[\]{}:;<>?!'/]+" | ||
_AURORA_HOST_SUFFIX = "rds.amazonaws.com" | ||
_POSTGRES_MAX_LEN = 63 | ||
_MAX_HOST_LENGTH = 253 | ||
|
||
_envname = os.getenv('envname', 'local') | ||
|
||
|
||
class DbConfig: | ||
def __init__(self, **kwargs): | ||
self.params = kwargs | ||
self.url = f"postgresql+pygresql://{self.params['user']}:{self.params['pwd']}@{self.params['host']}/{self.params['db']}" | ||
def __init__(self, user: str, pwd: str, host: str, db: str, schema: str): | ||
for param in (user, db, schema): | ||
if len(param) > _POSTGRES_MAX_LEN: | ||
raise ValueError( | ||
f"PostgreSQL doesn't allow values more than 63 characters" | ||
f" parameters {user}, {db}, {schema}" | ||
) | ||
|
||
if len(host) > _MAX_HOST_LENGTH: | ||
raise ValueError(f"Hostname is too long: {host}") | ||
|
||
if _envname not in ['local', 'pytest', 'dkrcompose'] and not host.lower().endswith(_AURORA_HOST_SUFFIX): | ||
raise ValueError(f"Unknown host {host} for the rds") | ||
|
||
self.user = self._sanitize(_SANITIZE_WORD_REGEX, user) | ||
self.host = self._sanitize(_SANITIZE_HOST_REGEX, host) | ||
self.db = self._sanitize(_SANITIZE_WORD_REGEX, db) | ||
self.schema = self._sanitize(_SANITIZE_WORD_REGEX, schema) | ||
pwd = self._sanitize(_SANITIZE_PWD_REGEX, pwd) | ||
self.url = f"postgresql+pygresql://{self.user}:{pwd}@{self.host}/{self.db}" | ||
|
||
def __str__(self): | ||
lines = [] | ||
lines.append(' DbConfig >') | ||
lines = [' DbConfig >'] | ||
hr = ' '.join(['+', ''.ljust(10, '-'), '+', ''.ljust(65, '-'), '+']) | ||
lines.append(hr) | ||
header = ' '.join(['+', 'Db Param'.ljust(10), ' ', 'Value'.ljust(65), '+']) | ||
lines.append(header) | ||
hr = ' '.join(['+', ''.ljust(10, '-'), '+', ''.ljust(65, '-'), '+']) | ||
lines.append(hr) | ||
for k in self.params: | ||
v = self.params[k] | ||
if k == 'pwd': | ||
v = '*' * len(self.params[k]) | ||
lines.append(' '.join(['|', k.ljust(10), '|', v.ljust(65), '|'])) | ||
lines.append(' '.join(['|', "host".ljust(10), '|', self.host.ljust(65), '|'])) | ||
lines.append(' '.join(['|', "db".ljust(10), '|', self.db.ljust(65), '|'])) | ||
lines.append(' '.join(['|', "user".ljust(10), '|', self.user.ljust(65), '|'])) | ||
lines.append(' '.join(['|', "pwd".ljust(10), '|', "*****".ljust(65), '|'])) | ||
|
||
hr = ' '.join(['+', ''.ljust(10, '-'), '+', ''.ljust(65, '-'), '+']) | ||
lines.append(hr) | ||
return '\n'.join(lines) | ||
|
||
@staticmethod | ||
def _sanitize(regex, string: str) -> str: | ||
return re.sub(regex, "", string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than sanitizing and modifying the string, we should probably raise an exception if the string is invalid. modifying the inputs can have unexpected results for the user There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I will raise an error if the string is not the same after sanitizing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from dataall.db import DbConfig | ||
|
||
|
||
def test_sanitize_database(): | ||
config = DbConfig( | ||
user='dataall', | ||
pwd='123456789', | ||
host="dataall.eu-west-1.rds.amazonaws.com", | ||
db='dataall\'; DROP TABLE users;', | ||
schema='dev' | ||
) | ||
|
||
# connection injection: 'postgresql+pygresql://dataall:123456789@dataall.eu-west-1.rds.amazonaws.com/dataall'; DROP TABLE users; | ||
assert config.url == \ | ||
'postgresql+pygresql://dataall:123456789@dataall.eu-west-1.rds.amazonaws.com/dataallDROPTABLEusers' | ||
|
||
|
||
def test_sanitize_user(): | ||
config = DbConfig( | ||
user='dataall2;^&*end', | ||
pwd='qwsufn3i20d-_s3qaSW3d2', | ||
host="dataall.eu-west-1.rds.amazonaws.com", | ||
db='dataall', | ||
schema='dev' | ||
) | ||
|
||
assert config.url == \ | ||
'postgresql+pygresql://dataall2end:qwsufn3i20d-_s3qaSW3d2@dataall.eu-west-1.rds.amazonaws.com/dataall' | ||
|
||
|
||
def test_sanitize_pwd(): | ||
config = DbConfig( | ||
user='dataall', | ||
pwd='qazxsVFRTGBdfrew-332_c2@dataall.eu-west-1.rds.amazonaws.com/dataall\'; drop table dataset; # ', | ||
host="dataall.eu-west-1.rds.amazonaws.com", | ||
db='dataall', | ||
schema='dev' | ||
) | ||
|
||
# without sanitation should be : | ||
# postgresql+pygresql://dataall:qazxsVFRTGBdfrew-332_c2@dataall.eu-west-1.rds.amazonaws.com/dataall' | ||
# drop table dataset; # @dataall.eu-west-1.rds.amazonaws.com/dataall | ||
assert config.url == \ | ||
"postgresql+pygresql://dataall:qazxsVFRTGBdfrew-332_c2@dataall.eu-west-1.rds.amazonaws.com" \ | ||
"dataalldroptabledataset@dataall.eu-west-1.rds.amazonaws.com/dataall" | ||
|
||
|
||
def test_sanitize_host(): | ||
config = DbConfig( | ||
user='dataall', | ||
pwd='q68rjdmwiosoxahGDYJWIdi-9eu93_9dJJ_', | ||
host="dataall.eu-west-1$%#&@*#)$#.rds.amazonaws.com", | ||
db='dataall', | ||
schema='dev' | ||
) | ||
|
||
assert config.url == "postgresql+pygresql://dataall:q68rjdmwiosoxahGDYJWIdi-9eu93_9dJJ_@dataall.eu-west-1.rds.amazonaws.com/dataall" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @nikpodsh I see that you removed the port. Do we need it to make a connection or was it unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I double checked and it turned out that the port was used. Thanks for bringing this up!
We don't use the port while creating the database connection in the backend. So it should always roll back to the default value 5432.
I can re-add it and this time make the port be configurable on the backend or delete the usages of the port (two in the local env and one
quicksight#create_analysis
). What do you think would be better ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think adding the port as a configuration adds any value for the user, just more "work" for them. When you say deleting the port you mean hardcoding it in local and quicksight?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just hardcoding. By deleting I meant the deleting a configurable parameter (which is configurable only in a few places)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good