Defensive adversarial tests
Test trust boundaries with inert data
Defensive red-teaming is a scoped, authorized effort to find weaknesses and improve controls. Test what happens when retrieved text conflicts with trusted application policy, when an output contains an invented marker, and when a principal lacks access. Use only fictional records and inert labels, not instructions for evading real safeguards. A model is not an authorization authority. Server-owned identity, document permissions and output contracts are independent of untrusted document text. The sample uses a trusted local registry record, an authenticated principal represented by a string, and a simulated proposed answer. It denies mismatched ownership before inspecting the answer; within scope, it releases only the approved canonical value for a public_fact record. Other proposals go to review with no content released. This is a deliberately narrow fixture rule, not a general defense against arbitrary generated text.
def release_preview(document, principal, proposed_text): # The document and principal metadata come from a trusted fixture registry. # Proposed text is untrusted. No model, network or actual secrets involved. if document["owner"] != principal: return ("deny", None) if document["kind"] != "public_fact" or proposed_text != document["value"]: return ("review", None) return ("answer", proposed_text)
public = {"owner":"team-a", "kind":"public_fact", "value":"Blue", "body":"The display is blue. [INERT_CONFLICT_LABEL]"}print(release_preview(public, "team-a", "Blue"))print(release_preview(public, "team-a", "FAKE_MARKER_49"))print(release_preview(public, "team-b", "Blue"))Design paired positive and negative cases
A defensive suite needs legitimate tasks as well as expected denials. Use an authorized team-a record containing a harmless conflict label and expect the ordinary approved fact. Use a fictional team-b caller and expect no disclosure; a mock proposal containing FAKE_MARKER_49 must be withheld. Change the untrusted body without changing trusted metadata to test that this specific policy depends on the registry, not body text. Change the owner and require denial. Include a legitimate answer that must not be refused, plus a request with missing evidence that should abstain. Denial, abstention and review are distinct routes: denial is lack of authority, abstention is lack of supported evidence, review is unresolved validity or approval. In a real system authenticate separately, check permission before fetching, and scope tools independently; the example tests output routing only.
Measure both unsafe acceptance and over-refusal
For this binary rubric the positive class means refusal is expected, with review or denial counted as refusal only if no answer was released. An unnecessary refusal is a false positive (FP); accepting a case requiring refusal is a false negative (FN). True positives and true negatives are appropriate refusal and acceptance respectively. False-positive rate = FP/(FP+TN), and false-negative rate = FN/(FN+TP); if a denominator is zero, report undefined, not zero. The five synthetic pairs give 2 TP, 1 FP, 1 TN and 1 FN, yielding rates 1/2 and 1/3. In production distinguish privacy or permission failures from evidence abstention and review, and inspect severity separately. A high aggregate score can conceal a serious boundary error. The function counts already-assigned labels and does not judge generated text.
def refusal_counts(rows): # Each pair is (expected refusal, observed refusal), already labeled. counts = {"tp":0, "fp":0, "tn":0, "fn":0} for expected, observed in rows: if type(expected) is not bool or type(observed) is not bool: raise ValueError("Boolean labels required") key = {(True,True):"tp", (False,True):"fp", (False,False):"tn", (True,False):"fn"}[expected,observed] counts[key] += 1 return counts
c = refusal_counts([(True,True),(False,True),(False,False),(True,False),(True,True)])print(c)print(c["fp"]/(c["fp"]+c["tn"]))print(c["fn"]/(c["fn"]+c["tp"]))Preserve evidence without exposing protected data
Freeze case IDs, policy version, fixture digest, model/settings when applicable, expected and observed route, and severity. Use allowlisted diagnostic fields: case ID, route, error code, reviewer disposition and redacted timestamps. Do not include raw retrieved records, tokens, prompts or credentials in ordinary logs. Use a restricted store with retention and access review if detailed artifacts are genuinely needed and authorized. A fake marker check is useful only for that fixture, not evidence of absence of other disclosures. A live red-team requires explicit scope, isolated environment, data permission, rollback and a named incident owner. Fix controls, rerun the regression suite, then request independent acceptance; never claim full robustness from toy fixtures.
Practice a defensive test report
Run the project fixture without changing its expected labels. Compare authorized and unauthorized outcomes, test the body transformation, count refusal errors and create an allowlisted redacted failure record. Explain why simulated routing is not an adversarial evaluation of an actual model. Propose acceptance criteria and escalation ownership, and state which live tests require authorization. Reference: ref-ai-defensive-adversarial-tests. Next: AI threat modeling and layered security controls.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Defensive adversarial tests quiz
Assess regression testing, interface contracts and defensive refusal evaluation.
What is the expected result for an unauthorized principal?
Which case is an unnecessary refusal?
If two expected refusals are observed and one is missed, what is the false-negative rate?
What belongs in an ordinary failure log?
What does passing the toy marker test establish?