From 81788fc337b742fb25a36b0a6bb3e107c0ae2222 Mon Sep 17 00:00:00 2001 From: 434b Date: Thu, 22 Jun 2023 14:34:35 +0200 Subject: [PATCH] [promise-polyfill] initial integration [promise-polyfill] fix build.sh [promise-polyfill] fix build.sh [promise-polyfill] add missing license header --- projects/promise-polyfill/Dockerfile | 26 ++++++ projects/promise-polyfill/babel.config.json | 4 + projects/promise-polyfill/build.sh | 61 ++++++++++++++ projects/promise-polyfill/fuzz.js | 89 +++++++++++++++++++++ projects/promise-polyfill/project.yaml | 15 ++++ 5 files changed, 195 insertions(+) create mode 100644 projects/promise-polyfill/Dockerfile create mode 100644 projects/promise-polyfill/babel.config.json create mode 100644 projects/promise-polyfill/build.sh create mode 100644 projects/promise-polyfill/fuzz.js create mode 100644 projects/promise-polyfill/project.yaml diff --git a/projects/promise-polyfill/Dockerfile b/projects/promise-polyfill/Dockerfile new file mode 100644 index 000000000000..e3762cd8ea39 --- /dev/null +++ b/projects/promise-polyfill/Dockerfile @@ -0,0 +1,26 @@ +# 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-javascript + +COPY build.sh $SRC/ + +RUN git clone --depth 1 https://github.com/taylorhakes/promise-polyfill.git + +COPY fuzz.js $SRC/promise-polyfill +COPY babel.config.json $SRC/promise-polyfill + +WORKDIR $SRC/promise-polyfill diff --git a/projects/promise-polyfill/babel.config.json b/projects/promise-polyfill/babel.config.json new file mode 100644 index 000000000000..8f0edb07b87b --- /dev/null +++ b/projects/promise-polyfill/babel.config.json @@ -0,0 +1,4 @@ +{ + "plugins": ["@babel/plugin-transform-modules-commonjs"], + "ignore": ["**/@jazzer.js", "**/@babel", "**/istanbul-reports"] +} diff --git a/projects/promise-polyfill/build.sh b/projects/promise-polyfill/build.sh new file mode 100644 index 000000000000..1b64c63e5d2d --- /dev/null +++ b/projects/promise-polyfill/build.sh @@ -0,0 +1,61 @@ +#!/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. +# +################################################################################ + +function change_type_to_commonjs() { + # Find all package.json files inside the node_modules directory + find "$1" -name "package.json" -type f | while read -r package_file; do + # Check if the file contains the "type" field + if grep -q '"type": "module"' "$package_file"; then + # Replace "type": "module" with "type": "commonjs" + sed -i 's/"type": "module"/"type": "commonjs"/' "$package_file" + echo "Updated $package_file" + fi + done +} + +function transform_dir_into_commonjs() { + babel "$1" --keep-file-extension -D -d "$1"_commonjs + rm -r "$1" + mv "$1"_commonjs "$1" +} + +function remove_dev_dependencies() { + package_json=$(cat package.json) + + # Remove the "devDependencies" item from package.json + new_package_json=$(echo "$package_json" | jq 'del(.devDependencies)') + + # Overwrite the package.json file with the updated content + echo "$new_package_json" >package.json + +} + +# Install dependencies. +remove_dev_dependencies + +npm install -g @babel/cli +npm install --save-dev @babel/core @babel/plugin-transform-modules-commonjs + +transform_dir_into_commonjs "$SRC/promise-polyfill/src" +transform_dir_into_commonjs "$SRC/promise-polyfill/node_modules" + +npm install --save-dev @jazzer.js/core + +change_type_to_commonjs "$SRC/promise-polyfill" + +# Build Fuzzers. +compile_javascript_fuzzer promise-polyfill fuzz -i promise-polyfill diff --git a/projects/promise-polyfill/fuzz.js b/projects/promise-polyfill/fuzz.js new file mode 100644 index 000000000000..48b6eb7e4f2e --- /dev/null +++ b/projects/promise-polyfill/fuzz.js @@ -0,0 +1,89 @@ +// 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. +// +//////////////////////////////////////////////////////////////////////////////// + +const Promise = require('./src/index.js').default; + +module.exports.fuzz = async function(data) { + try { + const promise = new Promise((resolve, reject) => { + if (data.toString() === 'reject') { + reject(new Error('rejected')); + } else { + resolve(data.toString()); + } + }); + + promise.then((result) => { + Promise.resolve(''); + result.toUpperCase(); + }, (_error) => { + }) + .catch((_error) => { + }) + .finally(() => { + }); + + Promise.resolve(data.toString()) + .then((result) => { + result.toUpperCase(); + }, (_error) => { + }) + .catch((_error) => { + }) + .finally(() => { + }); + + Promise.reject(new Error('rejected')) + .catch((_error) => { + }) + .finally(() => { + }); + + const promises = [ + Promise.resolve(data.toString()), + Promise.reject(new Error('rejected')), + Promise.resolve(data.toString()) + ]; + Promise.all(promises) + .then((_results) => { + Promise.resolve(''); + }, (_error) => { + }) + .catch((_error) => { + }) + .finally(() => { + }); + + Promise.race(promises) + .then((_result) => { + Promise.resolve(''); + }, (_error) => { + }) + .catch((_error) => { + }) + .finally(() => { + }); + + } catch (error) { + if (!ignoredError(error)) throw error; + } +}; + +function ignoredError(error) { + return !!ignored.find((message) => error.message.indexOf(message) !== -1); +} + +const ignored = []; diff --git a/projects/promise-polyfill/project.yaml b/projects/promise-polyfill/project.yaml new file mode 100644 index 000000000000..9e79f588672c --- /dev/null +++ b/projects/promise-polyfill/project.yaml @@ -0,0 +1,15 @@ +homepage: https://github.com/taylorhakes/promise-polyfill +language: javascript +main_repo: https://github.com/taylorhakes/promise-polyfill +fuzzing_engines: +- libfuzzer +sanitizers: +- none +vendor_ccs: + - "wagner@code-intelligence.com" + - "yakdan@code-intelligence.com" + - "glendowne@code-intelligence.com" + - "patrice.salathe@code-intelligence.com" + - "hlin@code-intelligence.com" + - "christopher.krah@code-intelligence.com" + - "bug-disclosure@code-intelligence.com"