-
Notifications
You must be signed in to change notification settings - Fork 3
feat: throttle only allowlisted hosts #82
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
Open
originalix
wants to merge
3
commits into
main
Choose a base branch
from
codex/network-throttle-bypass-origins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−13
Open
Changes from all commits
Commits
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
103 changes: 103 additions & 0 deletions
103
native-modules/react-native-network-throttle/src/__tests__/index.test.tsx
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,103 @@ | ||
| jest.mock('react-native', () => ({ | ||
| NativeModules: { | ||
| OneKeyNetworkThrottle: { | ||
| getConfig: jest.fn(), | ||
| setConfig: jest.fn(), | ||
| }, | ||
| }, | ||
| Platform: { | ||
| select: (options: { default: string }) => options.default, | ||
| }, | ||
| })); | ||
|
|
||
| import { NativeModules } from 'react-native'; | ||
|
|
||
| import { NetworkThrottle, type NetworkThrottleConfig } from '../index'; | ||
|
|
||
| const { getConfig: mockGetConfig, setConfig: mockSetConfig } = | ||
| NativeModules.OneKeyNetworkThrottle as { | ||
| getConfig: jest.Mock; | ||
| setConfig: jest.Mock; | ||
| }; | ||
|
|
||
| const baseConfig = { | ||
| enabled: true, | ||
| profile: 'slow4g' as const, | ||
| latencyMs: 562.5, | ||
| downloadBps: 102 * 1024, | ||
| uploadBps: 102 * 1024, | ||
| }; | ||
|
|
||
| describe('NetworkThrottle', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('normalizes config from an older native binary', async () => { | ||
| mockGetConfig.mockResolvedValue(baseConfig); | ||
|
|
||
| await expect(NetworkThrottle.getConfig()).resolves.toEqual({ | ||
| ...baseConfig, | ||
| throttleUrlHosts: [], | ||
| }); | ||
| }); | ||
|
|
||
| it('forwards the throttle allowlist with a complete native config', async () => { | ||
| const throttleUrlHosts = ['*.onekeycn.com']; | ||
| const expectedConfig: NetworkThrottleConfig = { | ||
| ...baseConfig, | ||
| throttleUrlHosts, | ||
| }; | ||
| mockGetConfig.mockResolvedValue({ | ||
| ...baseConfig, | ||
| throttleUrlHosts: [], | ||
| }); | ||
| mockSetConfig.mockResolvedValue(expectedConfig); | ||
|
|
||
| await expect( | ||
| NetworkThrottle.setConfig({ throttleUrlHosts }) | ||
| ).resolves.toEqual(expectedConfig); | ||
| expect(mockSetConfig).toHaveBeenCalledWith(expectedConfig); | ||
| }); | ||
|
|
||
| it('preserves the registered allowlist for unrelated config updates', async () => { | ||
| const currentConfig: NetworkThrottleConfig = { | ||
| ...baseConfig, | ||
| throttleUrlHosts: ['*.onekeycn.com'], | ||
| }; | ||
| const expectedConfig = { | ||
| ...currentConfig, | ||
| enabled: false, | ||
| }; | ||
| mockGetConfig.mockResolvedValue(currentConfig); | ||
| mockSetConfig.mockResolvedValue(expectedConfig); | ||
|
|
||
| await NetworkThrottle.setConfig({ enabled: false }); | ||
|
|
||
| expect(mockSetConfig).toHaveBeenCalledWith(expectedConfig); | ||
| }); | ||
| }); | ||
|
|
||
| // The native side implements this matching in Kotlin and Objective-C; this | ||
| // pins the contract both must satisfy, and mirrors the desktop URL patterns. | ||
| describe('throttle host matching contract', () => { | ||
| const matches = (host: string, pattern: string) => | ||
| pattern.startsWith('*.') | ||
| ? host.endsWith(pattern.slice(1)) | ||
| : host === pattern; | ||
|
|
||
| it.each([ | ||
| ['wallet.onekeycn.com', '*.onekeycn.com', true], | ||
| ['a.b.onekeycn.com', '*.onekeycn.com', true], | ||
| ['uni.onekey-asset.com', '*.onekey-asset.com', true], | ||
| ['app-assets.onekey.so', 'app-assets.onekey.so', true], | ||
| // the bare apex is not a sub-domain | ||
| ['onekeycn.com', '*.onekeycn.com', false], | ||
| // look-alike hosts must not match | ||
| ['evil-onekeycn.com', '*.onekeycn.com', false], | ||
| ['mainnet.infura.io', '*.onekeycn.com', false], | ||
| ['localhost', '*.onekeycn.com', false], | ||
| ])('%s vs %s -> %s', (host, pattern, expected) => { | ||
| expect(matches(host, pattern)).toBe(expected); | ||
| }); | ||
| }); |
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.
P2: [Host updates can only grow, never replace or clear]
This merges the new host array into process-global state instead of replacing the previous set. After a caller once registers
a.example.com, later updates likesetConfig({ throttleUrlHosts: ['b.example.com'] })orsetConfig({ throttleUrlHosts: [] })still leavea.example.comthrottled until the app restarts.The result is that settings changes are not actually applied, and stale domains keep getting slowed unexpectedly. The stored allowlist needs replacement semantics for a single runtime, or a real ownership model with explicit deregistration if multiple runtimes must coexist.