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): third parties ElasticSearch (CWE-201) #261

Merged
merged 2 commits into from
Feb 26, 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
64 changes: 64 additions & 0 deletions rules/java/third_parties/elasticsearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
imports:
- java_shared_lang_datatype
- java_shared_lang_instance
patterns:
- pattern: |
$<ES_CLIENT>.index($<_> -> $<_>.index().id().document($<OBJECT_WITH_DATATYPES>));
filters:
- variable: ES_CLIENT
values:
- esClient
- variable: OBJECT_WITH_DATATYPES
detection: java_third_parties_elasticsearch_object_with_datatypes
- pattern: |
$<ES_CLIENT>.update($<_> -> $<_>.index().id().upsert($<OBJECT_WITH_DATATYPES>)$<...>);
filters:
- variable: ES_CLIENT
values:
- esClient
- variable: OBJECT_WITH_DATATYPES
detection: java_third_parties_elasticsearch_object_with_datatypes
- pattern: |
$<BUILDER>.document($<OBJECT_WITH_DATATYPES>);
filters:
- variable: BUILDER
detection: java_third_parties_elasticsearch_index_request_builder
- variable: OBJECT_WITH_DATATYPES
detection: java_third_parties_elasticsearch_object_with_datatypes
auxiliary:
- id: java_third_parties_elasticsearch_object_with_datatypes
patterns:
- pattern: $<_> $<OBJ> = new $<_>($<...>$<DATA_TYPE>$<...>);
focus: OBJ
filters:
- variable: DATA_TYPE
detection: java_shared_lang_datatype
- id: java_third_parties_elasticsearch_index_request_builder
patterns:
- pattern: $<INDEX_REQUEST>.Builder<$<_>> $<BUILDER> = new $<INDEX_REQUEST>.Builder<>();
focus: BUILDER
filters:
- variable: INDEX_REQUEST
regex: \A(org\.elasticsearch\.action\.index\.)?IndexRequest\z
languages:
- java
skip_data_types:
- "Unique Identifier"
metadata:
description: Leakage of sensitive data to ElasticSearch
remediation_message: |
## Description
Leaking sensitive data to third-party data tools is a common cause of data
leaks and can lead to data breaches. This rule looks for instances of
sensitive data sent to Elasticsearch.

## Remediations
When sending data to third-party services, ensure all sensitive data is removed.

## Resources
- [Elasticsearch docs](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html)
cwe_id:
- 201
associated_recipe: Elasticsearch
id: java_third_parties_elasticsearch
documentation_url: https://docs.bearer.com/reference/rules/java_third_parties_elasticsearch
17 changes: 17 additions & 0 deletions tests/java/third_parties/elasticsearch/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { createNewInvoker, getEnvironment } = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)

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

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

const results = invoke(testCase)

expect(results).toEqual({
Missing: [],
Extra: [],
})
})
})
55 changes: 55 additions & 0 deletions tests/java/third_parties/elasticsearch/testdata/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Use bearer:expected java_third_parties_elasticsearch to flag expected findings

public class Foo {
public void bad(User user) {
// ...
ElasticsearchClient esClient = new ElasticsearchClient(transport);
Customer customer = new Customer("cust-1", user.email, user.name);

esClient.indices().create(c -> c
.index("customers"));
// bearer:expected java_third_parties_elasticsearch
IndexResponse response = esClient.index(i -> i
.index("customers")
.id(customer.getSku())
.document(customer));
}

public void bad2(User user) {
// ...
ElasticsearchClient esClient = new ElasticsearchClient(transport);
User esUser = new User("usr-1", user.email, user.name);

// bearer:expected java_third_parties_elasticsearch
esClient.update(u -> u
.index("users")
.id("usr-1")
.upsert(esUser),
User.class);
}

public void bad3(User user) {
Customer customer = new Customer("cust-1", user.email, user.name);

IndexRequest.Builder<Customer> indexReqBuilder = new IndexRequest.Builder<>();
indexReqBuilder.index("cust-1");
indexReqBuilder.id(customer.getSku());
// bearer:expected java_third_parties_elasticsearch
indexReqBuilder.document(customer);

IndexResponse response = esClient.index(indexReqBuilder.build());
}

public void good() {
// ...
ElasticsearchClient esClient = new ElasticsearchClient(transport);
Product product = new Product("prod-1", "item", 256);

esClient.update(u -> u
.index("products")
.id("prod-1")
.upsert(product),
Product.class);
}

}
Loading