Skip to content

Commit b1772a1

Browse files
authored
Merge pull request #772 from DavidKorczynski/main
Added fuzzer to be run with OSS-Fuzz
2 parents b4a3340 + d9b5ca8 commit b1772a1

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
[run]
33
branch = True
44
source = jsonschema
5-
omit = */jsonschema/_reflect.py,*/jsonschema/__main__.py,*/jsonschema/benchmarks/*
5+
omit = */jsonschema/_reflect.py,*/jsonschema/__main__.py,*/jsonschema/benchmarks/*,*/jsonschema/tests/fuzz_validate.py

.github/workflows/fuzz.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CIFuzz
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
Fuzzing:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Build Fuzzers
13+
id: build
14+
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
15+
with:
16+
oss-fuzz-project-name: 'jsonschema'
17+
language: python
18+
# Needed until google/oss-fuzz#4996 is merged
19+
continue-on-error: true
20+
- name: Run Fuzzers
21+
if: steps.build.outcome == 'success'
22+
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
23+
with:
24+
oss-fuzz-project-name: 'jsonschema'
25+
fuzz-seconds: 30
26+
- name: Upload Crash
27+
uses: actions/upload-artifact@v1
28+
if: failure() && steps.build.outcome == 'success'
29+
with:
30+
name: artifacts
31+
path: ./out/artifacts

jsonschema/tests/fuzz_validate.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Fuzzing setup for OSS-Fuzz.
3+
4+
See https://github.com/google/oss-fuzz/tree/master/projects/jsonschema for the
5+
other half of the setup here.
6+
"""
7+
import sys
8+
9+
from hypothesis import given, strategies
10+
11+
import jsonschema
12+
13+
PRIM = strategies.one_of(
14+
strategies.booleans(),
15+
strategies.integers(),
16+
strategies.floats(allow_nan=False, allow_infinity=False),
17+
strategies.text(),
18+
)
19+
DICT = strategies.recursive(
20+
base=(
21+
strategies.booleans()
22+
| strategies.dictionaries(strategies.text(), PRIM),
23+
),
24+
extend=lambda inner: strategies.dictionaries(strategies.text(), inner),
25+
)
26+
27+
28+
@given(obj1=DICT, obj2=DICT)
29+
def test_schemas(obj1, obj2):
30+
try:
31+
jsonschema.validate(instance=obj1, schema=obj2)
32+
except jsonschema.exceptions.ValidationError:
33+
pass
34+
except jsonschema.exceptions.SchemaError:
35+
pass
36+
37+
38+
def main():
39+
atheris.Setup(sys.argv,
40+
test_schemas.hypothesis.fuzz_one_input,
41+
enable_python_coverage=True)
42+
atheris.Fuzz()
43+
44+
45+
if __name__ == "__main__":
46+
import atheris
47+
main()

0 commit comments

Comments
 (0)