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

Modified KffDataObjectHandler.hash(..) method so it explicitly replaces IBDO params it generates. #596

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
7 changes: 7 additions & 0 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,13 @@ public void putParameters(final Map<? extends String, ? extends Object> m, final
}

final Object value = entry.getValue();

if ((policy == MergePolicy.DROP_EXISTING)) {
// store the provided value for this key, discarding any previously-stored value
setParameter(name, value);
continue;
}

if (value instanceof Iterable) {
for (final Object v : (Iterable<?>) value) {
if (policy == MergePolicy.KEEP_ALL || policy == MergePolicy.KEEP_EXISTING) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IBaseDataObject {
* Define the merge policy values for parameter handling
*/
enum MergePolicy {
DISTINCT, KEEP_EXISTING, KEEP_ALL
DISTINCT, KEEP_EXISTING, KEEP_ALL, DROP_EXISTING
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emissary/kff/KffDataObjectHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.kff;

import emissary.core.IBaseDataObject;
import emissary.core.IBaseDataObject.MergePolicy;
import emissary.core.channels.SeekableByteChannelFactory;

import org.slf4j.Logger;
Expand Down Expand Up @@ -193,9 +194,9 @@ public void hash(@Nullable final IBaseDataObject d, final boolean useSbc) throws

// Compute and add the hashes
if (useSbc && d.getChannelSize() > 0) {
d.putParameters(hashData(d.getChannelFactory(), d.shortName(), ""));
d.putParameters(hashData(d.getChannelFactory(), d.shortName(), ""), MergePolicy.DROP_EXISTING);
} else if (!useSbc && d.dataLength() > 0) {
d.putParameters(hashData(d.data(), d.shortName()));
d.putParameters(hashData(d.data(), d.shortName()), MergePolicy.DROP_EXISTING);
} else {
return;
}
Expand Down
25 changes: 23 additions & 2 deletions src/test/java/emissary/kff/KffDataObjectHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,38 @@ void testHashMethod() {
kff = new KffDataObjectHandler(true, true, true);
payload.setParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME, "test.filter");
kff.hash(payload);
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_BASE + "FILTERED_BY"));
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME));
assertTrue(KffDataObjectHandler.hashPresent(payload));
assertEquals(DATA_MD5, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_MD5));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_BASE + "CRC32"));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_CRC32));
assertEquals(DATA_SSDEEP, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SSDEEP));
assertEquals(DATA_SHA1, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA1));
assertEquals(DATA_SHA256, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA256));
assertEquals(KffDataObjectHandler.KFF_DUPE_CURRENT_FORM, payload.getFileType());
assertArrayEquals(new byte[0], payload.data());
}

@Test
void testHashMethodCalledTwice() {
// don't truncate known data or the second call will be made with an empty payload
kff = new KffDataObjectHandler(KffDataObjectHandler.KEEP_KNOWN_DATA, true, true);
payload.setParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME, "test.filter");
kff.hash(payload);

// hash again, to see the effect on the hash-related params.
// none of the parameters should have a duplicated value

kff.hash(payload);
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME));
assertTrue(KffDataObjectHandler.hashPresent(payload));
assertEquals(DATA_MD5, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_MD5));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_CRC32));
assertEquals(DATA_SSDEEP, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SSDEEP));
assertEquals(DATA_SHA1, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA1));
assertEquals(DATA_SHA256, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA256));
assertEquals(KffDataObjectHandler.KFF_DUPE_CURRENT_FORM, payload.getFileType());
}

@Test
void testParentToChildMethod() {
payload.setParameter(KffDataObjectHandler.KFF_PARAM_DUPE_HIT, payload);
Expand Down
Loading