@onekeyfe/react-native-sni-connect 6.7.0-5 → 6.7.0-51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -65
- package/android/src/main/java/com/sniconnect/SniConnectModule.kt +144 -13
- package/android/src/main/java/com/sniconnect/SniConnectValidation.kt +68 -0
- package/android/src/main/java/com/sniconnect/SniPinnedTransport.kt +89 -1
- package/android/src/test/java/com/sniconnect/SniConnectValidationTest.kt +235 -1
- package/ios/SniConnect.mm +27 -0
- package/ios/SniConnect.swift +63 -0
- package/ios/SniConnectClient.swift +27 -4
- package/ios/SniConnectValidation.swift +71 -0
- package/ios/Tests/SniConnectValidationTests/SniConnectValidationTests.swift +113 -0
- package/lib/module/NativeSniConnect.js +6 -0
- package/lib/module/index.js +11 -0
- package/lib/typescript/src/NativeSniConnect.d.ts +15 -0
- package/lib/typescript/src/index.d.ts +4 -2
- package/package.json +7 -7
- package/src/NativeSniConnect.ts +20 -0
- package/src/index.tsx +18 -0
|
@@ -550,6 +550,119 @@ final class SniConnectValidationTests: XCTestCase {
|
|
|
550
550
|
])
|
|
551
551
|
}
|
|
552
552
|
|
|
553
|
+
func testUploadMethodAllowsOnlyPostAndPut() {
|
|
554
|
+
XCTAssertNoThrow(try SniConnectValidation.validateUploadMethod("POST"))
|
|
555
|
+
XCTAssertNoThrow(try SniConnectValidation.validateUploadMethod("PUT"))
|
|
556
|
+
["GET", "HEAD", "PATCH", "DELETE", "OPTIONS"].forEach { method in
|
|
557
|
+
assertValidationFails {
|
|
558
|
+
try SniConnectValidation.validateUploadMethod(method)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
func testResolveUploadFileAcceptsSandboxFileByPathAndFileURL() throws {
|
|
564
|
+
let root = try makeUploadRoot()
|
|
565
|
+
defer { try? FileManager.default.removeItem(at: root) }
|
|
566
|
+
let file = root.appendingPathComponent("logs.zip")
|
|
567
|
+
try Data([0x50, 0x4B, 0x03, 0x04, 0xFF]).write(to: file)
|
|
568
|
+
|
|
569
|
+
let byPath = try SniConnectValidation.resolveUploadFile(file.path, allowedRoots: [root])
|
|
570
|
+
XCTAssertEqual(byPath.sizeBytes, 5)
|
|
571
|
+
XCTAssertEqual(
|
|
572
|
+
byPath.url.path,
|
|
573
|
+
file.standardizedFileURL.resolvingSymlinksInPath().path
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
let byURL = try SniConnectValidation.resolveUploadFile(
|
|
577
|
+
file.absoluteString,
|
|
578
|
+
allowedRoots: [root]
|
|
579
|
+
)
|
|
580
|
+
XCTAssertEqual(byURL.url.path, byPath.url.path)
|
|
581
|
+
|
|
582
|
+
let bySingleSlashURL = try SniConnectValidation.resolveUploadFile(
|
|
583
|
+
"file:" + file.path,
|
|
584
|
+
allowedRoots: [root]
|
|
585
|
+
)
|
|
586
|
+
XCTAssertEqual(bySingleSlashURL.url.path, byPath.url.path)
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
func testResolveUploadFileRejectsInvalidOrOutOfSandboxFiles() throws {
|
|
590
|
+
let root = try makeUploadRoot()
|
|
591
|
+
let outside = try makeUploadRoot()
|
|
592
|
+
defer {
|
|
593
|
+
try? FileManager.default.removeItem(at: root)
|
|
594
|
+
try? FileManager.default.removeItem(at: outside)
|
|
595
|
+
}
|
|
596
|
+
let outsideFile = outside.appendingPathComponent("secret.bin")
|
|
597
|
+
try Data([1, 2, 3]).write(to: outsideFile)
|
|
598
|
+
let emptyFile = root.appendingPathComponent("empty.zip")
|
|
599
|
+
try Data().write(to: emptyFile)
|
|
600
|
+
let link = root.appendingPathComponent("link.zip")
|
|
601
|
+
try FileManager.default.createSymbolicLink(at: link, withDestinationURL: outsideFile)
|
|
602
|
+
|
|
603
|
+
[
|
|
604
|
+
"",
|
|
605
|
+
"relative/logs.zip",
|
|
606
|
+
"https://example.com/logs.zip",
|
|
607
|
+
outsideFile.path,
|
|
608
|
+
root.appendingPathComponent("../\(outside.lastPathComponent)/secret.bin").path,
|
|
609
|
+
link.path,
|
|
610
|
+
emptyFile.path,
|
|
611
|
+
root.path,
|
|
612
|
+
root.appendingPathComponent("missing.zip").path,
|
|
613
|
+
root.path + "/logs\n.zip",
|
|
614
|
+
].forEach { path in
|
|
615
|
+
assertValidationFails {
|
|
616
|
+
_ = try SniConnectValidation.resolveUploadFile(path, allowedRoots: [root])
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
func testResolveUploadFileRejectsFilesOverTheSizeLimit() throws {
|
|
622
|
+
let root = try makeUploadRoot()
|
|
623
|
+
defer { try? FileManager.default.removeItem(at: root) }
|
|
624
|
+
let file = root.appendingPathComponent("big.zip")
|
|
625
|
+
FileManager.default.createFile(atPath: file.path, contents: nil)
|
|
626
|
+
let handle = try FileHandle(forWritingTo: file)
|
|
627
|
+
try handle.truncate(atOffset: UInt64(SniConnectValidation.maxUploadFileBytes + 1))
|
|
628
|
+
try handle.close()
|
|
629
|
+
|
|
630
|
+
assertValidationFails {
|
|
631
|
+
_ = try SniConnectValidation.resolveUploadFile(file.path, allowedRoots: [root])
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
func testReadUploadFileAcceptsExactLimitBinaryData() throws {
|
|
636
|
+
let root = try makeUploadRoot()
|
|
637
|
+
defer { try? FileManager.default.removeItem(at: root) }
|
|
638
|
+
let file = root.appendingPathComponent("exact.zip")
|
|
639
|
+
let expected = Data(repeating: 0xFF, count: SniConnectValidation.maxUploadFileBytes)
|
|
640
|
+
try expected.write(to: file)
|
|
641
|
+
XCTAssertEqual(try SniConnectValidation.readUploadFile(file), expected)
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
func testReadUploadFileRejectsGrowthAndTruncationAfterValidation() throws {
|
|
645
|
+
let root = try makeUploadRoot()
|
|
646
|
+
defer { try? FileManager.default.removeItem(at: root) }
|
|
647
|
+
let file = root.appendingPathComponent("changing.zip")
|
|
648
|
+
try Data([1]).write(to: file)
|
|
649
|
+
let validated = try SniConnectValidation.resolveUploadFile(file.path, allowedRoots: [root])
|
|
650
|
+
let handle = try FileHandle(forWritingTo: file)
|
|
651
|
+
defer { try? handle.close() }
|
|
652
|
+
// A sparse file exercises the consumption bound without a large fixture.
|
|
653
|
+
try handle.truncate(atOffset: UInt64(SniConnectValidation.maxUploadFileBytes * 4))
|
|
654
|
+
XCTAssertThrowsError(try SniConnectValidation.readUploadFile(validated.url))
|
|
655
|
+
try handle.truncate(atOffset: 0)
|
|
656
|
+
XCTAssertThrowsError(try SniConnectValidation.readUploadFile(validated.url))
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
private func makeUploadRoot() throws -> URL {
|
|
660
|
+
let root = FileManager.default.temporaryDirectory
|
|
661
|
+
.appendingPathComponent("sni-upload-\(UUID().uuidString)", isDirectory: true)
|
|
662
|
+
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
|
663
|
+
return root
|
|
664
|
+
}
|
|
665
|
+
|
|
553
666
|
private func assertValidationFails(_ block: () throws -> Void, file: StaticString = #filePath, line: UInt = #line) {
|
|
554
667
|
XCTAssertThrowsError(try block(), file: file, line: line)
|
|
555
668
|
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { NativeModules, Platform, TurboModuleRegistry } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Sends the raw bytes of an app-sandbox file as the request body over the same
|
|
7
|
+
* pinned-IP/SNI transport as `request()`. Set `content-type` in `headers`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
4
10
|
const LINKING_ERROR = `The package '@onekeyfe/react-native-sni-connect' doesn't seem to be linked. Make sure:\n\n` + Platform.select({
|
|
5
11
|
ios: "- You have run 'pod install'\n",
|
|
6
12
|
default: ''
|
package/lib/module/index.js
CHANGED
|
@@ -4,6 +4,17 @@ import NativeSniConnect from "./NativeSniConnect.js";
|
|
|
4
4
|
export function request(config) {
|
|
5
5
|
return NativeSniConnect.request(config);
|
|
6
6
|
}
|
|
7
|
+
export function isUploadFileSupported() {
|
|
8
|
+
return typeof NativeSniConnect.uploadFile === 'function';
|
|
9
|
+
}
|
|
10
|
+
export async function uploadFile(config) {
|
|
11
|
+
if (!isUploadFileSupported()) {
|
|
12
|
+
throw Object.assign(new Error('Native SNI file upload is not supported'), {
|
|
13
|
+
code: 'SNI_UNSUPPORTED'
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return NativeSniConnect.uploadFile(config);
|
|
17
|
+
}
|
|
7
18
|
export function cancelRequest(requestId) {
|
|
8
19
|
return NativeSniConnect.cancelRequest(requestId);
|
|
9
20
|
}
|
|
@@ -18,6 +18,20 @@ type NativeSniConnectRequest = SniConnectRequestBase & {
|
|
|
18
18
|
method: string;
|
|
19
19
|
body?: string | null;
|
|
20
20
|
};
|
|
21
|
+
type NativeSniConnectUploadFileRequest = SniConnectRequestBase & {
|
|
22
|
+
method: string;
|
|
23
|
+
filePath: string;
|
|
24
|
+
};
|
|
25
|
+
export type SniConnectUploadFileMethod = 'POST' | 'PUT';
|
|
26
|
+
/**
|
|
27
|
+
* Sends the raw bytes of an app-sandbox file as the request body over the same
|
|
28
|
+
* pinned-IP/SNI transport as `request()`. Set `content-type` in `headers`.
|
|
29
|
+
*/
|
|
30
|
+
export type SniConnectUploadFileRequest = SniConnectRequestBase & {
|
|
31
|
+
method: SniConnectUploadFileMethod;
|
|
32
|
+
/** Absolute path or `file://` URL inside the app sandbox, at most 20 MiB. */
|
|
33
|
+
filePath: string;
|
|
34
|
+
};
|
|
21
35
|
export type SniConnectBodylessMethod = 'GET' | 'HEAD';
|
|
22
36
|
export type SniConnectRequiredBodyMethod = 'POST' | 'PUT' | 'PATCH';
|
|
23
37
|
export type SniConnectOptionalBodyMethod = 'DELETE' | 'OPTIONS';
|
|
@@ -53,6 +67,7 @@ export type SniConnectDebugSnapshot = {
|
|
|
53
67
|
};
|
|
54
68
|
export interface Spec extends TurboModule {
|
|
55
69
|
request(config: NativeSniConnectRequest): Promise<SniConnectResponse>;
|
|
70
|
+
uploadFile(config: NativeSniConnectUploadFileRequest): Promise<SniConnectResponse>;
|
|
56
71
|
cancelRequest(requestId: string): Promise<{
|
|
57
72
|
success: boolean;
|
|
58
73
|
}>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { type SniConnectDebugSnapshot, type SniConnectDebugTarget, type SniConnectRequest, type SniConnectResponse } from './NativeSniConnect';
|
|
1
|
+
import { type SniConnectDebugSnapshot, type SniConnectDebugTarget, type SniConnectRequest, type SniConnectResponse, type SniConnectUploadFileRequest } from './NativeSniConnect';
|
|
2
2
|
export declare function request(config: SniConnectRequest): Promise<SniConnectResponse>;
|
|
3
|
+
export declare function isUploadFileSupported(): boolean;
|
|
4
|
+
export declare function uploadFile(config: SniConnectUploadFileRequest): Promise<SniConnectResponse>;
|
|
3
5
|
export declare function cancelRequest(requestId: string): Promise<{
|
|
4
6
|
success: boolean;
|
|
5
7
|
}>;
|
|
@@ -11,5 +13,5 @@ export declare function clearDNSCache(): Promise<{
|
|
|
11
13
|
}>;
|
|
12
14
|
export declare function getDebugSnapshot(target: SniConnectDebugTarget): Promise<SniConnectDebugSnapshot>;
|
|
13
15
|
export declare function isProxyActiveForUrl(url: string): Promise<boolean>;
|
|
14
|
-
export type { SniConnectBodylessMethod, SniConnectDebugSnapshot, SniConnectDebugTarget, SniConnectMethod, SniConnectOptionalBodyMethod, SniConnectRequest, SniConnectRequiredBodyMethod, SniConnectResponse, } from './NativeSniConnect';
|
|
16
|
+
export type { SniConnectBodylessMethod, SniConnectDebugSnapshot, SniConnectDebugTarget, SniConnectMethod, SniConnectOptionalBodyMethod, SniConnectRequest, SniConnectRequiredBodyMethod, SniConnectResponse, SniConnectUploadFileMethod, SniConnectUploadFileRequest, } from './NativeSniConnect';
|
|
15
17
|
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/react-native-sni-connect",
|
|
3
|
-
"version": "6.7.0-
|
|
3
|
+
"version": "6.7.0-51",
|
|
4
4
|
"description": "A React Native library for SNI-based HTTP requests with DNS caching and request management",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -65,11 +65,11 @@
|
|
|
65
65
|
"@eslint/compat": "^1.3.2",
|
|
66
66
|
"@eslint/eslintrc": "^3.3.1",
|
|
67
67
|
"@eslint/js": "^9.35.0",
|
|
68
|
-
"@react-native/babel-preset": "0.
|
|
69
|
-
"@react-native/eslint-config": "0.
|
|
68
|
+
"@react-native/babel-preset": "0.88.0",
|
|
69
|
+
"@react-native/eslint-config": "0.88.0",
|
|
70
70
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
71
71
|
"@types/jest": "^29.5.14",
|
|
72
|
-
"@types/react": "^19.
|
|
72
|
+
"@types/react": "^19.3.0",
|
|
73
73
|
"commitlint": "^19.8.1",
|
|
74
74
|
"del-cli": "^6.0.0",
|
|
75
75
|
"eslint": "^9.35.0",
|
|
@@ -78,8 +78,8 @@
|
|
|
78
78
|
"jest": "^29.7.0",
|
|
79
79
|
"lefthook": "^2.0.3",
|
|
80
80
|
"prettier": "^2.8.8",
|
|
81
|
-
"react": "19.
|
|
82
|
-
"react-native": "patch:react-native@npm%3A0.
|
|
81
|
+
"react": "19.3.0",
|
|
82
|
+
"react-native": "patch:react-native@npm%3A0.88.0#~/.yarn/patches/react-native-npm-0.88.0-60c6201dd2.patch",
|
|
83
83
|
"react-native-builder-bob": "^0.40.17",
|
|
84
84
|
"release-it": "^19.0.4",
|
|
85
85
|
"turbo": "^2.5.6",
|
|
@@ -128,7 +128,7 @@
|
|
|
128
128
|
"useTabs": false
|
|
129
129
|
},
|
|
130
130
|
"jest": {
|
|
131
|
-
"preset": "react-native",
|
|
131
|
+
"preset": "@react-native/jest-preset",
|
|
132
132
|
"modulePathIgnorePatterns": [
|
|
133
133
|
"<rootDir>/example/node_modules",
|
|
134
134
|
"<rootDir>/lib/"
|
package/src/NativeSniConnect.ts
CHANGED
|
@@ -28,6 +28,23 @@ type NativeSniConnectRequest = SniConnectRequestBase & {
|
|
|
28
28
|
body?: string | null;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
type NativeSniConnectUploadFileRequest = SniConnectRequestBase & {
|
|
32
|
+
method: string;
|
|
33
|
+
filePath: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type SniConnectUploadFileMethod = 'POST' | 'PUT';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sends the raw bytes of an app-sandbox file as the request body over the same
|
|
40
|
+
* pinned-IP/SNI transport as `request()`. Set `content-type` in `headers`.
|
|
41
|
+
*/
|
|
42
|
+
export type SniConnectUploadFileRequest = SniConnectRequestBase & {
|
|
43
|
+
method: SniConnectUploadFileMethod;
|
|
44
|
+
/** Absolute path or `file://` URL inside the app sandbox, at most 20 MiB. */
|
|
45
|
+
filePath: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
31
48
|
export type SniConnectBodylessMethod = 'GET' | 'HEAD';
|
|
32
49
|
export type SniConnectRequiredBodyMethod = 'POST' | 'PUT' | 'PATCH';
|
|
33
50
|
export type SniConnectOptionalBodyMethod = 'DELETE' | 'OPTIONS';
|
|
@@ -74,6 +91,9 @@ export type SniConnectDebugSnapshot = {
|
|
|
74
91
|
|
|
75
92
|
export interface Spec extends TurboModule {
|
|
76
93
|
request(config: NativeSniConnectRequest): Promise<SniConnectResponse>;
|
|
94
|
+
uploadFile(
|
|
95
|
+
config: NativeSniConnectUploadFileRequest
|
|
96
|
+
): Promise<SniConnectResponse>;
|
|
77
97
|
cancelRequest(requestId: string): Promise<{ success: boolean }>;
|
|
78
98
|
cancelAllRequests(): Promise<{ success: boolean }>;
|
|
79
99
|
clearDNSCache(): Promise<{ success: boolean }>;
|
package/src/index.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import NativeSniConnect, {
|
|
|
3
3
|
type SniConnectDebugTarget,
|
|
4
4
|
type SniConnectRequest,
|
|
5
5
|
type SniConnectResponse,
|
|
6
|
+
type SniConnectUploadFileRequest,
|
|
6
7
|
} from './NativeSniConnect';
|
|
7
8
|
|
|
8
9
|
export function request(
|
|
@@ -11,6 +12,21 @@ export function request(
|
|
|
11
12
|
return NativeSniConnect.request(config);
|
|
12
13
|
}
|
|
13
14
|
|
|
15
|
+
export function isUploadFileSupported(): boolean {
|
|
16
|
+
return typeof NativeSniConnect.uploadFile === 'function';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function uploadFile(
|
|
20
|
+
config: SniConnectUploadFileRequest
|
|
21
|
+
): Promise<SniConnectResponse> {
|
|
22
|
+
if (!isUploadFileSupported()) {
|
|
23
|
+
throw Object.assign(new Error('Native SNI file upload is not supported'), {
|
|
24
|
+
code: 'SNI_UNSUPPORTED',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return NativeSniConnect.uploadFile(config);
|
|
28
|
+
}
|
|
29
|
+
|
|
14
30
|
export function cancelRequest(
|
|
15
31
|
requestId: string
|
|
16
32
|
): Promise<{ success: boolean }> {
|
|
@@ -44,4 +60,6 @@ export type {
|
|
|
44
60
|
SniConnectRequest,
|
|
45
61
|
SniConnectRequiredBodyMethod,
|
|
46
62
|
SniConnectResponse,
|
|
63
|
+
SniConnectUploadFileMethod,
|
|
64
|
+
SniConnectUploadFileRequest,
|
|
47
65
|
} from './NativeSniConnect';
|