Skip to content

Commit

Permalink
attrs: basic fuzzing setup
Browse files Browse the repository at this point in the history
Fuzz creation of classes with various Unicode identifiers

attrs has a critically score of 0.70292 according to

        https://www.googleapis.com/download/storage/v1/b/ossf-criticality-score/o/2024.12.25%2F224906%2Fall.csv?generation=1736309219475649&alt=media

It has 5.4k stars on github and more than 11M of downloads per day based
on https://pypistats.org/packages/attrs
  • Loading branch information
serge-sans-paille committed Feb 5, 2025
1 parent e007cb7 commit 8622aaa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
21 changes: 21 additions & 0 deletions projects/attrs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/python3

# Copyright 2025 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-python
RUN git clone https://github.com/python-attrs/attrs.git
COPY *.sh *py $SRC/
WORKDIR $SRC/attrs
21 changes: 21 additions & 0 deletions projects/attrs/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash -eu

# Copyright 2025 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.
#
##########################################################################
pip3 install .
for fuzzer in $(find $SRC -name 'fuzz_*.py'); do
compile_python_fuzzer $fuzzer --hidden-import=html.parser
done
64 changes: 64 additions & 0 deletions projects/attrs/fuzz_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/python3

# Copyright 2025 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 sys
import atheris

import attrs
import ast
from string import ascii_letters

def consumeIdentifier(fdp):
return fdp.ConsumeUnicode(8)

@atheris.instrument_func
def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)
clsname = consumeIdentifier(fdp)
attrcount = fdp.ConsumeIntInRange(0, 12)
attrnames = [consumeIdentifier(fdp)
for _ in range(attrcount)]
attrvalues = [None] * attrcount

# Create class from attrs.make_class
try:
C0 = attrs.make_class(clsname, attrnames)
except Exception as e:
if any(not name.isidentifier() for name in [clsname] + attrnames):
return
raise
c0 = C0(**{k: v for k, v in zip(attrnames, attrvalues)})
d0 = attrs.asdict(c0)
c0_p = C0(**d0)
assert c0 == c0_p

# Create class from attrs.define
C1 = attrs.define(type(clsname, (), {f: attrs.field() for f in attrnames}))
c1 = C1(**{k: v for k, v in zip(attrnames, attrvalues)})
d1 = attrs.asdict(c1)
c1_p = C1(**d1)
assert c1 == c1_p


def main():
atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()


if __name__ == "__main__":
main()
9 changes: 9 additions & 0 deletions projects/attrs/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fuzzing_engines:
- libfuzzer
homepage: https://www.attrs.org
language: python
main_repo: https://github.com/python-attrs/attrs.git
sanitizers:
- address
vendor_ccs:
- [email protected]

0 comments on commit 8622aaa

Please sign in to comment.