Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/runtime/providers/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeQueryItem, joinURL } from 'ufo'
import { encodeQueryItem, hasProtocol, joinURL } from 'ufo'
import { createOperationsGenerator } from '../utils/index'
import { defineProvider } from '../utils/provider'

Expand Down Expand Up @@ -46,7 +46,7 @@ export default defineProvider<CloudflareOptions>({
const operations = operationsGenerator(mergeModifiers as any)

// https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : src
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : hasProtocol(src) ? src : joinURL(baseURL, src)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

Does ufo hasProtocol function return true for protocol-relative URLs like //example.com by default?

💡 Result:

No. In unjs/ufo, hasProtocol("//example.com") returns false by default; it only returns true for protocol-relative (scheme-relative) URLs if you pass { acceptRelative: true }. [1]


Handle protocol-relative URLs in the src parameter.

The code doesn't account for protocol-relative URLs (e.g., //cdn.example.com/image.jpg). Since hasProtocol returns false for these by default, they would be incorrectly joined with baseURL, producing malformed results like https://mysite.com//cdn.example.com/image.jpg.

Use hasProtocol(src, { acceptRelative: true }) to properly detect protocol-relative URLs as absolute:

Fix for protocol-relative URL handling
-    const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : hasProtocol(src) ? src : joinURL(baseURL, src)
+    const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : hasProtocol(src, { acceptRelative: true }) ? src : joinURL(baseURL, src)

Alternatively, the nested ternary could be rewritten as an if-else block for improved readability.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/runtime/providers/cloudflare.ts` at line 49, The URL assembly incorrectly
treats protocol-relative sources as relative because hasProtocol(src) is used;
update the check to hasProtocol(src, { acceptRelative: true }) (or refactor the
ternary into an if/else) so protocol-relative URLs (e.g., //cdn.example.com/...)
are treated as absolute and not joined with baseURL; adjust the expression that
sets url (which uses joinURL, baseURL, operations, src) to use this updated
hasProtocol call and preserve the existing logic for operations and normal
relative paths.


return {
url,
Expand Down
Loading