-
Notifications
You must be signed in to change notification settings - Fork 595
Improvements Over Path check in Regex #5830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
1. Allows spaces and special characters commonly found in paths 2. Platform-aware - distinguishes between Windows and Unix paths 3. Excludes invalid characters (*, ?, ", <, >, |) 4. Context-aware - considers file extensions and path structure 5. Better false-positive prevention - excludes URLs and base64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've reviewed this pull request using the Sourcery rules engine
There was a problem hiding this 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 refines the path and URL detection logic in the isUrlOrPath function to be more robust and platform-aware, supporting both Windows and Unix path formats while excluding invalid file path characters.
Key Changes:
- Updated URL regex to use
https?shorthand for matching both HTTP and HTTPS protocols - Replaced simple file path regex with a comprehensive pattern supporting Windows drives/UNC paths, Unix absolute/relative paths, and standalone filenames
- Added exclusion of invalid file path characters (*, ?, ", <, >, |) to prevent false positives
Comments suppressed due to low confidence (1)
packages/flet/lib/src/utils/images.dart:114
- The function
isUrlOrPathlacks test coverage. Consider adding tests to verify:
- Empty string handling
- Various Windows path formats (C:\path\file.txt, \server\share\file.txt)
- Various Unix path formats (~/file.txt, ./file.txt, ../file.txt, /absolute/path)
- Simple filenames (file.txt, image.png)
- Edge cases with invalid characters (*, ?, ", <, >, |)
- URL formats (http://, https://, www.)
- False positives like base64 strings
This is especially important given the complexity of the new regex patterns.
bool isUrlOrPath(String value) {
// Check for URL pattern
final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
if (urlPattern.hasMatch(value)) {
return true;
}
// Check for common file path characters
final filePathPattern = RegExp(
r'^('
r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths
r'|' // OR
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
r'|' // OR
r'[^\\/:*?"<>|\r\n]+' // Just filenames without path separators
r')$'
);
if (filePathPattern.hasMatch(value)) {
return true;
}
return false;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| final filePathPattern = RegExp( | ||
| r'^(' | ||
| r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths | ||
| r'|' // OR | ||
| r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths | ||
| r'|' // OR | ||
| r'[^\\/:*?"<>|\r\n]+' // Just filenames without path separators | ||
| r')$' | ||
| ); |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new file path regex can match empty strings, which is likely unintended. For example:
- The Windows path pattern
([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*has optional components that can all be empty - The Unix path pattern
(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*also has optional components with*quantifiers allowing zero matches
This means isUrlOrPath("") would return true, which is probably not the desired behavior. Consider using + instead of * for the final character class in each path pattern, or adding a length check before the regex matching.
| bool isUrlOrPath(String value) { | ||
| // Check for URL pattern | ||
| final urlPattern = RegExp(r'^(http:\/\/|https:\/\/|www\.)'); | ||
| final urlPattern = RegExp(r'^(https?:\/\/|www\.)'); |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The URL pattern ^(https?:\/\/|www\.) will match strings like "www.example" but also "www." or "http://" on their own. Consider adding a pattern to ensure there's something after the protocol/prefix, such as ^(https?:\/\/.+|www\..+) to prevent matching incomplete URLs.
| final urlPattern = RegExp(r'^(https?:\/\/|www\.)'); | |
| final urlPattern = RegExp(r'^(https?:\/\/.+|www\..+)'); |
| r'^(' | ||
| r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths | ||
| r'|' // OR | ||
| r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Trailing whitespace detected at the end of the comment. Consider removing it for cleaner code.
| r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths | |
| r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths |
|
|
||
| return false; | ||
| } | ||
|
|
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function isUrlOrPath lacks documentation explaining its purpose, parameters, return value, and expected behavior. Given the complexity of the new regex patterns and the specific exclusions mentioned in the PR description (URLs, base64, invalid characters), adding a doc comment would improve maintainability. For example:
/// Checks if the given [value] is a valid URL or file path.
///
/// Returns `true` if [value] matches:
/// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org")
/// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file")
/// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative")
/// - Simple filenames (e.g., "image.png", "document.txt")
///
/// Invalid file path characters (*, ?, ", <, >, |) are excluded.
/// Base64-encoded strings should be checked with [isBase64ImageString].
bool isUrlOrPath(String value) {| /// Checks if the given [value] is a valid URL or file path. | |
| /// | |
| /// Returns `true` if [value] matches: | |
| /// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org") | |
| /// - URLs starting with "www." | |
| /// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file") | |
| /// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative") | |
| /// - Simple filenames (e.g., "image.png", "document.txt") | |
| /// | |
| /// Invalid file path characters (*, ?, ", <, >, |) are excluded. | |
| /// Base64-encoded strings should be checked with [isBase64ImageString]. |
Description
Test Code
# Test code for the review of this PRType of change
Checklist
Screenshots
Additional details
Summary by Sourcery
Refine path and URL detection in isUrlOrPath by using a unified regex that supports Windows and Unix paths, standalone filenames, and HTTPS URLs while excluding invalid characters.
Enhancements: