Skip to content

Commit

Permalink
fix: make the external jar path relative to config file (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 authored Aug 10, 2023
1 parent 43457fd commit e937258
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ mvn compile io.github.algomaster99:classfile-fingerprint:generate
> ```json
> [
> {
> "path": "path/to/jar",
> "path": "path/to/jar"
> }
> ]
> ```
> 1. Path to `externalJars` **must** be absolute if the maven project is multimodular.
> 2. The `path` inside the file is relativized to the path of the `externalJars` file itself.
> However, if the path is absolute, it is not relativized.
Both methods will output a file `classfile.sha256.jsonl` in the `target` directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.github.algomaster99.terminator.commons.fingerprint.classfile.HashComputer.computeHash;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.algomaster99.terminator.commons.data.ExternalJar;
import io.github.algomaster99.terminator.commons.fingerprint.ParsingHelper;
Expand Down Expand Up @@ -144,6 +145,7 @@ private void goInsideJar(File artifactFileOnSystem, String... provenanceInformat
}
} catch (IOException e) {
getLog().error("Could not open JAR file: " + artifactFileOnSystem);
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -215,8 +217,10 @@ private void processExternalJars() {
ObjectMapper mapper = new ObjectMapper();
List<ExternalJar> externalJarList;
try {
externalJarList =
mapper.readerFor(new TypeReference<List<ExternalJar>>() {}).readValue(externalJars);
InjectableValues inject = new InjectableValues.Std().addValue("configFile", externalJars.getAbsolutePath());
externalJarList = mapper.setInjectableValues(inject)
.readerFor(new TypeReference<List<ExternalJar>>() {})
.readValue(externalJars);
} catch (IOException e) {
throw new RuntimeException("Could not open external jar file: " + e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void multi_module_with_sources(MavenExecutionResult result) throws IOException {
}

@MavenTest
@MavenOption("-DexternalJars=src/test/resources/externalJars.json")
@MavenOption("-DexternalJars=external_source/externalJars.json")
void url_classloader_local_jar(MavenExecutionResult result) {
assertThat(result).isSuccessful();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"path": "non-malicious.jar"
}
]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package io.github.algomaster99.terminator.commons.data;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@JsonDeserialize(using = ExternalJarDeserialize.class)
public record ExternalJar(File path) {}

class ExternalJarDeserialize extends JsonDeserializer<ExternalJar> {

@Override
public ExternalJar deserialize(JsonParser jp, DeserializationContext ctx) throws IOException, JacksonException {
JsonNode node = jp.getCodec().readTree(jp);

String configFile = String.valueOf(ctx.findInjectableValue("configFile", null, null));

File absolutePathOfExternalJar = new File(configFile)
.getParentFile()
.toPath()
// It trivially returns the path of external jar if it is absolute
.resolve(Path.of(node.get("path").asText()))
.toFile();

return new ExternalJar(absolutePathOfExternalJar.getAbsoluteFile());
}
}

0 comments on commit e937258

Please sign in to comment.