PyCQA / PyCQA/bandit

Wrongly formatted json output

Open
#1,019 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
8.3k
Forks
836
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Describe the bug

I'm running bandit with pre-commit. I want to dump the report in a json format so that I can exploit it afterward.
For some unknown reason, the json generated is broken at the end, in a random way at every run.

Here is the json ouput :

  "errors": [],
  "generated_at": "2023-04-11T14:36:30Z",
  "metrics": {
    "_totals": {
      "CONFIDENCE.HIGH": 47,
      "CONFIDENCE.LOW": 7,
      "CONFIDENCE.MEDIUM": 0,
      "CONFIDENCE.UNDEFINED": 0,
      "SEVERITY.HIGH": 0,
      "SEVERITY.LOW": 47,
      "SEVERITY.MEDIUM": 7,
      "SEVERITY.UNDEFINED": 0,
      "loc": 1010,
      "nosec": 0,
      "skipped_tests": 0
    },
    "some_file.py": {
      "CONFIDENCE.HIGH": 3,
      "CONFIDENCE.LOW": 0,
      "CONFIDENCE.MEDIUM": 0,
      "CONFIDENCE.UNDEFINED": 0,
      "SEVERITY.HIGH": 0,
      "SEVERITY.LOW": 3,
      "SEVERITY.MEDIUM": 0,
      "SEVERITY.UNDEFINED": 0,
      "loc": 31,
      "nosec": 0,
      "skipped_tests": 0
    }
  },
  "results": []
}kipped_tests": 0
    }
  },
  "results": []
}s": []
}": 0
    }
  },
  "results": []
}s": []
}"nosec": 0,
      "skipped_tests": 0
    }
  },
  "results": []
}

Here is my pre-commit hook :

- repo: https://github.com/PyCQA/bandit
    rev: '1.7.4'
    hooks:
    - id: bandit
      args: [-lll, -i, -f, json, -o, bandit_report.json]
Reproduction steps
1. Run the command : pre-commit run -a
2. Check the json file generated
Expected behavior

I would expect the json file to be properly formatted and not have this random excess at the end of the file.

Bandit version

1.7.4

Python version

3.11 (Default)

Additional context

Looks like the issue only happen when using bandit with pre-commit. I did the test using bandit alone an its working fine, so maybe this issue is not at the right place...

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with pre-commit run -a using Bandit 1.7.4 and compare the generated report with a direct Bandit invocation. Investigate the JSON output path and the interaction with the shown pre-commit hook; done means the generated report remains valid JSON without random trailing content.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.