Repository navigation
feat: add rule AZ-DB-008 SQL Server minimum TLS version below 1.2 #361
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
Open
dipeshrayg
wants to merge
3
commits into
OWASP:dev
Choose a base branch
from
dipeshrayg:feat/az-db-008-sql-minimum-tls
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+308
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #!/bin/bash | ||
| # Playbook: fix_az_db_008.sh | ||
| # Rule: AZ-DB-008 — Azure SQL Server minimum TLS version below 1.2 | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| echo "Usage: $0 <subscription_id>" | ||
| exit 1 | ||
| fi | ||
|
|
||
| SUBSCRIPTION_ID="$1" | ||
|
|
||
| echo "Setting subscription..." | ||
| az account set --subscription "$SUBSCRIPTION_ID" | ||
|
|
||
| echo "Fetching Azure SQL Servers..." | ||
| SERVERS=$(az sql server list --subscription "$SUBSCRIPTION_ID" --query "[].{name:name, rg:resourceGroup}" --output tsv) | ||
|
|
||
| if [[ -z "$SERVERS" ]]; then | ||
| echo "No Azure SQL Servers found." | ||
| exit 0 | ||
| fi | ||
|
|
||
| READ_FAILURES=0 | ||
|
|
||
| while IFS=$'\t' read -r SERVER_NAME RESOURCE_GROUP; do | ||
| echo "Checking $SERVER_NAME in $RESOURCE_GROUP..." | ||
|
|
||
| # A failed read (permissions, transient API error) says nothing about the | ||
| # server's TLS setting, so it must never fall through to the update below. | ||
| # Only a successful read returning an unset/"None"/below-1.2 value is | ||
| # treated as non-compliant. | ||
| if ! TLS_VERSION=$(az sql server show --name "$SERVER_NAME" --resource-group "$RESOURCE_GROUP" --query "minimalTlsVersion" --output tsv 2>/dev/null); then | ||
| echo "ERROR: could not read minimalTlsVersion for $SERVER_NAME, skipping (no change made)." >&2 | ||
| READ_FAILURES=$((READ_FAILURES + 1)) | ||
| continue | ||
| fi | ||
|
|
||
| if [[ "$TLS_VERSION" != "1.2" && "$TLS_VERSION" != "1.3" ]]; then | ||
| echo "Setting minimum TLS version to 1.2 on $SERVER_NAME..." | ||
| az sql server update --name "$SERVER_NAME" --resource-group "$RESOURCE_GROUP" --minimal-tls-version 1.2 --output none | ||
| echo "Done." | ||
| else | ||
| echo "$SERVER_NAME already enforces TLS $TLS_VERSION, skipping." | ||
| fi | ||
| done <<< "$SERVERS" | ||
|
|
||
| if [[ "$READ_FAILURES" -gt 0 ]]; then | ||
| echo "$READ_FAILURES server(s) could not be read and were left unchanged. Re-run after fixing access." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Done. Verify with:" | ||
| echo " az sql server show --name <name> --resource-group <rg> --query minimalTlsVersion" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """AZ-DB-008: Azure SQL Server does not enforce a minimum TLS version of 1.2.""" | ||
|
|
||
| from typing import Any, Dict, List | ||
|
|
||
| from scanner.evaluation import EvaluationStatus, RuleEvaluation, subscription_scope_id | ||
|
|
||
| RULE_ID = "AZ-DB-008" | ||
| RULE_NAME = "Azure SQL Server Minimum TLS Version Below 1.2" | ||
| SEVERITY = "HIGH" | ||
| CATEGORY = "Database" | ||
| FRAMEWORKS = {"CIS": "N/A-DB-008", "NIST": "PR.DS-2", "ISO27001": "A.10.1.1", "SOC2": "CC6.7"} | ||
| DESCRIPTION = ( | ||
| "The Azure SQL Server does not enforce a minimum TLS version of 1.2 or higher. " | ||
| "Connections are still able to negotiate the deprecated TLS 1.0 or 1.1 protocols, " | ||
| "or no minimum is set at all, leaving data in transit exposed to known protocol " | ||
| "downgrade and interception weaknesses in those older versions." | ||
| ) | ||
| REMEDIATION = ( | ||
| "Set the server's minimum TLS version to 1.2. " | ||
| "Run: az sql server update --name <server-name> --resource-group <resource-group> " | ||
| "--minimal-tls-version 1.2" | ||
| ) | ||
| PLAYBOOK = "playbooks/cli/fix_az_db_008.sh" | ||
|
|
||
| # The Server model's minimal_tls_version is a free string, not an enum, and the | ||
| # real Azure API allows "None" (no minimum enforced) alongside "1.0"/"1.1"/"1.2"/ | ||
| # "1.3" -- a missing attribute (None) must be treated the same as an explicit | ||
| # below-1.2 value, not skipped, since both mean TLS 1.2 is not being enforced. | ||
| _COMPLIANT_VERSIONS = {"1.2", "1.3"} | ||
|
|
||
|
|
||
| def _finding(azure_client: Any, server: Any, tls_version: Any) -> Dict[str, Any]: | ||
| parsed = azure_client.parse_resource_id(server.id) | ||
| return { | ||
| "rule_id": RULE_ID, | ||
| "rule_name": RULE_NAME, | ||
| "severity": SEVERITY, | ||
| "category": CATEGORY, | ||
| "resource_id": server.id, | ||
| "resource_name": server.name, | ||
| "resource_type": "Microsoft.Sql/servers", | ||
| "description": DESCRIPTION, | ||
| "remediation": REMEDIATION, | ||
| "playbook": PLAYBOOK, | ||
| "frameworks": FRAMEWORKS, | ||
| "metadata": { | ||
| "resource_group": parsed.get("resource_group", ""), | ||
| "location": getattr(server, "location", ""), | ||
| "minimal_tls_version": tls_version or "not set", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def scan(azure_client: Any, subscription_id: str) -> List[Dict[str, Any]]: | ||
| """Detect Azure SQL Servers whose minimum TLS version is below 1.2 or unset.""" | ||
| findings: List[Dict[str, Any]] = [] | ||
|
|
||
| for server in azure_client.get_sql_servers(): | ||
| tls_version = getattr(server, "minimal_tls_version", None) | ||
| if tls_version not in _COMPLIANT_VERSIONS: | ||
| findings.append(_finding(azure_client, server, tls_version)) | ||
|
|
||
| return findings | ||
|
|
||
|
|
||
| def evaluate(azure_client: Any, subscription_id: str) -> List[RuleEvaluation]: | ||
| """Report coverage per server (PASS included), so an inventory that could | ||
| not be read is recorded as UNKNOWN instead of reading as a clean scan.""" | ||
| servers = azure_client.get_sql_servers() | ||
| if not servers: | ||
| # AzureClient.get_sql_servers() returns [] both for a subscription with | ||
| # no SQL servers and when the list call itself failed (authorization, | ||
| # network, throttling), and scan() cannot tell them apart. UNKNOWN is | ||
| # the conservative record: it never counts as a pass in compliance | ||
| # scoring, whereas an empty findings list would look compliant. | ||
| return [ | ||
| RuleEvaluation( | ||
| rule_id=RULE_ID, | ||
| resource_id=subscription_scope_id(subscription_id), | ||
| resource_type="Microsoft.Sql/servers", | ||
| status=EvaluationStatus.UNKNOWN, | ||
| reason_code="INVENTORY_EMPTY_OR_UNAVAILABLE", | ||
| reason=( | ||
| "No Azure SQL servers were returned; this is indistinguishable from a failed " | ||
| "list call (authorization, network or throttling), so coverage is unknown." | ||
| ), | ||
| ) | ||
| ] | ||
|
|
||
| evaluations: List[RuleEvaluation] = [] | ||
| for server in servers: | ||
| tls_version = getattr(server, "minimal_tls_version", None) | ||
| if tls_version in _COMPLIANT_VERSIONS: | ||
| evaluations.append( | ||
| RuleEvaluation( | ||
| rule_id=RULE_ID, | ||
| resource_id=server.id, | ||
| resource_type="Microsoft.Sql/servers", | ||
| status=EvaluationStatus.PASS, | ||
| ) | ||
| ) | ||
| else: | ||
| evaluations.append( | ||
| RuleEvaluation( | ||
| rule_id=RULE_ID, | ||
| resource_id=server.id, | ||
| resource_type="Microsoft.Sql/servers", | ||
| status=EvaluationStatus.FAIL, | ||
| finding=_finding(azure_client, server, tls_version), | ||
| ) | ||
| ) | ||
|
|
||
| return evaluations | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AzureClient.get_sql_servers()catches SDK/API exceptions and returns[], so this loop cannot distinguish a successful empty inventory from an authorization, network, or throttling failure. In the latter case AZ-DB-008 reports no findings and can make an incomplete scan look clean. Please preserve an indeterminate/error state for inventory failures and avoid treating it as a compliant empty result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR notes that testing against a live Azure subscription was not performed because no subscription was available in the contributor’s environment. I had previously asked [@dipeshrayg](https://github.com/dipeshrayg) to test the change in Azure or let us know if they were unable to do so. Could you please confirm whether you can perform this manual test? If not, please let us know so we can determine how to proceed.