diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..325b30e --- /dev/null +++ b/.github/workflows/tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 78ec7b1..68e6a15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ __pycache__/ *.py[co] +.coverage +coverage.xml +.pytest_cache/ diff --git a/auth/phorum_mysql_users.py b/auth/phorum_mysql_users.py index 4c3db30..83cf0f9 100755 --- a/auth/phorum_mysql_users.py +++ b/auth/phorum_mysql_users.py @@ -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 diff --git a/auth/phorum_pgsql_users.py b/auth/phorum_pgsql_users.py index b099751..cf48a2b 100755 --- a/auth/phorum_pgsql_users.py +++ b/auth/phorum_pgsql_users.py @@ -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 diff --git a/docs/unit-testing-plan.md b/docs/unit-testing-plan.md index 570e77b..ad06719 100644 --- a/docs/unit-testing-plan.md +++ b/docs/unit-testing-plan.md @@ -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 diff --git a/requirements-dev.txt b/requirements-dev.txt index 8b17f72..4ad74d1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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" diff --git a/tests/test_sql_backends.py b/tests/test_sql_backends.py index 87f7c0c..f807cf1 100644 --- a/tests/test_sql_backends.py +++ b/tests/test_sql_backends.py @@ -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")