-
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
Sep 18, 2023
1 parent
0f11c9e
commit 0a26a76
Showing
7 changed files
with
318 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ENABLED = TRUE |
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,100 @@ | ||
package emissary.core; | ||
|
||
import emissary.config.ConfigUtil; | ||
import emissary.config.Configurator; | ||
import emissary.core.channels.SeekableByteChannelFactory; | ||
import emissary.util.ByteUtil; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
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 { | ||
protected static final Logger LOGGER = LoggerFactory.getLogger(SafeUsageChecker.class); | ||
|
||
public static final boolean ENABLED; | ||
public static final String UNSAFE_MODIFICATION_DETECTED = "Detected unsafe changes to IBDO byte array contents"; | ||
|
||
static { | ||
boolean enabled = false; | ||
|
||
try { | ||
Configurator configurator = ConfigUtil.getConfigInfo(SafeUsageChecker.class); | ||
|
||
enabled = configurator.findBooleanEntry("ENABLED", enabled); | ||
} catch (IOException e) { | ||
LOGGER.info("Could not get configuration!", e); | ||
} | ||
|
||
ENABLED = enabled; | ||
} | ||
|
||
/** | ||
* 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() { | ||
if (ENABLED) { | ||
cache.clear(); | ||
} | ||
} | ||
|
||
/** | ||
* Stores a new integrity snapshot | ||
* | ||
* @param bytes byte[] for which a snapshot should be captured | ||
*/ | ||
public void recordSnapshot(final byte[] bytes) { | ||
if (ENABLED) { | ||
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) { | ||
if (ENABLED) { | ||
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() { | ||
if (ENABLED) { | ||
boolean isUnsafe = cache.entrySet().stream().anyMatch(e -> !ByteUtil.sha256Bytes(e.getKey()).equals(e.getValue())); | ||
if (isUnsafe) { | ||
LOGGER.warn(UNSAFE_MODIFICATION_DETECTED); | ||
} | ||
reset(); | ||
|
||
return isUnsafe; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.