-
Notifications
You must be signed in to change notification settings - Fork 6
fix(ios): gate the media upload server on the site root too #624
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
Merged
jkmassel
merged 2 commits into
fix/register-core-media-upload-middleware
from
fix/media-server-site-root-guard
Oct 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
24 changes: 24 additions & 0 deletions
24
ios/Sources/GutenbergKit/Sources/Media/MediaServerCredentials.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import Foundation | ||
|
|
||
| /// Whether the editor configuration can reach the configured site for media. | ||
| /// | ||
| /// Deliberately outside `EditorViewController`. That type is `#if canImport(UIKit)`, | ||
| /// so on the macOS host it does not exist and nothing in it can be tested — including | ||
| /// this check, which already diverged silently between iOS and Android once. Living | ||
| /// here, it is reachable from the host test suite. | ||
| enum MediaServerCredentials { | ||
| /// Whether a ``DefaultMediaUploader`` built from this configuration could actually | ||
| /// reach the site. | ||
| /// | ||
| /// Both fields are required. The uploader delivers GutenbergKit's uploads to the | ||
| /// configured site, so it needs somewhere to send them and credentials to be | ||
| /// accepted; with either missing, every media request it makes fails. | ||
| /// | ||
| /// `siteApiRoot` is a `URL` here where Android types it as a `String`, so the | ||
| /// equivalent of Android's `isEmpty()` check is "not absolute" — a URL with no | ||
| /// scheme or host cannot address the site, and every request built from it fails at | ||
| /// the URLSession layer. | ||
| static func areUsable(siteApiRoot: URL, authHeader: String) -> Bool { | ||
| siteApiRoot.scheme != nil && siteApiRoot.host() != nil && !authHeader.isEmpty | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
ios/Tests/GutenbergKitTests/Media/MediaServerCredentialsTests.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import GutenbergKit | ||
|
|
||
| @Suite("MediaServerCredentials") | ||
| struct MediaServerCredentialsTests { | ||
| private static let siteRoot = URL(string: "https://example.com/wp-json/")! | ||
|
|
||
| @Test("accepts an absolute site root with an auth header") | ||
| func acceptsUsableCredentials() { | ||
| #expect(MediaServerCredentials.areUsable(siteApiRoot: Self.siteRoot, authHeader: "Bearer t")) | ||
| } | ||
|
|
||
| @Test("rejects an empty auth header") | ||
| func rejectsEmptyAuthHeader() { | ||
| #expect(!MediaServerCredentials.areUsable(siteApiRoot: Self.siteRoot, authHeader: "")) | ||
| } | ||
|
|
||
| // The two arms below are what a `URL` makes different from Android's `String`: | ||
| // `isEmpty()` has no direct equivalent, so "addressable" is spelled as scheme and | ||
| // host both being present. A URL missing either cannot reach the site, and every | ||
| // request built from it fails at the URLSession layer. | ||
|
|
||
| @Test("rejects a site root with no scheme") | ||
| func rejectsSchemelessSiteRoot() { | ||
| // A network-path reference: it has a host, so only the scheme arm rejects it. | ||
| // `example.com/wp-json/` would not do — it parses as a bare path with no host | ||
| // either, and passes on the host arm alone. | ||
| let relative = URL(string: "//example.com/wp-json/")! | ||
| #expect(relative.host() != nil) | ||
| #expect(!MediaServerCredentials.areUsable(siteApiRoot: relative, authHeader: "Bearer t")) | ||
| } | ||
|
|
||
| @Test("rejects a site root with no host") | ||
| func rejectsHostlessSiteRoot() { | ||
| let fileURL = URL(fileURLWithPath: "/tmp/wp-json") | ||
| #expect(!MediaServerCredentials.areUsable(siteApiRoot: fileURL, authHeader: "Bearer t")) | ||
| } | ||
|
|
||
| @Test("rejects a site root with no scheme or host") | ||
| func rejectsRelativeSiteRoot() { | ||
| #expect(!MediaServerCredentials.areUsable(siteApiRoot: URL(string: "/")!, authHeader: "Bearer t")) | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
Finding from Claude:
This widens the gate from
!authHeader.isEmptyto also require an addressablesiteApiRoot— but a delegate that handles uploads itself never touchesDefaultMediaUploader.MediaUploadServer.processAndUploadreturns.uploaded(result)straight fromuploadDelegatewithout resolvingcontext.defaultUploader, so that host now loses its own upload path over a site root it never uses.Intended? If not, the site-root arm belongs where
defaultUploaderis actually built.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.
This one is resolved by #625 – I'd have pulled it into this PR, but it'd be a bit hairy.