Skip to content

Commit

Permalink
feat(java): third parties elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Feb 26, 2024
1 parent e12cb06 commit ebb6d55
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
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
18 changes: 18 additions & 0 deletions tests/java/third_parties/elasticsearch/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("elasticsearch", () => {
const testCase = "main.java"

const results = invoke(testCase)

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

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

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

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

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

public void bad3(User user) {
// bearer:expected java_third_parties_elasticsearch
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());
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
);
}

}

0 comments on commit ebb6d55

Please sign in to comment.