fix: render Jinja2 watch URL on diff/preview pages instead of the raw template (#3776)#4256
Open
ebarkhordar wants to merge 1 commit into
Open
fix: render Jinja2 watch URL on diff/preview pages instead of the raw template (#3776)#4256ebarkhordar wants to merge 1 commit into
ebarkhordar wants to merge 1 commit into
Conversation
… template (dgtlmoon#3776) The diff and preview pages passed the raw watch['url'] into base.html as current_diff_url, so a URL containing a Jinja2 template (e.g. {% now 'utc','%Y' %}) was linked and displayed unrendered and non-clickable. Watch.link already performs jinja_render() plus safe-url validation, so use it at the four render call sites that feed current_diff_url.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3776
Root cause
The diff and preview pages pass the raw
watch['url']intobase.htmlascurrent_diff_url:changedetectionio/processors/text_json_diff/difference.py(diff page)changedetectionio/processors/image_ssim_diff/difference.py(image diff page)changedetectionio/blueprint/ui/preview.py(preview page)changedetectionio/processors/image_ssim_diff/preview.py(image preview page)base.htmlthen renders it as<a class="current-diff-url" href="{{ current_diff_url }}">. When a watch URL contains a Jinja2 template such as{% now 'utc', '%Y' %}, the link points at the unrendered template string, so it shows raw and is not clickable (the reported behaviour).Invariant
The URL linked on the diff and preview pages must be the Jinja2-rendered, safety-validated
Watch.linkvalue, never the raw template.Watch.link(model/Watch.py) already doesjinja_render()plusis_safe_valid_url()validation and is what the watch list uses, so the diff and preview headers should use the same value.Fix
Replace
current_diff_url=watch['url']withcurrent_diff_url=watch.linkat the four render call sites above. For a plain URLWatch.linkreturns it unchanged, so nothing changes for non-templated watches; for an invalid URL it returnsDISABLED, whichbase.htmlalready guards withis_safe_valid_url(current_diff_url).The reported issue is the diff page; the same raw-template value was passed at the preview and image-processor render calls, so I fixed all four rather than leave the preview page showing the same unrendered URL.
Verification
Ran in a clean Docker container (python:3.11-slim,
requirements.txt), against this branch and againstmaster:tests/test_diff_url_jinja2_render.py(adds a paused watch with a Jinja2 URL, injects two history snapshots, loads the diff page): fails onmaster(thecurrent-diff-urlhref is the raw...{% now ... %}...), passes on this branch (href is the rendered URL). Confirmed both runs load the branch/master copy viamodule.__file__.tests/test_download_patch.py(renders the diff page for normal URLs): 3 passed on this branch, sowatch.linkdoes not change plain-URL rendering.What I did not verify:
tests/test_jinja2.py::test_jinja2_in_url_query(preview page, does a live fetch) could not run in my container because the fetcher could not resolve the live-server host; it fails the same way on unmodifiedmaster, so it is an environment limit on my side, not this change. The preview and image-processor call sites get the same one-line substitution and the samebase.htmlcontract as the diff page, but only the diff page has a new regression test here.AI assistance: this fix was prepared with AI help; the reproduction, the differential test, and the Docker runs above were executed and checked before opening.