Skip to content

Commit

Permalink
jsonp-api: initial integration (google#9847)
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpsy authored and eamonnmcmanus committed Mar 15, 2023
1 parent 400b5bc commit e681fb2
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 0 deletions.
64 changes: 64 additions & 0 deletions projects/jsonp-api/CreateJsonFuzzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow;
import ee.jakarta.tck.jsonp.common.JSONP_Util;
import jakarta.json.*;
import jakarta.json.stream.JsonParsingException;
import java.io.PrintStream;
import java.io.OutputStream;

public class CreateJsonFuzzer {
private static final PrintStream noopStream = new PrintStream(new OutputStream() {
@Override
public void write(int b) {}
});

public static void fuzzerInitialize() {
System.setErr(noopStream);
System.setOut(noopStream);
}

public static void fuzzerTestOneInput(FuzzedDataProvider data) {
try {
testJsonCreators(data);
JsonValue.ValueType.valueOf(data.consumeString(100));
} catch (IllegalArgumentException | JsonParsingException e) {}
}

public static void testJsonCreators(FuzzedDataProvider data) {
JsonArray jsonArray = JSONP_Util.createJsonArrayFromString(data.consumeString(200));
JSONP_Util.toStringJsonArray(jsonArray);

JsonObject jsonObject = JSONP_Util.createJsonObjectFromString(data.consumeString(200));
JSONP_Util.toStringJsonObject(jsonObject);

String inStr = data.consumeAsciiString(200);
JsonString jsonString = JSONP_Util.createJsonString(inStr);
String outString = JSONP_Util.toStringJsonString(jsonString);
String intJsonStr = "\"" + inStr + "\"";
if (!intJsonStr.equals(outString)) {
throw new FuzzerSecurityIssueLow("JsonString: " + intJsonStr + " Expected. Got " + outString);
}

int inNumber = data.consumeInt();
JsonNumber jsonNumber = JSONP_Util.createJsonNumber(inNumber);
String outNumber = JSONP_Util.toStringJsonNumber(jsonNumber);
if (!Integer.toString(inNumber).equals(outNumber)) {
throw new FuzzerSecurityIssueLow("JsonNumber: " + inNumber + " Expected. Got " + outNumber);
}
}
}
28 changes: 28 additions & 0 deletions projects/jsonp-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

FROM gcr.io/oss-fuzz-base/base-builder-jvm

RUN git clone --depth 1 https://github.com/jakartaee/jsonp-api $SRC/jsonp-api

RUN curl -L https://downloads.apache.org/maven/maven-3/3.8.7/binaries/apache-maven-3.8.7-bin.zip -o maven.zip && \
unzip maven.zip -d $SRC/maven && \
rm -rf maven.zip
ENV MVN $SRC/maven/apache-maven-3.8.7/bin/mvn

COPY build.sh $SRC/
COPY *Fuzzer.java $SRC/
WORKDIR $SRC/jsonp-api
67 changes: 67 additions & 0 deletions projects/jsonp-api/GeneratorFuzzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow;
import ee.jakarta.tck.jsonp.common.JSONP_Util;
import jakarta.json.*;
import jakarta.json.stream.JsonGenerator;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;

public class GeneratorFuzzer {
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator generator = Json.createGenerator(baos).writeStartObject();

try {
for (int i = 0; i < data.consumeInt(0, 10); i++) {
if (data.consumeBoolean()) {
generator.writeStartObject(data.consumeString(20));
} else {
generator.writeStartArray(data.consumeString(20));
}

for (int j = 0; j < data.consumeInt(0, 20); j++) {
int writeChoice = data.consumeInt(0, 3);
switch (writeChoice) {
case 0:
generator.write(data.consumeString(50), data.consumeInt());
break;
case 1:
generator.write(data.consumeString(50), data.consumeString(50));
break;
case 2:
generator.write(data.consumeString(50), data.consumeBoolean());
break;
case 3:
generator.write(JSONP_Util.createJsonString(data.consumeString(50)));
break;
}
}
generator.writeEnd();
}

generator.writeEnd();
generator.close();
baos.close();

JSONP_Util.removeWhitespace(baos.toString("UTF-8"));

} catch (JsonException | IOException | IllegalArgumentException e) {}
}
}
65 changes: 65 additions & 0 deletions projects/jsonp-api/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash -eu
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

CURRENT_VERSION=$($MVN org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \
-Dexpression=project.version -q -DforceStdout)

cd tck
$MVN -U -C clean package org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade -DskipTests -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
cp tck-common/target/jakarta*.jar $OUT/
cp tck-tests/target/jakarta*.jar $OUT/
cd ..
cd api
$MVN -U -C clean package org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade -DskipTests -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
cp target/jakarta*.jar $OUT/
cd ..

# parsson dependencies need to be downloaded manually
$MVN dependency:get -Dartifact=org.eclipse.parsson:parsson:1.1.1
cp ~/.m2/repository/org/eclipse/parsson/parsson/1.1.1/*.jar $OUT/

ALL_JARS=$(find $OUT/ -name *.jar ! -name jazzer*.jar -printf "%f ")

# The classpath at build-time includes the project jars in $OUT as well as the
# Jazzer API.
BUILD_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "$OUT/%s:"):$JAZZER_API_PATH

# All .jar and .class files lie in the same directory as the fuzzer at runtime.
RUNTIME_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "\$this_dir/%s:"):\$this_dir

for fuzzer in $(find $SRC -name '*Fuzzer.java'); do
fuzzer_basename=$(basename -s .java $fuzzer)
javac -cp $BUILD_CLASSPATH $fuzzer
mv $SRC/*.class $OUT/

# Create an execution wrapper that executes Jazzer with the correct arguments.
echo "#!/bin/bash
# LLVMFuzzerTestOneInput for fuzzer detection.
this_dir=\$(dirname \"\$0\")
if [[ \"\$@\" =~ (^| )-runs=[0-9]+($| ) ]]; then
mem_settings='-Xmx1900m:-Xss900k'
else
mem_settings='-Xmx2048m:-Xss1024k'
fi
LD_LIBRARY_PATH=\"$JVM_LD_LIBRARY_PATH\":\$this_dir \
\$this_dir/jazzer_driver --agent_path=\$this_dir/jazzer_agent_deploy.jar \
--cp=$RUNTIME_CLASSPATH \
--target_class=$fuzzer_basename \
--jvm_args=\"\$mem_settings\" \
\$@" > $OUT/$fuzzer_basename
chmod u+x $OUT/$fuzzer_basename
done
9 changes: 9 additions & 0 deletions projects/jsonp-api/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
homepage: "https://github.com/jakartaee/jsonp-api"
language: jvm
fuzzing_engines:
- libfuzzer
main_repo: "https://github.com/jakartaee/jsonp-api"
sanitizers:
- address
vendor_ccs:
- "[email protected]"

0 comments on commit e681fb2

Please sign in to comment.