All 48 statements that pasted a value into the SQL text now send it as a
psycopg2 parameter, so a search term, an account name or a message body is data
and nothing else. Before this, `findby_name("'; DROP TABLE users; --")` ran two
statements, `log_in(admin, "' OR '1'='1")` handed out an administrator session
for a wrong password, and a legitimate apostrophe in `O'Brien` was a syntax
error.
No semantics changed: same statements, same column order and types (SELECT *
read by position, users.userid still a 4-byte int4), and every table/column name
is a literal - no identifier comes from input, so no whitelist was needed. The
connection string keeps `password={DB_PASSWORD}` from the environment, and the
LIKE fragments keep their wildcards in the parameter.
tests/test_injection.py adds 15 tests: the stacked DROP TABLE (no exception, and
the table is still there, checked with a SELECT), the legitimate apostrophe, the
quotes written and read back byte for byte through create_user/message_client/
message_all/add_article, the password bypass, and a structural guard that every
execute() receives a constant string. All 15 fail against the previous
database.py; `make test` is 55 passed, 0 failed.
README: SQL injection leaves "Known limitations"; what stays is listed (the
article[x - 1] IndexError, input() in the data layer, message_all()'s
Sum(pg_column_size(userid))/4, and the global USERID copy in main.py).
The suite documented every defect with two `xfail` and four `test_known_bug_*`
tests. With the tests as the specification, all of them are fixed and those
tests are now regression tests that fail against the previous code:
- findby_director() returned None on the success path (it printed the matches
and fell off the end); it returns the rows now.
- findby_actor() raised UnboundLocalError when no actor matched, and returned
only the last article of an actor with several; it returns the list, or None.
- findby_name() and findby_type() returned the integer 0 on no match, which
main.py indexed into; they return an empty list and the menus skip it.
- purchase() debited USERID (the account of the last log_in) instead of the
userid argument, while the rent went to the argument.
- the message list looked the author up with row[4] (the recipient) instead of
row[5] (the sender), in the unread and in the read list.
- remove_article() checked only the open rents, so the foreign keys on the
older rents and on pricehistory turned the refusal into a traceback; it now
explains which records keep the article and rolls back as a backstop.
- show_unread_messages() returned None when there was nothing; it returns an
empty list like the read side, and the messages menu skips an empty list.
- database.USERID did not exist before a login, so leaving the menu with 0
raised AttributeError; it starts as None and the menu code handles 0.
- every connection is opened and closed by one context manager:
findby_director(), findby_actor() and findby_type() returned before
conn.close() and statistics() never closed, leaving sessions
`idle in transaction`.
make test: 40 passed, 0 xfailed (was 30 passed, 2 xfailed). No xfail and no
test_known_bug_* is left. The README's previous "what is broken" paragraph and
its test output are updated, and a "Known limitations" section lists what was
deliberately not touched: SQL built by string concatenation, menu selections
past the end of the list, and input() inside the data layer.
The schema existed only inside the queries, so a clean clone could not run:
nothing created the tables that database.py and main.py expect, and there was
no declaration of what to install.
- schema.sql: the seven tables, their primary and foreign keys, and the
administrator the code requires. Reconstructed from the SQL the code issues
and typed for it: the code reads rows by position (SELECT *), so the column
order is part of the contract, and users.userid has to be a 4-byte integer
because message_all() counts rows with pg_column_size(userid)/4.
- scripts/db.py, Makefile, docker-compose.yml: a throwaway PostgreSQL and the
targets up/schema/seed/run/test/down. Both passwords come from the
environment (NETFLOX_DB_PASSWORD, NETFLOX_DEMO_PASSWORD); nothing is stored
in the repository, and schema.sql receives the administrator password
through a session setting.
- requirements.txt and requirements-dev.txt: psycopg2-binary, plus pytest for
the suite.
- tests/: 30 end-to-end tests that recreate the schema and drive the real
functions of database.py. They check the column order and the keys against
information_schema and walk every main flow. The defects found while mapping
the code are pinned by test_known_bug_* and xfail instead of fixed, since
fixing the application was not part of this change. The suite also guards the
credential fix: database.py must refuse to import without its variable and no
tracked file may contain the value.
- README.md: the sections shared with the other repositories, with a real
session.
- The report moved to docs/relatorio-BD.pdf: it had accents and spaces in the
filename.