Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: tests

on:
push:
branches: [master]
pull_request:

permissions:
contents: read

jobs:
unit:
name: unit tests (Python ${{ matrix.python }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15"]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python }}
allow-prereleases: true
cache: pip
cache-dependency-path: requirements-dev.txt
- run: pip install -r requirements-dev.txt
# the DB drivers and nntplib are replaced by fakes, so they aren't needed
- run: python -m pytest --cov=. --cov-report=term --cov-report=xml
- uses: actions/upload-artifact@v7
if: matrix.python == '3.13'
with:
name: coverage
path: coverage.xml
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
__pycache__/
*.py[co]
.coverage
coverage.xml
.pytest_cache/
7 changes: 6 additions & 1 deletion auth/phorum_mysql_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def is_valid_user(self, username, password):
if len(db_password) == 32:
result = (db_password != hashlib.md5(password.encode('utf-8')).hexdigest())
else:
result = (db_password != crypt.crypt(password, password[:settings.PHP_CRYPT_SALT_LENGTH]))
try:
result = (db_password != crypt.crypt(password, password[:settings.PHP_CRYPT_SALT_LENGTH]))
except OSError:
# glibc rejects salt characters outside [a-zA-Z0-9./]; such a
# password can't match a DES-crypted one
result = True
if result:
settings.logEvent('Error - Authentication failed for username \'%s\' (incorrect password)' % (username))
return 0
Expand Down
7 changes: 6 additions & 1 deletion auth/phorum_pgsql_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ def is_valid_user(self, username, password):
if len(db_password) == 32:
result = (db_password != hashlib.md5(password.encode('utf-8')).hexdigest())
else:
result = (db_password != crypt.crypt(password, password[:settings.PHP_CRYPT_SALT_LENGTH]))
try:
result = (db_password != crypt.crypt(password, password[:settings.PHP_CRYPT_SALT_LENGTH]))
except OSError:
# glibc rejects salt characters outside [a-zA-Z0-9./]; such a
# password can't match a DES-crypted one
result = True
if result:
settings.logEvent('Error - Authentication failed for username \'%s\' (incorrect password)' % (username))
return 0
Expand Down
2 changes: 1 addition & 1 deletion docs/unit-testing-plan.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Plan: Unit tests

**Status:** in progress (steps 1-5 done)
**Status:** in progress (steps 1-6 done; Docker integration tests remain)
**Related:** [integration testing plan](integration-testing-plan.md)

## Goal
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Test dependencies (install with: pip install -r requirements-dev.txt)
pytest
pytest-cov
# the Phorum auth modules import crypt, removed from the stdlib in 3.13
standard-crypt; python_version >= "3.13"
39 changes: 39 additions & 0 deletions tests/test_sql_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,42 @@ def test_check_statement_catches_problems():
assert check_statement("WHERE a=%s AND b=%s", ("x",))
assert check_statement("WHERE a=%s", None)
assert check_statement("WHERE a=%s", ("x",)) == []


class RowsCursor(FakeCursor):
"""Cursor that returns a fixed password hash."""
def __init__(self, stored):
super().__init__(1)
self.stored = stored

def fetchone(self):
return (self.stored,)


@pytest.mark.parametrize("module_name", ["auth.phorum_mysql_users", "auth.phorum_pgsql_users"],
ids=lambda m: m.split(".")[1])
@pytest.mark.parametrize("password, accepted", [
("secret", True),
("wrong!", False),
# regression: glibc's crypt() raises OSError for salt characters outside
# [a-zA-Z0-9./], and Phorum uses the password's first characters as salt
("!secret", False),
("'; --", False),
])
def test_phorum_crypt_passwords(load, module_name, password, accepted):
import crypt
module, _ = load(module_name)
auth = module.Papercut_Auth()
auth.cursor = RowsCursor(crypt.crypt("secret", "se"))
assert bool(auth.is_valid_user("alice", password)) == accepted


@pytest.mark.parametrize("module_name", ["auth.phorum_mysql_users", "auth.phorum_pgsql_users"],
ids=lambda m: m.split(".")[1])
def test_phorum_md5_passwords(load, module_name):
import hashlib
module, _ = load(module_name)
auth = module.Papercut_Auth()
auth.cursor = RowsCursor(hashlib.md5(b"secret").hexdigest())
assert auth.is_valid_user("alice", "secret")
assert not auth.is_valid_user("alice", "wrong")
Loading