left-icon

Ethical AI for Developers Succinctly®
by Ed Freitas

Previous
Chapter

of
A
A
A

CHAPTER 12

Anti-Ethical Practice: Data Leakage and Insecure Models


Overview

The anti-ethical developer often prioritizes speed and convenience over security, leading to vulnerabilities that expose user data and make models easy to compromise. This violates both the privacy and security principles.

Insecurity tactics

Logging sensitive data: Logging raw user inputs, including personally identifiable information (PII) or sensitive attributes, to unencrypted logs for easier debugging.

Insecure model deployment: Deploying models without input validation or rate limiting, making them susceptible to denial-of-service or simple adversarial attacks.

Hardcoding secrets: Storing API keys, database credentials, or encryption keys directly in the code repository or model files.

Ignoring drift: Failing to implement monitoring for data or concept drift, allowing the model's performance to degrade silently, leading to unreliable and potentially harmful decisions.

Insecure data handling (logging PII)

The following anti-ethical code demonstrates a common, dangerous practice: logging raw, sensitive user data in a way that creates a significant privacy vulnerability.

Code Listing 12-a: Insecure data handling example (insecure-data-handling.py)

import logging

import json

import os

# --- Insecure Logging of PII ---

# 1. Anti-Ethical Step: Configure logging to a file without encryption or

# rotation.

log_file = 'insecure_app.log'

# Ensure logging is configured to a file for the example to work.

logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(message)s', force=True)

def process_user_request_insecure(user_data):

    """

    Processes a user request and logs the entire raw input for 'debugging'.

    """

    # 2. Anti-Ethical Step: Log the entire raw user data dictionary.

    # This data often contains PII (e.g., 'ssn', 'address').

    logging.info(f"Raw Request Data: {json.dumps(user_data)}")

   

    # Simulate model prediction.

    prediction = "Approved" if user_data.get('credit_score', 0) > 650

    else "Denied"

   

    # 3. Anti-Ethical Step: Return prediction without sanitizing logs.

    logging.info(f"Prediction Result: {prediction}")

    return prediction

# Example of a request containing PII.

sensitive_request = {

    "name": "Jane Doe",

    "ssn": "999-99-9999", # Highly sensitive PII.

    "address": "123 Main St, Anytown, USA",

    "credit_score": 720,

    "loan_amount": 50000

}

process_user_request_insecure(sensitive_request)

print("--- Anti-Ethical Outcome Analysis ---")

print(f"Check the file '{log_file}' for the full, unencrypted PII log.")

# Ethical Reflection:

# An ethical developer would:

# 1. Sanitize the user_data dictionary to remove PII before logging.

# 2. Use a secure, encrypted logging service with strict access controls.

# 3. Implement log rotation and deletion policies.

This simple code demonstrates an unethical practice: an application logs entire user requests—including sensitive PII such as SSNs and addresses—into an unencrypted, unrotated log file under the pretext of debugging.

An unrotated log file is an active, current log file that has not yet been processed, renamed, compressed, or archived by a log rotation system.

Persisting raw personal data to disk without sanitization or security controls creates serious privacy, compliance, and breach risks that violate basic data-protection principles.

Now, let’s run the code with the command py insecure-data-handling.py from the terminal in VS Code.

Code Listing 12-b: Insecure data handling example execution

--- Anti-Ethical Outcome Analysis ---

Check the file 'insecure_app.log' for the full, unencrypted PII log.

Running the script creates a log file called insecure_app.log. Let’s open the file to see its content.

Code Listing 12-c: Log file generated

2026-01-16 20:29:28,145 - Raw Request Data: {"name": "Jane Doe", "ssn": "999-99-9999", "address": "123 Main St, Anytown, USA", "credit_score": 720, "loan_amount": 50000}

2026-01-16 20:29:28,145 - Prediction Result: Approved

As you can see, this log file shows a serious privacy violation. It records the entire raw request, including highly sensitive PII such as the user’s full name, Social Security number, and home address, in plain text with no masking, encryption, or access controls.

Recap

Even though the prediction result (Approved) is harmless on its own, storing it alongside exposed PII creates a permanent audit trail of personal data that could be leaked, misused, or exploited, violating data minimization principles and most privacy regulations.

Scroll To Top
Disclaimer

DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.