Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions noncompliant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# {fact rule=catch-and-rethrow-exception@v1.0 defects=1}
def nested_noncompliant():
try:
try_something()
except KeyError as e:
try:
catch_and_try_something()
# Noncompliant: unnecessary `except` clause.
except ValueError:

Check notice

Code scanning / CodeGuru Security Scanner

Catching and re-throwing an exception without further actions is redundant and wasteful.

We detected an `except` clause that is not necessary because it catches and re-throws an exception without doing anything with it. To call additional operations on the caught exception, consider wrapping it into a custom exception that adds more information or special handling.
raise
raise e
# {/fact}

# {fact rule=aws-logged-credentials@v1.0 defects=1}
def log_credentials_noncompliant():
import boto3
import logging
session = boto3.Session()
credentials = session.get_credentials()
credentials = credentials.get_frozen_credentials()
access_key = credentials.access_key
secret_key = credentials.secret_key
# Noncompliant: credentials are written to the logger.
logging.info('Access key: ', access_key)

Check failure

Code scanning / CodeGuru Security Scanner

CWE-255 - AWS credentials logged

We detected that unencrypted AWS credentials are logged in your code. This could expose those credentials to an attacker. To make your code more secure, encrypt sensitive data, such as credentials, before they are logged. [Learn more](https://cwe.mitre.org/data/definitions/798.html) Similar issue at line number 28.
logging.info('secret access key: ', secret_key)
# {/fact}

# {fact rule=log-injection@v1.0 defects=1}
def logging_noncompliant():
filename = input("Enter a filename: ")
# Noncompliant: unsanitized input is logged.
logger.info("Processing %s", filename)
# {/fact}

# {fact rule=sql-injection@v1.0 defects=1}
def execute_query_noncompliant(request):
import sqlite3
name = request.GET.get("name")
query = "SELECT * FROM Users WHERE name = " + name + ";"
with sqlite3.connect("example.db") as connection:
cursor = connection.cursor()
# Noncompliant: user input is used without sanitization.
cursor.execute(query)

Check failure

Code scanning / CodeGuru Security Scanner

CWE-89 - SQL injection

We detected an SQL command that might use unsanitized input. This can result in an SQL injection. To increase the security of your code, sanitize inputs before using them to form a query string. [Learn more](https://cwe.mitre.org/data/definitions/89.html)
connection.commit()
connection.close()
# {/fact}

# {fact rule=hardcoded-credentials@v1.0 defects=1}
def create_session_noncompliant():
import boto3
# Noncompliant: uses hardcoded secret access key.
sample_key = "AjWnyxxxxx45xxxxZxxxX7ZQxxxxYxxx1xYxxxxx"

Check failure

Code scanning / CodeGuru Security Scanner

CWE-798 - Hardcoded credentials

Your code uses hardcoded AWS credentials which might allow unauthorized users access to your AWS account. These attacks can occur a long time after the credentials are removed from the code. We recommend that you set AWS credentials with environment variables or an AWS profile instead. You should consider deleting the affected account or rotating the secret key and then monitoring Amazon CloudWatch for unexpected activity. [https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html)
boto3.session.Session(aws_secret_access_key=sample_key)
# {/fact}