You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "eschutho (via GitHub)" <gi...@apache.org> on 2026/07/16 23:10:50 UTC

[PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

eschutho opened a new pull request, #42120:
URL: https://github.com/apache/superset/pull/42120

   ### Decisions made that were not in the instructions
   
   - **Validation depth**: chose a cheap PNG/JPEG magic-byte header check (`\x89PNG\r\n\x1a\n` / `\xff\xd8\xff`), not a full image decode, per the instruction to keep this cheap. A full decode would add CPU cost to every cache read/write for a class of corruption (truncated/blank-but-headered files) that a header check won't catch — deemed out of scope for closing the specific "blank PDF from stale cache" report.
   - **Chart-screenshot endpoint**: no changes were needed to `superset/charts/api.py`. Both the dashboard and chart `screenshot()`/`thumbnail()`/`cache_*` handlers already funnel through the shared `BaseScreenshot.get_from_cache_key()` classmethod, so fixing validation there closes the read-side hole for both resource types mechanically, with no separate chart-specific patch.
   - **Where the read-side check lives**: added it to `get_from_cache_key()` rather than to `ScreenshotCachePayload.get_image()` or `__init__`. This scopes the change to the cache-retrieval choke point (what the endpoints actually call to decide "hit vs. miss") without changing the behavior of `get_image()`/`ScreenshotCachePayload` construction elsewhere (e.g. the Celery report/alert pipeline, which calls `get_screenshot()` directly and never touches this cache layer).
   
   ### SUMMARY
   Dashboard/chart screenshot and thumbnail caching (`ScreenshotCachePayload` in `superset/utils/screenshots.py`) had two gaps:
   
   1. **No read-side validation.** `BaseScreenshot.get_from_cache_key()` returned whatever was in the cache as long as a status of `UPDATED` was recorded, even if the stored image was `None`/0-byte or otherwise not a real image. Since the cache key is digest-based, an unchanged dashboard/chart kept serving the same stale/blank entry indefinitely (e.g. a blank PDF download).
   2. **Write-side validation was falsy-only.** A prior fix (#41097) set `ERROR` status when the screenshot task produced a falsy (`None`/`b""`) result, but a non-empty, non-image payload (e.g. truncated/corrupt bytes) still passed the `if image:` check and got cached with `UPDATED` status.
   
   This PR adds a shared, cheap validator (`validate_screenshot_image()` — checks non-empty + PNG/JPEG magic-byte header, no full decode) used on both paths:
   
   - **Read side**: `get_from_cache_key()` now rejects a cached payload that claims a successful screenshot (`status == UPDATED`) but fails validation, returning `None` — the same value callers already treat as a cache miss — and logs a `WARNING` with the cache key and the reason (`empty` vs `undecodable`). Because both the dashboard and chart `screenshot`/`thumbnail`/`cache_*` endpoints already call this same shared classmethod, this closes the hole for both resource types without touching `charts/api.py` or `dashboards/api.py`.
   - **Write side**: `BaseScreenshot.compute_and_cache()` now runs the same check on the freshly generated image before caching it as a success. If it fails, the payload is marked `ERROR` (consistent with #41097's approach) instead of `UPDATED`, and a `WARNING` with the cache key and reason is logged.
   
   **Explicitly not changed:**
   - Capture/webdriver logic (`superset/utils/screenshot_utils.py`, `superset/utils/webdriver.py`) — untouched, owned by concurrent PRs #42119 and #42118.
   - Cache backend, cache keys, and TTLs — unchanged.
   - Behavior for correctly-cached images — a valid, non-empty PNG/JPEG payload is served and cached exactly as before; this only changes behavior for payloads that were already broken (empty/corrupt).
   - No new config flags — the validation is unconditional, since serving a 0-byte image is never the intended behavior.
   
   ### TESTING INSTRUCTIONS
   Added unit tests in `tests/unit_tests/utils/test_screenshot_cache_fix.py` and updated fixtures in `tests/unit_tests/utils/screenshot_test.py` (existing tests used non-image placeholder bytes like `b"image_data"` as stand-ins for a "successful" screenshot; these now use a minimal valid PNG header so they still exercise the success path under the new validation):
   
   - Read side: a cached payload with a 0-byte image is served as a cache miss (`None`) with a `WARNING` logged; a payload with non-image garbage bytes is likewise treated as a cache miss; a valid PNG-header payload is served normally; a non-`UPDATED` (e.g. `PENDING`) payload is returned as-is.
   - Write side: `compute_and_cache()` with an empty or garbage-bytes screenshot result caches `ERROR` status (never `UPDATED`) and logs a `WARNING` including the cache key and reason.
   
   Run:
   ```
   pytest tests/unit_tests/utils/test_screenshot_cache_fix.py tests/unit_tests/utils/screenshot_test.py -q
   ```
   48 passed. Also ran the broader `tests/unit_tests/utils/ -k screenshot` suite (76 passed); 3 unrelated pre-existing failures in `webdriver_test.py`/`test_screenshot_utils.py` reproduce identically on `master` (playwright-version mismatch in the test environment, unrelated to this change and in files this PR does not touch).
   
   `ruff check` passes on the changed files; `mypy` reports no errors in `superset/utils/screenshots.py`.
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #42120:
URL: https://github.com/apache/superset/pull/42120#discussion_r3605932605


##########
superset/utils/screenshots.py:
##########
@@ -326,15 +361,24 @@ def compute_and_cache(  # pylint: disable=too-many-arguments
                         image = None
 
                 # Cache the result (success or error) to avoid immediate retries
-                if image:
+                invalid_reason = validate_screenshot_image(image)
+                if image and invalid_reason is None:
                     with event_logger.log_context(
                         f"screenshot.cache.{self.thumbnail_type}"
                     ):
                         cache_payload.update(image)
-                elif cache_payload.status != StatusValues.ERROR:
-                    # Only call error() if not already set — avoids overwriting
-                    # the timestamp recorded when the actual failure occurred above.
-                    cache_payload.error()
+                else:
+                    if invalid_reason:
+                        logger.warning(
+                            "Not caching screenshot result for %s: %s image payload",
+                            cache_key,
+                            invalid_reason,
+                        )

Review Comment:
   The warning message "Not caching screenshot result …" is misleading because the code still writes a cache entry (with ERROR status) right after this. This can confuse operators reading logs during incident/debugging. Consider rephrasing to indicate you're caching an ERROR payload, and avoid duplicating this warning when the payload is already ERROR due to an exception earlier in the method.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "netlify[bot] (via GitHub)" <gi...@apache.org>.
netlify[bot] commented on PR #42120:
URL: https://github.com/apache/superset/pull/42120#issuecomment-5064233145

   ### <span aria-hidden="true">✅</span> Deploy Preview for *superset-docs-preview* ready!
   
   
   |  Name | Link |
   |:-:|------------------------|
   |<span aria-hidden="true">🔨</span> Latest commit | 4800a885f39653c0d9b14b1e41934dc8bede153b |
   |<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/superset-docs-preview/deploys/6a62978c072cc0000843286b |
   |<span aria-hidden="true">😎</span> Deploy Preview | [https://deploy-preview-42120--superset-docs-preview.netlify.app](https://deploy-preview-42120--superset-docs-preview.netlify.app) |
   |<span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />![QR Code](https://app.netlify.com/qr-code/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJodHRwczovL2RlcGxveS1wcmV2aWV3LTQyMTIwLS1zdXBlcnNldC1kb2NzLXByZXZpZXcubmV0bGlmeS5hcHAifQ.unyDOW0DO0mkw4Petj16UYjVFeIn2BnSaJ1DzDkmUjc)<br /><br />_Use your smartphone camera to open QR code link._</details> |
   |<span aria-hidden="true">🤖</span> Make changes | [Run an agent on this branch](https://app.netlify.com/projects/superset-docs-preview/agent-runs#DcpBCoAgEEDRu8w6QSzUukHHGEdDwVRUoojunn_1F--FUvNZOmwAE5iKifz4I9ysUXUuNZ87IyTv2IUxWOwhp0GtKzE_ux1YohSr0sSVIOIjvcxCSwPfDw) |
   ---
   <!-- [superset-docs-preview Preview](https://deploy-preview-42120--superset-docs-preview.netlify.app) -->
   _To edit notification comments on pull requests, go to your [Netlify project configuration](https://app.netlify.com/projects/superset-docs-preview/configuration/notifications#deploy-notifications)._


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42120:
URL: https://github.com/apache/superset/pull/42120#issuecomment-5007180357

   <!-- Bito Reply -->
   The warning message can be updated to better reflect that an error payload is being cached, and the logic can be adjusted to avoid redundant warnings when the status is already set to ERROR. You can modify the `else` block in `compute_and_cache` to check the current status before logging the warning.
   
   ```python
                   else:
                       if invalid_reason and cache_payload.status != StatusValues.ERROR:
                           logger.warning(
                               "Caching error payload for %s: %s image payload",
                               cache_key,
                               invalid_reason,
                           )
                       if cache_payload.status != StatusValues.ERROR:
                           cache_payload.error()
   ```
   
   **superset/utils/screenshots.py**
   ```
   else:
                       if invalid_reason and cache_payload.status != StatusValues.ERROR:
                           logger.warning(
                               "Caching error payload for %s: %s image payload",
                               cache_key,
                               invalid_reason,
                           )
                       if cache_payload.status != StatusValues.ERROR:
                           cache_payload.error()
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #42120:
URL: https://github.com/apache/superset/pull/42120#issuecomment-5064238825

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/42120?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `14.81481%` with `23 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 55.62%. Comparing base ([`b459601`](https://app.codecov.io/gh/apache/superset/commit/b459601ceb57b4f9a4e96f4bacd74a65223ce28a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`4800a88`](https://app.codecov.io/gh/apache/superset/commit/4800a885f39653c0d9b14b1e41934dc8bede153b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 190 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/42120?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [superset/utils/screenshots.py](https://app.codecov.io/gh/apache/superset/pull/42120?src=pr&el=tree&filepath=superset%2Futils%2Fscreenshots.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvdXRpbHMvc2NyZWVuc2hvdHMucHk=) | 14.81% | [23 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/42120?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   > :exclamation:  There is a different number of reports uploaded between BASE (b459601) and HEAD (4800a88). Click for more details.
   > 
   > <details><summary>HEAD has 7 uploads less than BASE</summary>
   >
   >| Flag | BASE (b459601) | HEAD (4800a88) |
   >|------|------|------|
   >|python|8|1|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #42120      +/-   ##
   ==========================================
   - Coverage   65.08%   55.62%   -9.46%     
   ==========================================
     Files        2752     2785      +33     
     Lines      154475   156870    +2395     
     Branches    35404    35791     +387     
   ==========================================
   - Hits       100544    87266   -13278     
   - Misses      52019    68780   +16761     
   + Partials     1912      824    -1088     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [hive](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [mysql](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [postgres](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.34% <14.81%> (-0.65%)` | :arrow_down: |
   | [python](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.34% <14.81%> (-18.90%)` | :arrow_down: |
   | [sqlite](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/42120/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/superset/pull/42120?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42120:
URL: https://github.com/apache/superset/pull/42120#issuecomment-5065287907

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "rusackas (via GitHub)" <gi...@apache.org>.
rusackas commented on PR #42120:
URL: https://github.com/apache/superset/pull/42120#issuecomment-5006225514

   Thanks Elizabeth! LGTM on the approach, and thanks for the tests.
   
   One nit: since `validate_screenshot_image` already returns "empty" for falsy bytes, is the `image and` in `compute_and_cache`'s check redundant, or am I missing something?
   
   Will take another look once CI's green.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(screenshots): validate cached screenshot image bytes on read and write [superset]

Posted by "eschutho (via GitHub)" <gi...@apache.org>.
eschutho merged PR #42120:
URL: https://github.com/apache/superset/pull/42120


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org