From ae0fe798524b27f112bb755fcdae96de234a27c5 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Wed, 8 Mar 2023 16:00:42 +0530 Subject: [PATCH] fix: misc fixes, use bandit - use `bandit` instead of `codeql` - updates docstring - makes `value` positional only argument --- .github/workflows/bandit.yml | 52 ++++++++++++++++++++++++ .github/workflows/codeql.yml | 77 ------------------------------------ poetry.lock | 15 ++++++- pyproject.toml | 7 +++- tests/__init__.py | 1 + validators/hashes.py | 25 +++++++----- validators/iban.py | 5 ++- validators/mac_address.py | 4 +- validators/slug.py | 4 +- validators/uuid.py | 4 +- 10 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 .github/workflows/bandit.yml delete mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/bandit.yml b/.github/workflows/bandit.yml new file mode 100644 index 00000000..1871b97a --- /dev/null +++ b/.github/workflows/bandit.yml @@ -0,0 +1,52 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +# Bandit is a security linter designed to find common security issues in Python code. +# This action will run Bandit on your codebase. +# The results of the scan will be found under the Security tab of your repository. + +# https://github.com/marketplace/actions/bandit-scan is ISC licensed, by abirismyname +# https://pypi.org/project/bandit/ is Apache v2.0 licensed, by PyCQA + +name: Bandit +on: + workflow_dispatch: + push: + branches: ["master"] + pull_request: + # The branches below must be a subset of the branches above + branches: ["master"] + schedule: + - cron: "28 12 * * 2" + +jobs: + bandit: + permissions: + contents: read # for actions/checkout to fetch code + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Bandit Scan + uses: shundor/python-bandit-scan@9cc5aa4a006482b8a7f91134412df6772dbda22c + with: # optional arguments + # exit with 0, even with results found + exit_zero: true # optional, default is DEFAULT + # Github token of the repository (automatically created by Github) + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information. + # File or directory to run bandit on + path: ./validators # optional, default is . + # Report only issues of a given severity level or higher. Can be LOW, MEDIUM or HIGH. Default is UNDEFINED (everything) + # level: # optional, default is UNDEFINED + # Report only issues of a given confidence level or higher. Can be LOW, MEDIUM or HIGH. Default is UNDEFINED (everything) + # confidence: # optional, default is UNDEFINED + # comma-separated list of paths (glob patterns supported) to exclude from scan (note that these are in addition to the excluded paths provided in the config file) (default: .svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg) + excluded_paths: tests,docs,.github # optional, default is DEFAULT + # comma-separated list of test IDs to skip + # skips: # optional, default is DEFAULT + # path to a .bandit file that supplies command line arguments + # ini_path: # optional, default is DEFAULT diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index 98651fc0..00000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,77 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - workflow_dispatch: - push: - branches: [ "master" ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ "master" ] - schedule: - - cron: '43 14 * * 5' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ 'python' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] - # Use only 'java' to analyze code written in Java, Kotlin or both - # Use only 'javascript' to analyze code written in JavaScript, TypeScript or both - # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - - # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v2 - - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - # If the Autobuild fails above, remove it and uncomment the following three lines. - # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. - - # - run: | - # echo "Run, Build Application using script" - # ./location_of_script_within_repo/buildscript.sh - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 - with: - category: "/language:${{matrix.language}}" diff --git a/poetry.lock b/poetry.lock index 583b11b7..fa8d1b20 100644 --- a/poetry.lock +++ b/poetry.lock @@ -36,6 +36,7 @@ colorama = {version = ">=0.3.9", markers = "platform_system == \"Windows\""} GitPython = ">=1.0.1" PyYAML = ">=5.3.1" stevedore = ">=1.20.0" +toml = {version = "*", optional = true, markers = "extra == \"toml\""} [package.extras] test = ["beautifulsoup4 (>=4.8.0)", "coverage (>=4.5.4)", "fixtures (>=3.0.0)", "flake8 (>=4.0.0)", "pylint (==1.9.4)", "stestr (>=2.5.0)", "testscenarios (>=0.5.0)", "testtools (>=2.3.0)", "toml"] @@ -1181,6 +1182,18 @@ files = [ [package.dependencies] pbr = ">=2.0.0,<2.1.0 || >2.1.0" +[[package]] +name = "toml" +version = "0.10.2" +description = "Python Library for Tom's Obvious, Minimal Language" +category = "dev" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +files = [ + {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, + {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, +] + [[package]] name = "tomli" version = "2.0.1" @@ -1303,4 +1316,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "3fc714f5868085f3a877de6de4249b756bbe311bb354a5df26df4d3ac190c978" +content-hash = "2503c8f28f5fefc9238c258ebef5cfb19013a2558f1b644e25b2c84bdc18a2f6" diff --git a/pyproject.toml b/pyproject.toml index 2ee84f2c..8d74adbc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ classifiers = [ python = "^3.9" [tool.poetry.group.dev.dependencies] -bandit = "^1.7.4" black = "^23.1.0" flake8 = "^6.0.0" flake8-docstrings = "^1.7.0" @@ -39,6 +38,9 @@ setuptools = "^67.2.0" [tool.poetry.group.tests.dependencies] pytest = "^7.2.2" +[tool.poetry.group.sast.dependencies] +bandit = { extras = ["toml"], version = "^1.7.4" } + [tool.poetry.group.docs.dependencies] mkdocs = "^1.4.2" mkdocs-material = "^9.1.1" @@ -56,6 +58,9 @@ build-backend = "poetry.core.masonry.api" line-length = 100 target-version = ['py39', 'py310', 'py311'] +[tool.bandit] +exclude_dirs = [".github", ".pytest_cache", ".tox", ".vscode", "tests", "docs"] + [tool.tox] legacy_tox_ini = ''' [tox] diff --git a/tests/__init__.py b/tests/__init__.py index e69de29b..d420712d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""Tests.""" diff --git a/validators/hashes.py b/validators/hashes.py index a3736df0..1767dfbc 100644 --- a/validators/hashes.py +++ b/validators/hashes.py @@ -9,7 +9,7 @@ @validator -def md5(value: str): +def md5(value: str, /): """Return whether or not given value is a valid MD5 hash. Examples: @@ -19,7 +19,8 @@ def md5(value: str): # Output: ValidationFailure(func=md5, args={'value': '900zz11'}) Args: - value: MD5 string to validate. + value: + MD5 string to validate. Returns: (Literal[True]): @@ -33,7 +34,7 @@ def md5(value: str): @validator -def sha1(value: str): +def sha1(value: str, /): """Return whether or not given value is a valid SHA1 hash. Examples: @@ -43,7 +44,8 @@ def sha1(value: str): # Output: ValidationFailure(func=sha1, args={'value': '900zz11'}) Args: - value: SHA1 string to validate. + value: + SHA1 string to validate. Returns: (Literal[True]): @@ -57,7 +59,7 @@ def sha1(value: str): @validator -def sha224(value: str): +def sha224(value: str, /): """Return whether or not given value is a valid SHA224 hash. Examples: @@ -67,7 +69,8 @@ def sha224(value: str): # Output: ValidationFailure(func=sha224, args={'value': '900zz11'}) Args: - value: SHA224 string to validate. + value: + SHA224 string to validate. Returns: (Literal[True]): @@ -81,7 +84,7 @@ def sha224(value: str): @validator -def sha256(value: str): +def sha256(value: str, /): """Return whether or not given value is a valid SHA256 hash. Examples: @@ -94,7 +97,8 @@ def sha256(value: str): # Output: ValidationFailure(func=sha256, args={'value': '900zz11'}) Args: - value: SHA256 string to validate. + value: + SHA256 string to validate. Returns: (Literal[True]): @@ -108,7 +112,7 @@ def sha256(value: str): @validator -def sha512(value: str): +def sha512(value: str, /): """Return whether or not given value is a valid SHA512 hash. Examples: @@ -122,7 +126,8 @@ def sha512(value: str): # Output: ValidationFailure(func=sha512, args={'value': '900zz11'}) Args: - value: SHA512 string to validate. + value: + SHA512 string to validate. Returns: (Literal[True]): diff --git a/validators/iban.py b/validators/iban.py index bf44562f..0241912c 100644 --- a/validators/iban.py +++ b/validators/iban.py @@ -21,7 +21,7 @@ def _mod_check(value: str): @validator -def iban(value: str): +def iban(value: str, /): """Return whether or not given value is a valid IBAN code. Examples: @@ -31,7 +31,8 @@ def iban(value: str): # Output: ValidationFailure(func=iban, ...) Args: - value: IBAN string to validate. + value: + IBAN string to validate. Returns: (Literal[True]): diff --git a/validators/mac_address.py b/validators/mac_address.py index dc4110da..65e76ccf 100644 --- a/validators/mac_address.py +++ b/validators/mac_address.py @@ -9,7 +9,7 @@ @validator -def mac_address(value: str): +def mac_address(value: str, /): """Return whether or not given value is a valid MAC address. This validator is based on [WTForms MacAddress validator][1]. @@ -24,7 +24,7 @@ def mac_address(value: str): Args: value: - A string to validate. + MAC address string to validate. Returns: (Literal[True]): diff --git a/validators/slug.py b/validators/slug.py index d6ca498a..fe3d3169 100644 --- a/validators/slug.py +++ b/validators/slug.py @@ -9,7 +9,7 @@ @validator -def slug(value: str): +def slug(value: str, /): """Validate whether or not given value is valid slug. Valid slug can contain only lowercase alphanumeric characters and hyphens. @@ -23,7 +23,7 @@ def slug(value: str): Args: value: - A string to validate. + Slug string to validate. Returns: (Literal[True]): diff --git a/validators/uuid.py b/validators/uuid.py index 6cc8fade..aa484e20 100644 --- a/validators/uuid.py +++ b/validators/uuid.py @@ -10,7 +10,7 @@ @validator -def uuid(value: Union[str, UUID]): +def uuid(value: Union[str, UUID], /): """Return whether or not given value is a valid UUID-v4 string. This validator is based on [WTForms UUID validator][1]. @@ -25,7 +25,7 @@ def uuid(value: Union[str, UUID]): Args: value: - A string or UUID object to validate. + UUID string or object to validate. Returns: (Literal[True]):