
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































EXAM INFORMATION
Subject: Software Security and Testing: Static Code Analysis, Flake8, Python Security, Logging, Testing & Remediation Questions: 50 | Format: Multiple choice Duration: 3 hours Passing score: 98%
ABOUT THIS EXAM
This exam assesses advanced competency in static code analysis with Flake8, secure Python coding, vulnerability remediation, structured logging, and rigorous software testing practices aligned with modern DevSecOps and OWASP standards.
WHAT THIS COVERS
Detailed Explanations
Q A flake8 run with the bandit plugin reports B301: pickle.loads() on untrusted data. Which remediation best preserves functionality while eliminating the vulnerability?
A (^) Replace pickle with json and validate the schema with pydantic before use.
B (^) Wrap pickle.loads in a try/except and log the exception.
C (^) Use pickle.Unpickler with find_class restricted to a whitelist of safe classes.
CORRECT
D (^) Enable Python's -X dev mode to catch deserialization errors.
EXPLANATION Restricting find_class to a whitelist maintains pickle compatibility while blocking arbitrary code execution. JSON (A) may not support all types; try/except (B) does not prevent exploitation; -X dev (D) only adds runtime checks, not security.
Detailed Explanations
Q Which flake8 plugin combination best detects both style and security issues in Python code?
A (^) flake8-docstrings and flake8-import-order
B (^) flake8-bugbear and flake8-comprehensions
C (^) flake8-bandit and flake8-bugbear CORRECT
D (^) flake8-annotations and flake8-quotes
EXPLANATION flake8-bandit integrates Bandit security checks; flake8-bugbear catches likely bugs. The others focus on style, documentation, or import ordering, not security.
Detailed Explanations
Q A developer uses logging.info(f"User {user} logged in with password {password}"). What is the most critical security flaw?
A (^) Using f-strings instead of %-formatting causes performance overhead.
B (^) Logging at INFO level may be disabled in production.
C (^) Sensitive credentials are written to logs, risking exposure. CORRECT
D (^) The log message lacks a timestamp and correlation ID.
EXPLANATION Logging passwords is a severe data leakage vulnerability. Performance (A) and log level (B) are secondary; missing metadata (D) is an audit concern, not the primary flaw.
Detailed Explanations
Q A flake8 report shows F401 'module imported but unused' for the 'cryptography' module. What is the best remediation?
A (^) Remove the import if truly unused. CORRECT
B (^) Add # noqa: F401 to suppress the warning.
C (^) Use the module in a dummy function to avoid the warning.
D (^) Move the import inside a function.
EXPLANATION Unused imports should be removed to reduce attack surface and improve clarity. Suppression (B) or dummy usage (C) hides the issue; moving the import (D) doesn't resolve unused status.
Detailed Explanations
Q Which of the following is a secure way to handle password hashing in Python?
A (^) hashlib.md5(password.encode()).hexdigest()
B (^) hashlib.sha256(password.encode()).hexdigest()
C (^) bcrypt.hashpw(password.encode(), bcrypt.gensalt()) CORRECT
D (^) base64.b64encode(password.encode())
EXPLANATION bcrypt is designed for password hashing with salt and work factor. MD5 (A) and SHA-256 (B) are fast and unsuitable; base64 (D) is encoding, not hashing.
Detailed Explanations
Q Which Python logging practice best prevents log injection attacks?
A (^) Using f-strings to embed user input directly.
B (^) Sanitizing user input before logging and using structured logging.
CORRECT
C (^) Logging only at ERROR level.
D (^) Storing logs in a database instead of files.
EXPLANATION Sanitization and structured logging prevent newline injection. F-strings (A) allow injection; log level (C) doesn't sanitize; database storage (D) doesn't prevent injection.
Detailed Explanations
Q A flake8-bandit report flags B303: use of insecure MD5. Which remediation is most appropriate?
A (^) Replace MD5 with SHA-1.
B (^) Replace MD5 with SHA-256 for non-security checksums.
C (^) Replace MD5 with SHA-256 or better for security-sensitive hashing.
CORRECT
D (^) Add # nosec to suppress the warning.
EXPLANATION For security, use SHA-256 or stronger. SHA-1 (A) is also broken; SHA-256 for non-security (B) is acceptable but not for security; suppression (D) ignores the risk.
Detailed Explanations
Q A logging configuration sets a root logger at WARNING and a module logger at DEBUG, but DEBUG records from that module never appear. Which explanation is most consistent with Python's logging hierarchy?
A (^) The module logger's effective level is overridden by the root handler's level filter.
B (^) The module logger lacks a handler and propagates to root, whose handler level is WARNING.
CORRECT
C (^) DEBUG records are dropped at creation because the root logger is set to WARNING.
D (^) The module logger must be set to NOTSET for DEBUG to be emitted.
EXPLANATION A child logger with no handler propagates records to ancestor handlers; the root handler's level (WARNING) then filters DEBUG records. Logger levels and handler levels are independent, so the effective level alone does not explain the drop, and NOTSET is not required.
Detailed Explanations
Q A test asserts that a function raises ValueError for negative input, but the function currently calls sys.exit(1). Which pytest construct most precisely verifies the intended contract without allowing the process to terminate the test runner?
A (^) with pytest.raises(SystemExit): fn(-1)
B (^) with pytest.raises(ValueError): fn(-1) CORRECT
C (^) assert fn(-1) is None
D (^) monkeypatch.setattr(sys, 'exit', lambda code: None) then call fn(-1)
EXPLANATION The intended contract is ValueError, so pytest.raises(ValueError) encodes that expectation and will fail until the code is remediated. pytest.raises(SystemExit) merely asserts the buggy behavior, and monkeypatching sys.exit hides the defect rather than testing the contract.
Detailed Explanations
Q A project's CI runs flake8 --max-line-length=79 and passes, yet black --check fails. Which change most likely reconciles both tools without weakening linting?
A (^) Lower flake8's max-line-length to 88.
B (^) Configure both tools to a shared line length and disable flake8's E203/W503 conflicts.
CORRECT
C (^) Disable black's line-length check in CI.
D (^) Add '# noqa' to every line black reformats.
EXPLANATION Black and flake8 disagree on slice and whitespace conventions, so aligning line length and ignoring E203/W503 resolves the conflict without suppressing real issues. Lowering the limit or disabling black's check does not reconcile the tools and weakens enforcement.
Detailed Explanations
Q A security review finds that a Flask app logs full request bodies, including passwords. Which logging change best follows current OWASP guidance while preserving useful diagnostics?
A (^) Log the body only when the log level is DEBUG.
B (^) Redact or omit sensitive fields before logging, and log structured, non-sensitive metadata.
CORRECT
C (^) Encrypt the log file at rest and continue logging the body.
D (^) Log only the HTTP status code and drop all request context.
EXPLANATION OWASP recommends minimizing and redacting sensitive data at the point of logging; structured metadata preserves diagnostics without exposing secrets. Logging at DEBUG still persists secrets, encryption at rest does not prevent exposure to log readers, and dropping all context harms operability.
Detailed Explanations
Q A test suite uses unittest.mock.patch on requests.get at the module level. A new test imports the function directly (from requests import get) and the patch no longer takes effect. Which patching target is correct?
A (^) patch('requests.get')
B (^) patch('mymodule.get') where mymodule imported it CORRECT
C (^) patch('requests')
D (^) patch.object(requests, 'get', autospec=True)
EXPLANATION Mock patches the name where it is looked up, so the module that imported get must be patched. Patching requests.get affects the original binding, not the local reference, and patching the module object is too broad.
Detailed Explanations
Q A CI pipeline runs pip-audit and reports a vulnerable transitive dependency. The team cannot upgrade it directly. Which remediation best reduces risk while preserving functionality?
A (^) Pin the vulnerable version and add it to an allowlist.
B (^) Add a constraints file overriding the transitive version to a patched release and re-run tests.
CORRECT
C (^) Vendor the vulnerable package source into the repo unchanged.
D (^) Disable pip-audit for transitive dependencies.
EXPLANATION A constraints file forces a patched transitive version without altering direct dependencies, and re-running tests verifies compatibility. Pinning or allowlisting the vulnerable version, vendoring unchanged code, or disabling the audit all preserve the vulnerability.