Repository navigation
fix(ios): require a caller token on the native schemes #902
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6daafe3
fix: send the native scheme caller token from the page
dcalhoun 2ea9f7b
fix(ios): require a caller token on the native schemes
dcalhoun 98a0d01
docs: describe the caller token on the native schemes
dcalhoun effa6e7
test(ios): check a sandboxed frame can't use the native schemes (#904)
jkmassel 60356b3
refactor(ios): use system primitives for the caller token
dcalhoun 9773ba8
docs: note that the upload scheme answers preflights without the token
dcalhoun cdce298
fix(ios): match the caller token prefix in UTF-8
dcalhoun 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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
ios/Sources/GutenbergKit/Sources/Media/SchemeCallerToken.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,44 @@ | ||
| import Foundation | ||
|
|
||
| /// A per-editor secret that a request to the native URL schemes must carry. | ||
| /// | ||
| /// Native code hands it to the editor page in `window.GBKit`, which is injected into | ||
| /// the main frame only. The page sends it in ``header`` on every `gbk-rest:` and | ||
| /// `gbk-upload:` request, and ``RestRelaySchemeHandler`` and | ||
| /// ``MediaUploadSchemeHandler`` refuse a request without it. A frame that can't read | ||
| /// the page's `GBKit` can't learn it, so it can't use the schemes. Mirrors the token | ||
| /// Android's loopback `HttpServer` requires. | ||
| /// | ||
| /// One token serves both schemes for the editor's lifetime. It is never stored. | ||
| struct SchemeCallerToken: Sendable { | ||
| /// The request header that carries the token, as `Bearer <token>`. Android's | ||
| /// loopback server takes the same header. Never sent on to the site. | ||
| static let header = "Relay-Authorization" | ||
|
|
||
| /// The token, as 64 lowercase hexadecimal characters. | ||
| let value: String | ||
|
|
||
| /// A new random token. | ||
| init() { | ||
| var generator = SystemRandomNumberGenerator() | ||
| let words = (0..<4).map { _ in generator.next() } // 4 × UInt64 = 256 bits | ||
| self.value = words.map { String(format: "%016llx", $0) }.joined() | ||
| } | ||
|
|
||
| init(value: String) { | ||
| self.value = value | ||
| } | ||
|
|
||
| /// Whether `request` carries this token in ``header``. | ||
| func authorizes(_ request: URLRequest) -> Bool { | ||
| let prefix = "Bearer ".utf8 | ||
| guard let sent = request.value(forHTTPHeaderField: Self.header)?.utf8, | ||
| sent.starts(with: prefix) else { | ||
| return false | ||
| } | ||
| let provided = Array(sent.dropFirst(prefix.count)) | ||
| let expected = Array(value.utf8) | ||
| return provided.count == expected.count | ||
| && timingsafe_bcmp(provided, expected, expected.count) == 0 | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
This should be infallible as written, but to avoid the
precondition(which looks a little scary), WDYT about using the infallible-by-contractSystemRandomNumberGenerator? With it,import Securityon line 2 can go too.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.
Addressed in 60356b3.