Skip to content

Commit f8fb415

Browse files
authored
Breaking change/return input method (#4)
As we would like to be able to retrieve the id of the used keyboard, we change the isCurrentKeyboardSafe method to getCurrentInputMethodInfo. The new method retrieves the id of the used keyboard, along with a safety verification (comparison with the three main keyboards currently used).
1 parent 970a474 commit f8fb415

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ Mitigating this threat is achieved by:
136136
- On iOS, doing nothing specific since iOS already prevent the use of third-party keyboard on sensitive fields such as passwords.
137137

138138
```tsx
139-
import { SafeKeyboardDetector } from '@bam.tech/react-native-app-security';
139+
import { SafeKeyboardDetector } from "@bam.tech/react-native-app-security";
140140

141-
const isCurrentKeyboardSafe = SafeKeyboardDetector.isCurrentKeyboardSafe() // will always return true on iOS
141+
const { isInDefaultSafeList, inputMethodId } = getCurrentInputMethodInfo(); // Will always return {isInDefaultSafeList: true, inputMethodId: "iosKeyboard"} on iOS
142+
if (!isInDefaultSafeList) {
143+
console.warn(`Your current keyboard (${inputMethodId}) is not safe`);
144+
}
142145

143146
// Prompt the user to change the current keyboard
144-
SafeKeyboardDetector.showInputMethodPicker() // can only be called on Android
147+
SafeKeyboardDetector.showInputMethodPicker(); // can only be called on Android
145148
```
146149

147150
# Contributing

android/src/main/java/tech/bam/rnas/RNASModule.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ class RNASModule : Module() {
1717
}
1818

1919

20-
Function("isCurrentKeyboardSafe") { customPackagesList: Array<String>? ->
20+
Function("getCurrentInputMethodInfo") {
2121
val currentKeyboardId =
2222
Settings.Secure.getString(context.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)
23+
val isInDefaultSafeList = isKeyboardSafe(currentKeyboardId)
2324

24-
return@Function isKeyboardSafe(currentKeyboardId, customPackagesList)
25+
return@Function mapOf(
26+
"isInDefaultSafeList" to isInDefaultSafeList,
27+
"inputMethodId" to currentKeyboardId.substringBefore('/')
28+
)
2529
}
2630
}
2731
private val context
@@ -52,7 +56,6 @@ fun doesPackageNameMatch(input: String, allowedPackagesList: Array<String>): Boo
5256
return allowedPackagesList.any { packageName.matches("${Regex.escape(it)}.*".toRegex()) }
5357
}
5458

55-
fun isKeyboardSafe(keyboardID: String, customAllowedKeyboardPackagesList: Array<String>? = defaultAllowedKeyboardPackagesList): Boolean {
56-
val allowedPackagesList = customAllowedKeyboardPackagesList ?: defaultAllowedKeyboardPackagesList
57-
return doesPackageNameMatch(keyboardID, allowedPackagesList)
58-
}
59+
fun isKeyboardSafe(keyboardID: String): Boolean {
60+
return doesPackageNameMatch(keyboardID, defaultAllowedKeyboardPackagesList)
61+
}

example/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const fetchInvalid = async () => {
6868
};
6969

7070
const checkIsKeyboardSafe = () => {
71-
const isKeyboardSafe = SafeKeyboardDetector.isCurrentKeyboardSafe();
71+
const isKeyboardSafe =
72+
SafeKeyboardDetector.getCurrentInputMethodInfo().isInDefaultSafeList;
73+
console.log(SafeKeyboardDetector.getCurrentInputMethodInfo().inputMethodId);
7274
console.warn("is Keyboard safe", isKeyboardSafe);
7375
};

ios/RNASModule.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class RNASModule: Module {
99
throw InputMethodPickerUnavailableException()
1010
}
1111

12-
Function("isCurrentKeyboardSafe") {() in
13-
return true
12+
Function("getCurrentInputMethodInfo") {() in
13+
return ["isInDefaultSafeList": true, "inputMethodId": "iosKeyboard"]
1414
}
1515
}
1616
}
1717

1818

19+
20+
1921
internal class InputMethodPickerUnavailableException: Exception {
2022
override var reason: String {
2123
return "Method not implemented on iOS since third-party keyboards security issues are not relevant on iOS."

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import RNASModule from "./RNASModule";
22
import { SafeKeyboardDetectorInterface } from "./types";
33

44
export const SafeKeyboardDetector: SafeKeyboardDetectorInterface = {
5-
isCurrentKeyboardSafe: RNASModule.isCurrentKeyboardSafe,
65
showInputMethodPicker: RNASModule.showInputMethodPicker,
6+
getCurrentInputMethodInfo: RNASModule.getCurrentInputMethodInfo,
77
};

src/types.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
export type SafeKeyboardDetectorInterface ={
1+
export type SafeKeyboardDetectorInterface = {
22
/**
3-
* Compare the current keyboard package name with a list of safe keyboard package names on Android
4-
* Will always return true on iOS
5-
*
6-
* @param customAllowedKeyboardList a list keyboard package names. If not provided, a default list is used.
3+
* Compare the current keyboard package name with a list of default safe keyboard package names on Android
4+
* Will always return {isInDefaultSafeList: true, inputMethodId: "iosKeyboard"} on iOS
75
*
86
* @example
9-
* const isSafe = isCurrentKeyboardSafe(["com.touchtype.swiftkey", "com.samsung.android", "com.google.android"])
7+
* const { isInDefaultSafeList, inputMethodId } = getCurrentInputMethodInfo();
8+
* if (!isInDefaultSafeList) {
9+
* console.warn(`Your current keyboard (${inputMethodId}) is not safe`);
10+
* }
1011
*/
11-
isCurrentKeyboardSafe: (customAllowedKeyboardList?: string[]) => boolean;
12+
getCurrentInputMethodInfo: () => {
13+
isInDefaultSafeList: boolean;
14+
inputMethodId: string;
15+
};
1216
/**
1317
* Prompt the user to change his current keyboard to a safe one.
1418
* Will throw an error if used on iOS
1519
*/
1620
showInputMethodPicker: () => void;
17-
}
21+
};

0 commit comments

Comments
 (0)