Repository navigation
Conversation
Percent-encode user and password in connection string.
Added tests for connection string generation, including user and password escaping and round-trip verification.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #13384 +/- ##
=========================================
Coverage 23.01% 23.01%
Complexity 10244 10244
=========================================
Files 307 307
Lines 39643 39646 +3
Branches 672 672
=========================================
+ Hits 9122 9125 +3
Misses 29952 29952
Partials 569 569
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
RichardJSun
left a comment
There was a problem hiding this comment.
The quoting is correct, and both test files pass for me.
This PR adds the same quote(..., safe='') code to 17 files. Can the scripts call db_utils.generate_connect_string instead? That function now does the escaping. adduser.py, anonymize.py and generate_repos.py already use it. Then the escaping is in one location. Five of the 15 scripts already import submitty_utils. The other scripts need the import. The migrator can keep its own copy if it must not depend on submitty_utils.
Posted with assistance from Claude Opus 5.5.
The scripts touched earlier in this PR each built their own SQLAlchemy URL and each called quote() on the user and password. db_utils.generate_connect_string already does that escaping, so the scripts now call it instead and the escaping lives in one place, as adduser.py, anonymize.py and generate_repos.py already do. The port now comes from database_port in the config each script already loads (falling back to 5432 like the migrator). For a socket host the port is no longer passed, and for a TCP host the configured port is now used where it used to be left out. The migrator keeps its own quote() call because it is installed and run before python_submitty_utils during setup.
|
Thanks @RichardJSun, done in 893c3e1. All 15 scripts now build the URL with The migrator keeps its own Some of the scripts had no port before. They now read
Also, reupload_old_assignments.py always used Tests: python_submitty_utils, migration and autograder unit tests pass locally, and |
|
I merged the latest main into the branch and fixed the conflicts from the ruff format change, so every script still builds its URL through db_utils.generate_connect_string and the files pass ruff check and ruff format. The db_utils and migrator tests pass locally. |
Why is this Change Important & Necessary?
submitty_utils.db_utils.generate_connect_string()puts the database user and password into thepostgresql://URL as-is. The password is whatever the admin typed intoCONFIGURE_SUBMITTY.py, so it can contain characters that mean something in a URL. With an@in the password, SQLAlchemy splits the URL at the wrong place: the part after the@becomes the host and the password is cut short. A%xxsequence in the password gets decoded as well, so the password sent to Postgres is not the configured one.Every Python script that builds its connection through this helper then fails to connect:
sbin/adduser.py,sbin/adduser_course.py,sbin/anonymize.pyandbin/generate_repos.py. The PHP site is not affected because it builds akey=valuePDO DSN rather than a URL.There is no existing issue for this; I found it while reading
db_utils.py.What is the New Behavior?
The user and password are percent-encoded with
urllib.parse.quote(..., safe='')before they go into the URL, as SQLAlchemy's docs suggest for passwords with special characters (quoterather thanquote_plus, so a space also round-trips). SQLAlchemy decodes them again, so Postgres receives the original values. Passwords without special characters produce exactly the same string as before.Before, with the database password set to
se@cret%40x:After:
What steps should a reviewer take to reproduce or test the bug or new feature?
@, e.g.CREATE ROLE submitty_dbuser LOGIN PASSWORD 'se@cret%40x';, and a database it owns.db_utils.generate_connect_string("localhost", 5432, "submitty", "submitty_dbuser", "se@cret%40x")and connect withsqlalchemy.create_engine(...).connect()(I used the pinnedsqlalchemy==2.0.52andpsycopg2-binary==2.9.12).mainthe connection fails as shown above; on this branch it connects, over TCP and over the Unix socket.Automated Testing & Documentation
Added two unit tests to
python_submitty_utils/tests/test_db_utils.py:@,:,/and%;Both fail on
mainand pass here. The existing tests are unchanged and still pass, andpython -m unittest discoverinpython_submitty_utilspasses (25 tests).ruff checkis clean. No documentation change is needed.Other information
Not a breaking change and no migration needed. Some
.setup/binsample-data scripts andautograder/insert_database_version_data.py/sbin/cleanup_active_graders.pybuild their own URLs the same way; I left them out to keep this PR small and can move them onto this helper in a follow-up if that is wanted.I used an AI assistant (Claude) while tracing this and writing the tests. I reviewed the change and ran the tests and the Postgres check above myself.
Update: The same percent-encoding (
urllib.parse.quote(..., safe='')) is now also applied to every other place that built a PostgreSQL URL from the DB user and password by hand:migration/migrator/db.py(so install/update work with an@in the password),bin/set_allowed_mins.py,bin/insert_build_data.py,autograder/autograder/insert_database_version_data.py,sbin/auto_rainbow_scheduler.py,sbin/cleanup_active_graders.py,sbin/database_queries.py,sbin/send_email.py,sbin/send_notification.py,sbin/grading_clustering/clustering_database_queries.py, and the dev setup scripts in.setup/bin/(setup_sample_courses.py,setup_sample_emails.py,populate_office_hours_queue.py,reupload_old_assignments.py,recreate_submissions.py,sample_courses/models/course/course_create.py).New migrator unit tests check that a user and password containing
@,:,/and%are escaped and parse back to the original values through SQLAlchemy, for both a TCP host and a socket directory.Test coverage is incomplete: the
bin/,sbin/, autograder and.setup/bin/scripts build the URL inline next to the connection code and have no existing unit tests, so they were only checked by hand and with ruff.Left out on purpose: the shell scripts and the
os.system(... PGPASSWORD='...' psql ...)calls in.setup/, since they pass the password throughPGPASSWORDinstead of a URL (their shell quoting is a separate issue).