Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bandit Skipping Directory and Unable to Output Report Error #1190

Open
spyd3r98 opened this issue Oct 11, 2024 · 0 comments
Open

Bandit Skipping Directory and Unable to Output Report Error #1190

spyd3r98 opened this issue Oct 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@spyd3r98
Copy link

spyd3r98 commented Oct 11, 2024

Describe the bug

Command Used:
dist\run_bandit.exe -r C:\repo\python\kubernetes\base\config -f json -o result.json

Output:
Running Bandit with the following parameters: Report directory: C:\repo\python\kubernetes\base\config Output format: json Output file: result.json Discovering files in directory: C:\repo\python\kubernetes\base\config Skipping directory (C:\repo\python\kubernetes\base\config), use -r flag to scan contents Running Bandit tests... Outputting results with severity level 'LOW' and confidence level 'LOW' Error during output results: Unable to output report using 'screen' formatter: 'screen'

Script Used:

import sys
from bandit.core import manager as bandit_manager
from bandit.core import config as bandit_config

if __name__ == "__main__":
    # Argument parsing
    report_directory = None
    output_format = 'json'  # Default output format
    output_file = None

    # Parse arguments
    args = sys.argv[1:]
    for i in range(len(args)):
        if args[i] == '-r':
            report_directory = args[i + 1]
        elif args[i] == '-f':
            output_format = args[i + 1]
        elif args[i] == '-o':
            output_file = args[i + 1]

    # Ensure required arguments are provided
    if not report_directory:
        print("Error: Missing required '-r' argument for directory")
        sys.exit(1)

    # Print the command-line parameters for debugging
    print(f"Running Bandit with the following parameters:")
    print(f"Report directory: {report_directory}")
    print(f"Output format: {output_format}")
    print(f"Output file: {output_file if output_file else 'Not specified'}")

    # Load Bandit configuration
    config = bandit_config.BanditConfig()

    # Initialize Bandit manager
    b_mgr = bandit_manager.BanditManager(config, output_format)

    # Discover files and run tests
    print(f"Discovering files in directory: {report_directory}")
    b_mgr.discover_files([report_directory])
    if b_mgr.results_count == 0:
        print(f"No Python files found in '{report_directory}'. Please check the directory.")
        sys.exit(1)

    print("Running Bandit tests...")
    b_mgr.run_tests()

    # Generate the report
    lines = []  # Since the results will be written to the output file, we leave this empty
    sev_level = 'LOW'  # Severity level (LOW, MEDIUM, HIGH)
    conf_level = 'LOW'  # Confidence level (LOW, MEDIUM, HIGH)

    # Output results to the specified file or to console if no file is specified
    print(f"Outputting results with severity level '{sev_level}' and confidence level '{conf_level}'")
    
    try:
        b_mgr.output_results(lines, sev_level, conf_level, output_file, output_format)
    except Exception as e:
        print(f"Error during output results: {e}")
        sys.exit(1)

    # Exit with Bandit result code
    print(f"Bandit completed with {b_mgr.results_count} findings.")
    sys.exit(b_mgr.results_count)

PyInstaller Command Used:
pyinstaller --onefile --hidden-import=bandit.core.manager --hidden-import=stevedore.extension run_bandit.py

Reproduction steps

Run the command:
run_bandit.exe -r -f json -o result.json

Expected behavior

Bandit should discover Python files in the specified directory and generate a JSON report without errors.

Bandit version

1.7.10 (Default)

Python version

3.12

Additional context

No response

@spyd3r98 spyd3r98 added the bug Something isn't working label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant