Skip to content

fix(web): stop CodeQL reopening the thumbnail path alert by using checks it recognises - #638

Merged
GeiserX merged 2 commits into
mainfrom
fix/thumbnail-path-containment
Nov 23, 2026
Merged

GeiserX merged 2 commits into
mainfrom
fix/thumbnail-path-containment

Conversation

@GeiserX

@GeiserX GeiserX commented Nov 23, 2026

Copy link
Copy Markdown
Owner

CodeQL keeps reporting a path injection in ensure_thumbnail (alert #71, and #25, #26, #36, #54, #64 to #67 before it). The code already resolves each path and checks that it stays inside its root. But CodeQL does not model Path.resolve() or Path.is_relative_to(), so it does not see that check. Each time the lines move, the dismissed alert comes back under a new number.

The tainted input is the size path parameter. It goes into the thumbnail destination path.

Fix

The three checks (the source file, the legacy-folder fallback and the thumbnail destination in both cache layouts) now use os.path.realpath and then str.startswith, inline. CodeQL's query treats these as a normalisation and a guard.

The behaviour does not change:

  • Symlinks and .. are resolved as before. Path.resolve() calls os.path.realpath itself.
  • The path and the root both end in a separator before the compare. So the root itself still passes, as with is_relative_to, and a sibling such as media-evil next to media does not.
  • Absolute folders and file names are still rejected.

Tests

New tests in tests/test_web_thumbnails.py cover both cache layouts and the legacy fallback: .. in the folder and in the file name, absolute paths, symlinks inside the media root that point outside, a sibling directory with the root's name as a prefix, and the root itself. They pass on main and on this branch. Mutants that drop the trailing separator or the fallback check make them fail.

CodeQL's py/path-injection query does not model Path.resolve() and
Path.is_relative_to(), so it kept reopening the same finding in
ensure_thumbnail each time the lines moved. The checks now use
os.path.realpath and str.startswith against the root plus a trailing
separator, which the query treats as a normalisation and a guard.
Accept and reject behaviour is unchanged, including the root itself and
a sibling directory that shares the root's name as a prefix; new tests
pin both cache layouts and the legacy folder fallback.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b413480 and 952ab43.

Files selected for processing: 2

  • telegram_archive/web/thumbnails.py
  • tests/test_web_thumbnails.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

🐳 Dev images published!

  • drumsergio/telegram-archive:dev
  • drumsergio/telegram-archive-viewer:dev

The dev/test instance will pick up these changes automatically (Portainer GitOps).

To test locally:

docker pull drumsergio/telegram-archive:dev
docker pull drumsergio/telegram-archive-viewer:dev

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.94%. Comparing base (b413480) to head (952ab43).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #638      +/-   ##
==========================================
+ Coverage   94.90%   94.94%   +0.04%     
==========================================
  Files          89       89              
  Lines       22703    22705       +2     
==========================================
+ Hits        21546    21558      +12     
+ Misses       1157     1147      -10     
Files with missing lines Coverage Δ
telegram_archive/web/thumbnails.py 94.17% <100.00%> (+5.40%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GeiserX

GeiserX commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

CodeQL evidence for this change.

The PR's own analysis reports 0 Python results, but it is diff-informed: it only reports results in changed lines. Line 303 (dest.exists()) and line 356 are not in the diff, so that zero alone does not prove the fix.

So I also ran the same query (CodeQL 2.27.5, python-queries 1.9.1, Security/CWE-022/PathInjection.ql) on full databases of both commits:

  • main (b413480): 5 results. Four are in thumbnails.py, at lines 292, 296, 303 and 356. All four have the same source, the size path parameter of serve_thumbnail. The fifth is main.py:3297.
  • This branch (76aeb27): 1 result, main.py:3297. This is the avatar lookup, a separate finding that this PR does not touch.

main's result count matches GitHub's latest main analysis (5 results), so the local run reproduces what code scanning sees.

…l checks

Three of the new containment guards had no test that fails on Linux:
resolving the media root, resolving the thumbs root, and the trailing
separator on the legacy fallback path. A media root or cache dir given
as a symlink must still serve, and an alternate folder that resolves to
the media root itself still passes, as Path.is_relative_to did. Both
tests pass on main and on this branch.
@github-actions

Copy link
Copy Markdown

🐳 Dev images published!

  • drumsergio/telegram-archive:dev
  • drumsergio/telegram-archive-viewer:dev

The dev/test instance will pick up these changes automatically (Portainer GitOps).

To test locally:

docker pull drumsergio/telegram-archive:dev
docker pull drumsergio/telegram-archive-viewer:dev

@GeiserX
GeiserX merged commit c82b8bb into main Nov 23, 2026
14 checks passed
@GeiserX
GeiserX deleted the fix/thumbnail-path-containment branch November 23, 2026 18:04
GeiserX added a commit that referenced this pull request Nov 23, 2026
…eleted (#640)

Releases #636, #637 and #638 as 9.9.0. Migration 043 adds an empty account_deleted_at column to chats and users. Every version pin, the changelog and the upgrading table are updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant