Skip to content

Fix: correct regex for file-preview URL re-signing#25620

Merged
QuantumGhost merged 17 commits intolanggenius:mainfrom
hyongtao-code:fix-re
Sep 22, 2025
Merged

Fix: correct regex for file-preview URL re-signing#25620
QuantumGhost merged 17 commits intolanggenius:mainfrom
hyongtao-code:fix-re

Conversation

@hyongtao-code
Copy link
Copy Markdown
Contributor

@hyongtao-code hyongtao-code commented Sep 12, 2025

Fixes #25619

The regex patterns for file-preview and image-preview contained an unescaped ?,
which caused incorrect matches such as file-previe or image-previw.
This led to malformed URLs being incorrectly re-signed.

Changes:

  • Escape ? in both file-preview and image-preview regex patterns.
  • Ensure only valid URLs are re-signed.

Added unit tests to cover:

  • Valid file-preview and image-preview URLs (correctly re-signed).
  • Misspelled file/image preview URLs (no longer incorrectly matched).

Other:

  • Fix a deprecated function datetime.utcnow()

Related with #24180

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Copilot AI review requested due to automatic review settings September 12, 2025 13:02
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Sep 12, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @hyongtao-code, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug where unescaped ? characters in URL regex patterns caused incorrect matching and re-signing of malformed file-preview and image-preview URLs. The changes ensure robust URL validation and re-signing, preventing potential issues with file access. Additionally, it updates deprecated datetime function calls to improve code maintainability and consistency.

Highlights

  • Regex Correction for URL Re-signing: The regex patterns for file-preview and image-preview URLs have been corrected by escaping the ? character. This prevents incorrect matches and ensures that only valid URLs are considered for re-signing.
  • Enhanced URL Validation: The re-signing mechanism now strictly processes only genuinely valid file-preview and image-preview URLs, avoiding malformed URL re-signing that previously occurred due to the unescaped ?.
  • New Unit Tests: Comprehensive unit tests have been added to validate the corrected URL re-signing logic. These tests cover scenarios for correctly re-signed valid URLs and confirm that misspelled preview URLs are no longer incorrectly matched.
  • Deprecated Function Update: The deprecated datetime.utcnow() function has been replaced with naive_utc_now() across several files, improving code maintainability and addressing best practices for handling timestamps.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a regex vulnerability in URL re-signing logic where unescaped ? characters caused incorrect URL pattern matching for file-preview and image-preview endpoints. The fix ensures only valid URLs are re-signed by properly escaping the regex patterns.

Key changes:

  • Fixed regex patterns by escaping ? characters in file-preview and image-preview URL matching
  • Replaced deprecated datetime.utcnow() calls with naive_utc_now() for future compatibility
  • Added comprehensive unit tests to verify correct behavior for valid and misspelled URLs

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
api/models/model.py Fixed regex patterns by escaping ? in file-preview and image-preview URL matching
api/tests/unit_tests/models/test_model.py Added unit tests covering valid URL re-signing and protection against misspelled URL matching
api/tests/unit_tests/core/repositories/test_workflow_node_execution_conflict_handling.py Replaced deprecated datetime.utcnow() with naive_utc_now()
api/tests/test_containers_integration_tests/tasks/test_batch_clean_document_task.py Replaced deprecated datetime.utcnow() with naive_utc_now()

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a bug in the URL re-signing logic by correcting the regular expressions for file-preview and image-preview URLs. The unescaped ? was causing incorrect matches, and this fix resolves that. The inclusion of new unit tests is excellent, as it covers both the valid cases and the previously failing cases with misspelled URLs, ensuring the fix is robust. Additionally, I appreciate the proactive replacement of the deprecated datetime.utcnow() function, which improves the overall health of the codebase. My feedback includes a minor suggestion to enhance the readability of the new regular expressions.

crazywoola
crazywoola previously approved these changes Sep 12, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Sep 12, 2025
@hyongtao-code hyongtao-code marked this pull request as draft September 12, 2025 13:16
@hyongtao-code hyongtao-code marked this pull request as ready for review September 12, 2025 13:25
Copy link
Copy Markdown
Contributor

@QuantumGhost QuantumGhost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@QuantumGhost QuantumGhost merged commit 5bc6e8a into langgenius:main Sep 22, 2025
11 checks passed
@hyongtao-code hyongtao-code deleted the fix-re branch October 15, 2025 02:19
LawrenceZHLee pushed a commit to LawrenceZHLee/dify that referenced this pull request Oct 28, 2025
Fixes langgenius#25619

The regex patterns for file-preview and image-preview contained an unescaped `?`, 
which caused incorrect matches such as `file-previe` or `image-previw`. 
This led to malformed URLs being incorrectly re-signed.

Changes:
- Escape `?` in both file-preview and image-preview regex patterns.
- Ensure only valid URLs are re-signed.

Added unit tests to cover:
- Valid file-preview and image-preview URLs (correctly re-signed).
- Misspelled file/image preview URLs (no longer incorrectly matched).

Other:
- Fix a deprecated function `datetime.utcnow()`

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chore] correct regex for file-preview URL re-signing

5 participants