Android 剪切板敏感信息泄露漏洞解决:禁用EditText的复制粘贴功能

今天药忘吃喽~ 2023-02-21 11:34 71阅读 0赞

Android 中任何第三方软件都可访问剪切板内容,虽然高版本对剪切板做了访问限制,但是还是需要照顾一下低版本的,解决方法就是要禁用复制剪切选项。下边来看具体实现吧。

1、自定义NoMenuEditText 继承自AppCompatEditText

2、重写isSuggestionsEnabled方法并返回false

  1. 创建`canPaste()`方法并返回false。该方法是一个隐藏方法

3、实现ActionMode.Callback回调

  1. private class ActionModeCallbackInterceptor implements ActionMode.Callback {
  2. public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  3. //删除复制选项
  4. MenuItem itemCopy = menu.findItem(android.R.id.copy);
  5. if (itemCopy != null) {
  6. menu.removeItem(android.R.id.copy);
  7. }
  8. //删除剪切选项
  9. MenuItem itemCut = menu.findItem(android.R.id.cut);
  10. if (itemCut != null) {
  11. menu.removeItem(android.R.id.cut);
  12. }
  13. return true;
  14. }
  15. public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  16. return false;
  17. }
  18. public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  19. return false;
  20. }
  21. public void onDestroyActionMode(ActionMode mode) {
  22. }
  23. }

4、设置回调

  1. this.setLongClickable(false);
  2. //this.setTextIsSelectable(false);
  3. this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
  4. //使用该方式基本可以实现禁用粘贴复制功能,6.0以上可用
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  6. this.setCustomInsertionActionModeCallback(new ActionModeCallbackInterceptor());
  7. }

使用以上方式即可禁用掉EditText的复制粘贴功能,但是长按EditTeit控件中的文本时光标会有变化,并且该方式只能在6.0以上版本中使用。如果以上方式不能满足你的需求,请看以下方式:

1、2、3步同上,修改第4步不再用 this.setCustomInsertionActionModeCallback(new ActionModeCallbackInterceptor())方法。

使用以下方式(反射)代替:

  1. this.setOnTouchListener(new OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. NoMenuEditText.this.clearFocus();
  5. return false;
  6. }
  7. });
  8. @SuppressLint("ClickableViewAccessibility")
  9. @Override
  10. public boolean onTouchEvent(MotionEvent event) {
  11. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  12. // setInsertionDisabled when user touches the view
  13. this.setInsertionDisabled();
  14. }
  15. return super.onTouchEvent(event);
  16. }
  17. private void setInsertionDisabled() {
  18. try {
  19. Field editorField = TextView.class.getDeclaredField("mEditor");
  20. editorField.setAccessible(true);
  21. Object editorObject = editorField.get(this);
  22. @SuppressLint("PrivateApi") Class editorClass = Class.forName("android.widget.Editor");
  23. Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
  24. mInsertionControllerEnabledField.setAccessible(true);
  25. mInsertionControllerEnabledField.set(editorObject, false);
  26. } catch (Exception ignored) {
  27. }
  28. }

使用这种方式即可解决第一种方式的版本兼容问题,并且长按EditText控件中的文本光标也不会有任何变化,完美解决剪切板敏感信息泄露问题。希望以上方案对你有所帮助。

完整代码(别忘了点个赞哦):

  1. @SuppressLint("NewApi")
  2. public class NoMenuEditText extends AppCompatEditText {
  3. private final Context context;
  4. /**
  5. * 该方法为隐藏方法
  6. */
  7. boolean canPaste() {
  8. return false;
  9. }
  10. @Override
  11. public boolean isSuggestionsEnabled() {
  12. return false;
  13. }
  14. public NoMenuEditText(Context context) {
  15. super(context);
  16. this.context = context;
  17. init();
  18. }
  19. public NoMenuEditText(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. this.context = context;
  22. init();
  23. }
  24. public NoMenuEditText(Context context, AttributeSet attrs, int defStyle) {
  25. super(context, attrs, defStyle);
  26. this.context = context;
  27. init();
  28. }
  29. @SuppressLint("ClickableViewAccessibility")
  30. private void init() {
  31. this.setLongClickable(false);
  32. //this.setTextIsSelectable(false);
  33. this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
  34. //使用该方式基本可以实现禁用粘贴复制功能,6.0以上可用
  35. // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  36. // this.setCustomInsertionActionModeCallback(new ActionModeCallbackInterceptor());
  37. // }
  38. this.setOnTouchListener(new OnTouchListener() {
  39. @Override
  40. public boolean onTouch(View v, MotionEvent event) {
  41. NoMenuEditText.this.clearFocus();
  42. return false;
  43. }
  44. });
  45. }
  46. @SuppressLint("ClickableViewAccessibility")
  47. @Override
  48. public boolean onTouchEvent(MotionEvent event) {
  49. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  50. // setInsertionDisabled when user touches the view
  51. this.setInsertionDisabled();
  52. }
  53. return super.onTouchEvent(event);
  54. }
  55. private void setInsertionDisabled() {
  56. try {
  57. Field editorField = TextView.class.getDeclaredField("mEditor");
  58. editorField.setAccessible(true);
  59. Object editorObject = editorField.get(this);
  60. @SuppressLint("PrivateApi") Class editorClass = Class.forName("android.widget.Editor");
  61. Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
  62. mInsertionControllerEnabledField.setAccessible(true);
  63. mInsertionControllerEnabledField.set(editorObject, false);
  64. } catch (Exception ignored) {
  65. }
  66. }
  67. /**
  68. * Prevents the action bar (top horizontal bar with cut,copy,paste,etc.)
  69. * from appearing by intercepting the callback that would cause it to be
  70. * created,and returning false.
  71. */
  72. private class ActionModeCallbackInterceptor implements ActionMode.Callback {
  73. public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  74. //删除复制选项
  75. MenuItem itemCopy = menu.findItem(android.R.id.copy);
  76. if (itemCopy != null) {
  77. menu.removeItem(android.R.id.copy);
  78. }
  79. //删除剪切选项
  80. MenuItem itemCut = menu.findItem(android.R.id.cut);
  81. if (itemCut != null) {
  82. menu.removeItem(android.R.id.cut);
  83. }
  84. return true;
  85. }
  86. public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  87. return false;
  88. }
  89. public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  90. return false;
  91. }
  92. public void onDestroyActionMode(ActionMode mode) {
  93. }
  94. }
  95. }

参考:https://stackoverflow.com/questions/41673185/disable-edittext-context-menu

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读