WGU D385
Software Security and Testing
Python Secure-Coding Practice Workbook
Original exercises + worked solutions | September 2026 alignment edition
40 focused exercises + 4 integrated capstones
Assertions | logging | exception handling | input validation | access control | insecure data handling
| unit tests | debugging vulnerable code
STUDY USE NOTICE
This is an independently created practice workbook. It is not an official WGU assessment, rubric,
task submission, answer key, or reproduction of protected course content. Use it to practice secure-
coding reasoning and testing skills.
Original study workbook - not an official WGU assessment | Page 1
, D385 Software Security and Testing | Python Secure-Coding Practice Workbook | Sept 2026
1. September 2026 Course Alignment
Official catalog basis. The WGU Institutional Catalog - September 2026 describes D385 Software Security and
Testing as preparing learners to recognize software vulnerabilities, plan interventions, develop those
interventions, and test them. It names Web security, permissions and identity security, debugging, log file
analysis, API security, and encryption/cryptography concepts as course topics.
Important scope note: The catalog supports the broad alignment above. The Python-specific workbook topics requested here -
assertions, unittest patterns, concrete logging snippets, and the exact exercises - are original practice material and should not be
presented as official WGU tasks or exam questions.
Workbook topic D385 catalog area Practice emphasis
Assertions Debugging; testing interventions Internal invariants, unsafe assert usage,
regression tests
Python logging Log file analysis; debugging Severity, redaction, exception logging,
auditable denial events
Exception handling Debugging; intervention design Narrow catches, fail-closed behavior, safe
error responses
Input validation Web security; API security Type/range/length validation, allowlists,
path/file constraints
Access-control mistakes Permissions and identity security Ownership, roles, authentication vs.
authorization, default deny
Insecure data handling Encryption/cryptography; Web/API Secrets, password hashing interface,
security token comparison, minimization
Unit tests Develop and test interventions Boundary tests, negative tests, mocks,
security regressions
Debugging vulnerable code Recognize vulnerabilities; debugging; test Reproduce, classify, remediate, verify,
fixes document
2. How to Use This Workbook
Attempt the vulnerable-code analysis before reading the worked solution.
For each exercise, name the trust boundary, protected asset, failure mode, and intended security property.
Run or adapt the snippets in an isolated local Python environment. Many examples use placeholders such as
repository, client, or policy_service to keep the focus on the security decision.
After reviewing a remediation, write at least one negative test that reproduces the original unsafe condition
and one positive test that preserves intended functionality.
Use the capstones at the end as integrated practice: identify, remediate, test, and document rather than
solving only one line of code.
3. Secure-Coding Debug Workflow
1. Reproduce: State the exact unsafe behavior and the conditions that trigger it.
2. Localize: Find the smallest code path responsible for the decision or data flow.
3. Classify: Label the issue: validation, access control, sensitive data, exception flow, logging, cryptographic
misuse, or another category.
4. Remediate: Change the control closest to the root cause; avoid cosmetic fixes.
5. Verify: Add tests for the original bad case, boundary cases, and expected legitimate behavior.
6. Document: Record the finding, risk, intervention, evidence, and remaining assumptions.
Original study workbook - not an official WGU assessment | Page 2
, D385 Software Security and Testing | Python Secure-Coding Practice Workbook | Sept 2026
Module 1 - Assertions: Internal Invariants vs. Security Validation
Alignment: Supports debugging and intervention testing. The Python emphasis is an original practice layer; the
September 2026 WGU catalog names debugging and testing interventions, but does not prescribe these exact
exercises.
Core concepts
Use assert for internal developer assumptions and invariants, not for validating untrusted user input.
Python assertions may be removed when the interpreter runs with optimization (-O), so security checks
must use explicit conditionals and exceptions.
A good assertion states an invariant that should never be false if the program is correct.
Unit-test assertions are different from production assert statements: test frameworks use assertions to
verify observable behavior.
Exercise 1.1 - Replace an Unsafe Authorization Assertion
Objective: Distinguish a developer assertion from an authorization control.
Scenario: A profile service uses assert to prevent ordinary users from editing administrator profiles.
Starting code
def update_profile(actor_role, target_role, display_name):
assert actor_role == "admin" or target_role != "admin"
return {"role": target_role, "display_name": display_name}
Your tasks
Identify why the assert statement is unsafe as an access-control boundary.
Rewrite the function so the security decision cannot disappear under optimized execution.
Choose an exception type that makes the denial explicit.
Worked solution
class AuthorizationError(PermissionError):
pass
def update_profile(actor_role, target_role, display_name):
if target_role == "admin" and actor_role != "admin":
raise AuthorizationError("Administrator profile changes require admin privileges")
return {"role": target_role, "display_name": display_name}
Why this works: Authorization is a runtime security requirement. Explicit conditional logic remains active
regardless of Python optimization settings, while assert may not.
Security checkpoint: If a check protects data, identity, money, permissions, or another security boundary, do
not rely on production assert.
Exercise 1.2 - Preserve a Legitimate Internal Invariant
Objective: Recognize a case where assert is appropriate.
Scenario: A private helper normalizes a percentage before calling a formatter. All public callers validate the
input first.
Starting code
def _format_percentage(normalized):
# normalized is produced only by trusted internal code
return f"{normalized * 100:.1f}%"
Your tasks
Add an assertion that documents the internal invariant.
Explain why the assertion is acceptable here but would be insufficient at an API boundary.
Worked solution
Original study workbook - not an official WGU assessment | Page 3