The standard way to mitigate this kind of thing is to have a list of valid values for the
dataSourcevariable and validate against that list before doing the lookup.Theoretically something along these lines would work:
List<String> lookup = new ArrayList<>(); lookup.add("datasource1"); lookup.add("datasource2"); lookup.add("datasource3"); .... if (lookup.contains(userInputDatasource)) { doLookup(userInputDatasource); } else { throw Exception("Datasource not found;") }JavaScript eval is not recommended:
eval()function is evil, never use it. Needing to use eval usually indicates a problem in your design.
That said a similar approach to the above could be used to verify the input only contains valid values
Answer from peater on Stack Overflowjava - Dynamic Code Evaluation: JNDI Reference Injection Logging unmarshalled object - Stack Overflow
java - Dynamic Code Evaluation: Unsafe Deserialization Fortify Issue - Stack Overflow
Dynamic Code Evaluation: Unsafe Deserialization (Spring Boot 2) - how to avoid actuator related fortify issue, or is it a false positive?
Dynamic Code Evaluation validation not picked by SCA
ObjectMessage objects depend on Java serialization to marshal and unmarshal their object payload. This process is generally considered unsafe, because a malicious payload can exploit the host system. Lots of CVEs have been created for this. For this reason, most JMS providers force users to explicitly whitelist packages that can be exchanged using ObjectMessage messages. For example, here's the related documentation for ActiveMQ Artemis.
There is no magic code fix for this issue that will eliminate the warning from Fortify aside from removing the use of ObjectMessage from your code altogether (which is what I would actually recommend).
There are a number of other issues with using JMS ObjectMessage not related to security that you should read about.
Another option (depending on your scenario), if you don't have access to the JMS provider or they don't provide this feature, is to use a (lookahead) ValidatingInputStream in your code so you can whitelist internally.
- https://docs.oracle.com/javacomponents/jmc/api/common/org/openjdk/jmc/common/io/ValidatingObjectInputStream.html