Skip to content

#1064 pyscg ensure 06_logging has an rfc compliant audit log example#1079

Open
BartKaras1128 wants to merge 2 commits intoossf:mainfrom
BartKaras1128:logging_audit_branch
Open

#1064 pyscg ensure 06_logging has an rfc compliant audit log example#1079
BartKaras1128 wants to merge 2 commits intoossf:mainfrom
BartKaras1128:logging_audit_branch

Conversation

@BartKaras1128
Copy link
Copy Markdown
Contributor

Added 2 new code examples for auditing a failed authentication attempt, and I added a section to the README explaining it for pyscg-0020

BartKaras1128 and others added 2 commits March 31, 2026 11:47
Signed-off-by: Bartlomiej Karas <bartlomiej.karas@ericsson.com>
Signed-off-by: Bartlomiej Karas <moezarts@gmail.com>
Copy link
Copy Markdown
Contributor

@myteron myteron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggested an alternative compliant02.py cod example

@@ -1,6 +1,6 @@
# pyscg-0020: Implement Informative Event Logging
# pyscg-0020: Insufficient Logging
Copy link
Copy Markdown
Contributor

@myteron myteron Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BartKaras1128
rule still has the old title, see new one pyscg-0020: Implement Informative Event Logging

looks like you started out from an outdated branch?

Comment on lines +5 to +23
import logging

logging.basicConfig(
format="%(asctime)s %(levelname)s event=%(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
level=logging.INFO,
)
_audit = logging.getLogger("audit")


def login(username: str, password: str) -> bool:
"""Authenticate user with audit logging"""
# TODO: use a proper credential store
if username == "admin" and password == "s3cr3t":
_audit.info("login_success user=%s", username)
return True
_audit.warning("login_failed user=%s", username)
# TODO: forward logs to a remote logging service in production
return False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here a code example that is a little closer to the rfc with timestamps and such but still missing some stuff added as TODO.

Suggested change
import logging
logging.basicConfig(
format="%(asctime)s %(levelname)s event=%(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
level=logging.INFO,
)
_audit = logging.getLogger("audit")
def login(username: str, password: str) -> bool:
"""Authenticate user with audit logging"""
# TODO: use a proper credential store
if username == "admin" and password == "s3cr3t":
_audit.info("login_success user=%s", username)
return True
_audit.warning("login_failed user=%s", username)
# TODO: forward logs to a remote logging service in production
return False
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
"""Compliant Code Example"""
import json
import logging
from datetime import datetime, timezone
logging.basicConfig(format="%(message)s", level=logging.INFO)
def audit_log(event: str, user: str, outcome: str) -> None:
"""Write a simple audit log entry in JSON format"""
# TODO: add hostname, app_name, proc_id per RFC 5424
# TODO: forward logs to a remote logging service
# TODO: sanitize user input to prevent log injection, see pyscg-0022
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(timespec="milliseconds"),
"event": event,
"user": user,
"outcome": outcome,
}
logging.info("%s", json.dumps(entry))
def login(username: str, password: str) -> bool:
"""Authenticate user with audit logging"""
# TODO: use a proper credential store, see pyscg-0041
if username == "admin" and password == "s3cr3t":
audit_log("login", username, "success")
return True
audit_log("login", username, "failure")
return False
#####################
# Trying to exploit above code example
#####################
login("admin", "wrong_password")
login("admin", "password123!")
login("admin", "s3cr3t")


## Compliant Solution (Audit Logging)

The `compliant02.py` solution configures a `logging.Formatter` with timestamp, severity, and a structured event message. Both successful and failed authentication attempts are logged with the event type and username, without exposing sensitive data such as the password. Successful logins are logged at `INFO` level and failures at `WARNING` level.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you do adapt the code then we will have to explain why we use json instead of strictly following the rfc. Here a draft for that explanation:

RFC 5424 defines the standard transport for system logs without JSON. This guide recommends using structured JSON payloads to ensure logs are machine-readable for automated security analysis instead plain-text as defined in RFC 5424 as supported by:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants