Skip to content
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

feat(java/android): add screenshot prevention rule (CWE-200) #228

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/canary_integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
"ruby/third_parties",
"java/lang",
"java/spring",
"java/android",
"php/lang",
"php/symfony",
"php/third_parties",
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
"ruby/third_parties",
"java/lang",
"java/spring",
"java/android",
"php/lang",
"php/symfony",
"php/third_parties",
Expand Down
49 changes: 49 additions & 0 deletions rules/java/android/prevent_screenshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
imports:
- java_shared_lang_instance
patterns:
- pattern: |
$<GET_WINDOW>.$<METHOD>($<FLAG_SECURE>$<...>);
filters:
- variable: GET_WINDOW
detection: java_android_prevent_screenshot_get_window
- variable: METHOD
values:
- setFlags
- addFlags
- variable: FLAG_SECURE
detection: java_android_prevent_screenshot_flag_secure
auxiliary:
- id: java_android_prevent_screenshot_get_window
patterns:
- getWindow()
- pattern: $<_>.getWindow()
- id: java_android_prevent_screenshot_flag_secure
patterns:
- WindowManager.LayoutParams.FLAG_SECURE;
languages:
- java
severity: warning
metadata:
description: Permissive screenshot option set
remediation_message: |
## Description

Android may take screenshots of the current application view for display purposes, for example when an application is sent to the background.
Whether or not Android is permitted to take such screenshots is determined by the FLAG_SECURE option.

By default, the FLAG_SECURE option is not set and no screenshots are taken.

For best security practices, we should not set the FLAG_SECURE to true and we should never allow Android to take screenshots of the current application activity.

## Remediations

❌ Do not set the FLAG_SECURE option, to ensure that Android does not take screenshots of potentially sensitive information

## References

- []()

cwe_id:
- 200
id: java_android_prevent_screenshot
documentation_url: https://docs.bearer.com/reference/rules/java_android_prevent_screenshot
18 changes: 18 additions & 0 deletions tests/java/android/prevent_screenshot/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const {
createNewInvoker,
getEnvironment,
} = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)

describe(ruleId, () => {
const invoke = createNewInvoker(ruleId, ruleFile, testBase)

test("tapjacking", () => {
const testCase = "main.java"

const results = invoke(testCase)

expect(results.Missing).toEqual([])
expect(results.Extra).toEqual([])
})
})
13 changes: 13 additions & 0 deletions tests/java/android/prevent_screenshot/testdata/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class FlagSecure extends Activity {
public void bad() {
// bearer:expected java_android_prevent_screenshot
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
// bearer:expected java_android_prevent_screenshot
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}

public void ok() {
activity.getWindow().addFlags("some other flag");
}
}
Loading