-
Notifications
You must be signed in to change notification settings - Fork 4
fix: harden shop payment bridge #668
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c1c629d
fix: gate shop origin and pin pay
ben-kaufman dc0f3e2
chore: rename changelog fragment
ben-kaufman a23456a
fix: keep btc map shop navigation
ben-kaufman 9c215ea
Merge branch 'master' into fix/shop-origin-and-pin-pay
ovitrif a1a2fa8
fix: harden shop payment bridge
ben-kaufman 05921c3
Merge branch 'master' into fix/shop-origin-and-pin-pay
ben-kaufman 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Foundation | ||
|
|
||
| enum ShopOrigin { | ||
| static let rootHost = "bitrefill.com" | ||
|
|
||
| static func isAllowedHost(_ host: String?) -> Bool { | ||
| guard var host = host?.lowercased() else { return false } | ||
| host = host.trimmingCharacters(in: CharacterSet(charactersIn: ".")) | ||
| return host == rootHost || host.hasSuffix(".\(rootHost)") | ||
| } | ||
|
|
||
| static func isAllowed(_ url: URL?) -> Bool { | ||
| guard let url else { return false } | ||
| guard url.scheme?.lowercased() == "https" else { return false } | ||
| return isAllowedHost(url.host) | ||
| } | ||
|
|
||
| static var messageBridgeScript: String { | ||
| """ | ||
| window.addEventListener('message', function(event) { | ||
| try { | ||
| var originUrl = new URL(event.origin); | ||
| if (originUrl.protocol !== 'https:') return; | ||
| var host = originUrl.hostname.toLowerCase(); | ||
| if (host !== '\(rootHost)' && !host.endsWith('.\(rootHost)')) return; | ||
| } catch (e) { | ||
| return; | ||
| } | ||
| window.webkit.messageHandlers.messageHandler.postMessage(JSON.stringify(event.data)); | ||
| }); | ||
| """ | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| @testable import Bitkit | ||
| import XCTest | ||
|
|
||
| final class PaymentNavigationHelperTests: XCTestCase { | ||
| func testQuickpayIsBlockedWhenPinIsRequiredForPayments() { | ||
| XCTAssertTrue( | ||
| PaymentNavigationHelper.isBlockedByPaymentPin(pinEnabled: true, requirePinForPayments: true) | ||
| ) | ||
| } | ||
|
|
||
| func testQuickpayIsAllowedWhenPinForPaymentsIsOff() { | ||
| XCTAssertFalse( | ||
| PaymentNavigationHelper.isBlockedByPaymentPin(pinEnabled: true, requirePinForPayments: false) | ||
| ) | ||
| XCTAssertFalse( | ||
| PaymentNavigationHelper.isBlockedByPaymentPin(pinEnabled: false, requirePinForPayments: true) | ||
| ) | ||
| XCTAssertFalse( | ||
| PaymentNavigationHelper.isBlockedByPaymentPin(pinEnabled: false, requirePinForPayments: false) | ||
| ) | ||
| } | ||
| } |
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,31 @@ | ||
| @testable import Bitkit | ||
| import XCTest | ||
|
|
||
| final class ShopOriginTests: XCTestCase { | ||
| func testHttpsBitrefillHostsAreAllowed() { | ||
| XCTAssertTrue(ShopOrigin.isAllowed(URL(string: "https://embed.bitrefill.com"))) | ||
| XCTAssertTrue(ShopOrigin.isAllowed(URL(string: "https://embed.bitrefill.com/gift-cards"))) | ||
| XCTAssertTrue(ShopOrigin.isAllowed(URL(string: "https://bitrefill.com"))) | ||
| XCTAssertTrue(ShopOrigin.isAllowed(URL(string: "https://www.bitrefill.com/esims"))) | ||
| XCTAssertTrue(ShopOrigin.isAllowedHost("embed.bitrefill.com")) | ||
| XCTAssertTrue(ShopOrigin.isAllowedHost("BITREFILL.COM")) | ||
| } | ||
|
|
||
| func testNonBitrefillAndNonHttpsOriginsAreRejected() { | ||
| XCTAssertFalse(ShopOrigin.isAllowed(nil as URL?)) | ||
| XCTAssertFalse(ShopOrigin.isAllowed(URL(string: "https://evil.example"))) | ||
| XCTAssertFalse(ShopOrigin.isAllowed(URL(string: "https://bitrefill.com.evil.example"))) | ||
| XCTAssertFalse(ShopOrigin.isAllowed(URL(string: "https://notbitrefill.com"))) | ||
| XCTAssertFalse(ShopOrigin.isAllowed(URL(string: "http://embed.bitrefill.com"))) | ||
| XCTAssertFalse(ShopOrigin.isAllowed(URL(string: "javascript:alert(1)"))) | ||
| XCTAssertFalse(ShopOrigin.isAllowedHost("evil.example")) | ||
| XCTAssertFalse(ShopOrigin.isAllowedHost(nil)) | ||
| } | ||
|
|
||
| func testBridgeScriptChecksMessageOrigin() { | ||
| let script = ShopOrigin.messageBridgeScript | ||
| XCTAssertTrue(script.contains("addEventListener('message'")) | ||
| XCTAssertTrue(script.contains("bitrefill.com")) | ||
| XCTAssertFalse(script.contains("window.postMessage =")) | ||
| } | ||
| } |
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 @@ | ||
| Shop checkout only accepts Bitrefill payment requests, and QuickPay now honors PIN protection. |
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.
Uh oh!
There was an error while loading. Please reload this page.