-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Identify invalid IBDO data/array modifications.
- Loading branch information
James Cover jdcove2
committed
Aug 22, 2023
1 parent
4bae59a
commit 95fc212
Showing
6 changed files
with
301 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package emissary.core; | ||
|
||
import emissary.core.channels.SeekableByteChannelFactory; | ||
import emissary.util.ByteUtil; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Utility for validating that Places safely interact with IBDO payloads in byte array form. Specifically, this class | ||
* helps with validating that changes to a IBDO's payload are followed by a call to the | ||
* {@link IBaseDataObject#setData(byte[]) setData(byte[])}, {@link IBaseDataObject#setData(byte[], int, int) | ||
* setData(byte[], int, int)}, or {@link IBaseDataObject#setChannelFactory(SeekableByteChannelFactory) | ||
* setChannelFactory(SeekableByteChannelFactory)} method. | ||
*/ | ||
public class SafeUsageChecker { | ||
static final Logger logger = LoggerFactory.getLogger(SafeUsageChecker.class); | ||
public static final String UNSAFE_MODIFICATION_DETECTED = "Detected unsafe changes to IBDO byte array contents"; | ||
|
||
/** | ||
* Cache that records each {@literal byte[]} reference made available to IBDO clients, along with a sha256 hash of the | ||
* array contents. Used for determining whether the clients modify the array contents without explicitly pushing those | ||
* changes back to the IBDO | ||
*/ | ||
private final Map<byte[], String> cache = new HashMap<>(); | ||
|
||
/** | ||
* Resets the snapshot cache | ||
*/ | ||
public void reset() { | ||
cache.clear(); | ||
} | ||
|
||
/** | ||
* Stores a new integrity snapshot | ||
* | ||
* @param bytes byte[] for which a snapshot should be captured | ||
*/ | ||
public void recordSnapshot(final byte[] bytes) { | ||
cache.put(bytes, ByteUtil.sha256Bytes(bytes)); | ||
} | ||
|
||
|
||
/** | ||
* Resets the cache and stores a new integrity snapshot | ||
* | ||
* @param bytes byte[] for which a snapshot should be captured | ||
*/ | ||
public void resetCacheThenRecordSnapshot(final byte[] bytes) { | ||
reset(); | ||
recordSnapshot(bytes); | ||
} | ||
|
||
/** | ||
* Uses the snapshot cache to determine whether any of the byte arrays have unsaved changes | ||
* | ||
* @return boolean indication of unsafe changes | ||
*/ | ||
public boolean checkForUnsafeDataChanges() { | ||
boolean isUnsafe = cache.entrySet().stream().anyMatch(e -> !ByteUtil.sha256Bytes(e.getKey()).equals(e.getValue())); | ||
if (isUnsafe) { | ||
logger.warn(UNSAFE_MODIFICATION_DETECTED); | ||
} | ||
reset(); | ||
return isUnsafe; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/emissary/test/core/junit5/LogbackTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package emissary.test.core.junit5; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.read.ListAppender; | ||
import org.apache.commons.lang3.Validate; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class LogbackTester implements Closeable { | ||
public final String name; | ||
public final Logger logger; | ||
public final ListAppender<ILoggingEvent> appender; | ||
|
||
public LogbackTester(final String name) { | ||
Validate.notNull(name, "Required: name != null"); | ||
|
||
this.name = name; | ||
logger = (Logger) LoggerFactory.getLogger(name); | ||
appender = new ListAppender<>(); | ||
|
||
appender.setContext(logger.getLoggerContext()); | ||
appender.start(); | ||
logger.addAppender(appender); | ||
logger.setAdditive(false); | ||
} | ||
|
||
public void checkLogList(final Level[] levels, final String[] messages, final boolean[] throwables) { | ||
Validate.notNull(levels, "Required: levels != null"); | ||
Validate.notNull(messages, "Required: messages != null"); | ||
Validate.notNull(throwables, "Required: throwables != null"); | ||
Validate.isTrue(levels.length == messages.length, "Required: levels.length == messages.length"); | ||
Validate.isTrue(levels.length == throwables.length, "Required: levels.length == throwables.length"); | ||
|
||
assertEquals(levels.length, appender.list.size(), "Expected lengths do not match number of log messages"); | ||
|
||
for (int i = 0; i < appender.list.size(); i++) { | ||
final ILoggingEvent item = appender.list.get(i); | ||
|
||
assertEquals(levels[i], item.getLevel(), "Levels not equal for element " + i); | ||
assertEquals(messages[i], item.getFormattedMessage(), "Messages not equal for element " + i); | ||
assertEquals(throwables[i], item.getThrowableProxy() != null, "Throwables not equal for elmeent " + i); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
logger.detachAndStopAllAppenders(); | ||
} | ||
} |