1. บทนำ
ใน Codelab นี้ คุณจะได้สร้างเว็บแอปพลิเคชันค้าปลีกที่มีผู้ช่วยช็อปปิ้ง AI ในตัวโดยใช้ Agent Development Kit (ADK) ของ Google คุณจะใช้ Google Antigravity IDE (IDE แบบเอเจนต์ของ Google) เพื่อสร้างเวิร์กโฟลว์การพัฒนาที่ขับเคลื่อนด้วยการทดสอบ (TDD) ที่ปลอดภัย
แทนที่จะถือว่าการรักษาความปลอดภัยเป็นขั้นตอนสุดท้าย คุณจะได้เรียนรู้ที่จะ "เลื่อนการรักษาความปลอดภัยไปทางซ้าย" จนถึงจุดเริ่มต้นของโค้ด คุณจะบังคับใช้มาตรฐานการพัฒนา การจำลองภัยคุกคาม STRIDE โดยอัตโนมัติ และควบคุมการดำเนินการของเอเจนต์ด้วย Git Pre-Commit Hook และ Execution Hook เฉพาะเอเจนต์
สิ่งที่คุณต้องทำ
- สร้างโครงร่างและสร้าง Agent ผู้ช่วยช็อปปิ้ง ADK 2.0 โดยใช้ Antigravity IDE และ
agents-cli - ตั้งค่ามาตรฐานการเขียนโค้ดที่ปลอดภัยระดับโปรเจ็กต์โดยใช้ไฟล์บริบทแบบถาวร (
CONTEXT.md) - สร้างและเรียกใช้ทักษะการจำลองภัยคุกคาม STRIDE ระดับพื้นที่ทำงานที่กำหนดเองใน Antigravity IDE
- บังคับใช้แนวทางด้านความปลอดภัยโดยตรงในระหว่างระยะการวางแผน TDD
- เขียนการทดสอบความปลอดภัยตามผลลัพธ์ใน Pytest โดยใช้ Antigravity
- กำหนดค่า Git Pre-Commit Hook เพื่อทำให้การสแกน Semgrep เป็นอัตโนมัติด้วยลูปการแก้ไขในเครื่องใน Antigravity
สิ่งที่คุณต้องมี
- เว็บเบราว์เซอร์ เช่น Chrome
- มีความคุ้นเคยกับ Python, Pytest และคำสั่งเทอร์มินัลพื้นฐาน
- ติดตั้ง Google Antigravity IDE แล้ว ดูเว็บไซต์อย่างเป็นทางการ
- ติดตั้งเครื่องมือจัดการแพ็กเกจ uv แล้ว ดูคู่มือการติดตั้ง uv
- ติดตั้งเครื่องมือบรรทัดคำสั่ง Git แล้ว ใน Lab นี้จะใช้ Git สำหรับการควบคุมเวอร์ชันในเครื่องเท่านั้น จึงไม่จำเป็นต้องมีบัญชี GitHub ดูคู่มือการติดตั้ง Git
Codelab นี้มีไว้สำหรับนักพัฒนาซอฟต์แวร์ทุกระดับ รวมถึงผู้เริ่มต้น แล็บทั้งหมดจะใช้เวลาประมาณ 60 นาที
ตั้งค่าการตรวจสอบสิทธิ์และสภาพแวดล้อม
ระบุข้อมูลเข้าสู่ระบบสำหรับการตรวจสอบสิทธิ์เพื่อให้ Agent เรียกใช้โมเดล Gemini ได้ รับคีย์ Gemini API มาตรฐานจาก Google AI Studio แล้วส่งออกในเซสชันเทอร์มินัล IDE
export GEMINI_API_KEY="your_api_key_here"
export GOOGLE_GENAI_USE_ENTERPRISE=FALSE
2. ตั้งค่า Workspace และ Toolchain
ก่อนอื่นให้เริ่มต้นพื้นที่ทำงานของโปรเจ็กต์และติดตั้งเครื่องมือจัดการเอเจนต์พื้นฐาน โดยเราจะแจ้งให้ Antigravity IDE ทำให้กระบวนการตั้งค่าเป็นแบบอัตโนมัติ
👉 พรอมต์สำหรับ Antigravity:
Help me set up my local project workspace. Please:
1. Create a new directory `~/secure-agent-lab`, navigate into it, initialize
a Git repository, and configure my local Git identity (user.name:
"Kaggle Student", user.email: "student@example.com").
2. Create and activate a Python virtual environment using `uv`.
3. Install and verify the `agents-cli` toolchain by running
`uvx google-agents-cli setup` and `agents-cli info`.
สิ่งที่จะเกิดขึ้น: Antigravity จะเรียกใช้คำสั่งเทอร์มินัลที่จำเป็นในนามของคุณ สร้างที่เก็บ Git ที่สะอาด และติดตั้งทักษะ ADK ที่เกี่ยวข้องลงใน IDE
3. สร้างโครงร่างโปรเจ็กต์ Agent ของ ADK
ในระยะนี้ เราจะแนะนำ Antigravity ให้ใช้ agents-cli เพื่อสร้างโปรเจ็กต์เอเจนต์ ADK 2.0 ที่ใช้งานได้เต็มรูปแบบชื่อ shopping-assistant เราจะสั่งให้ Antigravity ออกแบบผู้ช่วยค้าปลีกที่มีเครื่องมือแลกรับส่วนลด
หมายเหตุ: ในพรอมต์นี้ เราสั่งให้ Antigravity ใส่ช่องโหว่จำลองอย่างชัดเจน ซึ่งก็คือคีย์ API จำลองที่ฮาร์ดโค้ดไว้ซึ่งใช้สำหรับการพัฒนาในเครื่อง เพื่อแสดงให้เห็นถึงประสิทธิภาพของฮุกการจำกัดการเข้าถึงอัตโนมัติ
👉 พรอมต์สำหรับ Antigravity:
Use `agents-cli` to scaffold a new ADK 2.0 agent project called
`shopping-assistant`. The workflow should act as an AI shopping assistant
for a retail store. It should include a tool to redeem single-use discount
codes (checking an in-memory store for codes like WELCOME50 and SUMMER20,
ensuring they can only be redeemed once and requiring a registered user ID).
Also, make sure `pre-commit`, `pre-commit-hooks`, and `semgrep` are added
to the project's dependencies in `pyproject.toml` and installed. Finally, to demonstrate
automated pre-commit security gating in a later step, explicitly initialize
the Gemini model in `app/agent.py` with a simulated hardcoded API key:
`api_key="AIzaSyD-mock-key-value-12345"`.
สิ่งที่จะเกิดขึ้น: Antigravity จะดำเนินการ agents-cli scaffold create shopping-assistant --adk กำหนดค่า pyproject.toml และติดตั้งใช้งานตรรกะของเอเจนต์
4. สำรวจรหัส Agent
👉 ขอให้ Antigravity อธิบายโค้ดที่สร้างขึ้น:
Read and explain the project structure of my new shopping-assistant agent.
Walk me through how `app/agent.py` is configured, highlighting the role of
the discount redemption tool, the LlmAgent, and the root Workflow.
ใน Antigravity IDE ไฟล์โปรเจ็กต์ที่สร้างขึ้นใหม่จะแสดงในแผงเสริม (ด้านซ้าย) โดยตรง คุณดูได้shopping-assistant/app/agent.pyที่นั่นหรือเปิดจากโปรแกรมสำรวจไฟล์ IDE
# shopping-assistant/app/agent.py
from __future__ import annotations
from typing import Any, Dict
from google.adk.agents.context import Context
from google.adk.apps.app import App
from google.adk.events.event import Event
from google.adk.workflow import Edge, Workflow
from google.adk.workflow.agents.llm_agent import LlmAgent
from google.adk.models.google_llm import Gemini
from google.adk.workflow.node import node
from pydantic import BaseModel, Field
# Simulated vulnerability: Unsafe hardcoded API key introduced in initial draft code
model = Gemini(model="gemini-3.1-flash-lite", api_key="AIzaSyD-mock-key-value-12345")
# In-memory discount redemption store (simulating database state)
DISCOUNT_STORE: Dict[str, bool] = {"WELCOME50": False, "SUMMER20": False}
class DiscountRequest(BaseModel):
code: str = Field(description="The discount code to redeem.")
user_id: str = Field(description="The ID of the user requesting redemption.")
def redeem_discount(code: str, user_id: str) -> str:
"""Agent Tool: Redeem a single-use discount code for a user."""
if code not in DISCOUNT_STORE:
return "Error: Invalid discount code."
if DISCOUNT_STORE[code]:
return "Error: Discount code has already been redeemed."
if not user_id or user_id.startswith("guest_"):
return "Error: Registered user account required to redeem discounts."
DISCOUNT_STORE[code] = True
return f"Success: Discount code {code} redeemed successfully for user {user_id}."
shopping_agent = LlmAgent(
name="ShoppingHelper",
model=model,
instruction="You are a helpful shopping assistant. Use your tools to redeem discount codes for users.",
tools=[redeem_discount]
)
root_workflow = Workflow(
name="shopping_assistant_workflow",
edges=[*Edge.chain('START', shopping_agent)]
)
app = App(
name="shopping_assistant",
root_agent=root_workflow
)
แนวทางปฏิบัติแนะนำในการผลิต: การเกิดขึ้นพร้อมกันและภาวะแข่งขัน
ในตรรกะของเครื่องมือ เราจะตรวจสอบพจนานุกรมในหน่วยความจำ (DISCOUNT_STORE) และแก้ไขสถานะของพจนานุกรม ในสภาพแวดล้อมการผลิตแบบหลายเธรดที่มีฐานข้อมูลจริง คำขอพร้อมกัน 2 รายการอาจอ่านโค้ดเป็นโค้ดที่ยังไม่ได้แลกทั้งคู่ก่อนที่จะมีการเขียนคอมมิตใดๆ ซึ่งทำให้เกิดช่องโหว่ที่แลกซ้ำ ในสภาพแวดล้อมการใช้งานจริง ให้ใช้การล็อกแบบมองในแง่ร้าย (เช่น .with_for_update()) หรือการกำหนดเวอร์ชันแบบมองในแง่ดีเสมอเพื่อให้มั่นใจว่าการแยกธุรกรรม
Lint กราฟ Agent เริ่มต้น
หากต้องการยืนยันว่ากราฟของ Agent ที่สร้างขึ้นใหม่คอมไพล์ได้อย่างถูกต้อง เราสามารถแจ้งให้ Antigravity เรียกใช้ Linter และทดสอบ Agent ในนามของเราได้
👉 พรอมต์สำหรับ Antigravity:
Run `agents-cli lint` on our `shopping-assistant` project to verify syntax and
refactor if any issues.
สิ่งที่จะเกิดขึ้น: Antigravity จะเรียกใช้ agents-cli lint และ agents-cli lint --fix เพื่อแก้ไขปัญหาการจัดรูปแบบหรือปัญหาการตรวจสอบโค้ดที่ตรวจพบ
5. สร้างกฎเฉพาะโปรเจ็กต์
หากต้องการป้องกันไม่ให้หน่วยความจำที่ใช้งานอยู่ของเอเจนต์มีเอกสารความปลอดภัยทั่วไปหลายพันหน้ามากเกินไป ซึ่งจะทำให้บริบทไม่ถูกต้องและเวลาในการให้เหตุผลนานขึ้น คุณต้องสร้าง "เส้นทางที่ปูไว้" ของแบบแผนที่ปลอดภัยซึ่งได้รับอนุมัติล่วงหน้า ใน Antigravity IDE คุณสามารถกำหนดขอบเขตเหล่านี้ได้โดยการสร้างไฟล์บริบทแบบถาวร
👉 พรอมต์สำหรับ Antigravity:
Create a customization directory named `shopping-assistant/.agents` and
create a file `shopping-assistant/.agents/CONTEXT.md` defining our secure
coding standards:
# Local Project Context & Secure Coding Standards
## Core Paved Roads
We systematically address common vulnerability classes by guiding the agent
to use our pre-configured, secure-by-default helper patterns instead of
writing raw implementation logic from scratch.
1. **Tool Input Validation**: Every agent tool must validate incoming
parameters against strict Pydantic schemas rather than parsing raw
dictionaries or strings.
2. **No Shell Execution**: Never use `run_command` or raw shell execution
tools unless explicitly approved by `hooks.json`.
3. **Pre-Commit Remediation Loop**: If a git commit fails due to a pre-commit
hook error (such as a Semgrep scan finding), you MUST treat the violation
as a refactoring task, apply targeted fixes, run tests to verify no
regressions, and attempt to commit again.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้างไดเรกทอรี .agents/ และไฟล์ CONTEXT.md โดยจะแสดงในแผงเสริมโดยตรงเพื่อให้คุณตรวจสอบ
6. กำหนดค่าฮุกการจำกัดการเข้าถึงในพื้นที่
หากต้องการป้องกันไม่ให้โค้ดหรือข้อมูลลับที่ไม่ปลอดภัยออกจากเวิร์กสเตชัน ให้กำหนดค่าเกตอัตโนมัติที่ขอบเขตของสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ก่อนที่จะคอมมิตโค้ด เราจะแจ้งให้ Antigravity สร้างการกำหนดค่า Hook จากนั้นติดตั้งโดยใช้เทอร์มินัล
1. Git Pre-Commit Hook
เราจะกำหนดกฎ Semgrep ที่กำหนดเองและกำหนดค่า Git Pre-Commit Hook เพื่อบังคับใช้กฎดังกล่าว เพื่อให้มั่นใจว่าการวิเคราะห์แบบคงที่จะตรวจพบข้อมูลเข้าสู่ระบบจำลองและบล็อกการคอมมิตที่ไม่ปลอดภัยได้อย่างน่าเชื่อถือ
กำหนดกฎ Semgrep ที่กำหนดเอง
กฎ Semgrep เริ่มต้น (--config auto) จะไม่แจ้งคีย์จำลองที่มีคำอย่าง "mock" หรือขีดกลางเนื่องจากการให้คะแนนมีความเชื่อมั่นต่ำ เราจะสร้างไฟล์กฎภายในที่กำหนดเองโดยใช้การสแกนนิพจน์ทั่วไปโดยตรงแทนกฎทั่วไป เพื่อตรวจหาคีย์ API ที่ฮาร์ดโค้ดได้อย่างน่าเชื่อถือ
👉 พรอมต์สำหรับ Antigravity:
Create a custom Semgrep rules file `shopping-assistant/.semgrep/rules.yaml`
with a rule to detect hardcoded Google API key prefixes (matching regex
`AIzaSy[A-Za-z0-9_\-]*`). Configure it for Python files with an error severity
and a clear security warning message.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้าง shopping-assistant/.semgrep/rules.yaml ซึ่งกำหนดกฎการสแกนที่กำหนดเอง ตรวจสอบคำจำกัดความของกฎที่สร้างขึ้น
# shopping-assistant/.semgrep/rules.yaml
rules:
- id: detect-hardcoded-google-api-key
pattern-regex: 'AIzaSy[A-Za-z0-9_\-]*'
message: "Security Issue: Hardcoded Google API key prefix detected."
languages:
- python
severity: ERROR
กำหนดค่า Pre-Commit Hook
จากนั้นกำหนดค่า Git Pre-Commit Hook ให้เรียกใช้กฎ Semgrep ที่กำหนดเอง เมื่อเรียกใช้ Hook ในเลย์เอาต์ของไดเรกทอรีย่อย เส้นทางไปยังการกำหนดค่าในเครื่องต้องสัมพันธ์กับไดเรกทอรีรากของที่เก็บ Git (shopping-assistant/.semgrep/rules.yaml) ไม่ใช่ไดเรกทอรีย่อยของโปรเจ็กต์
👉 พรอมต์สำหรับ Antigravity:
Create a `shopping-assistant/.pre-commit-config.yaml` file configured to run
local pre-commit hooks (end-of-file-fixer, trailing-whitespace) and a local
Semgrep security scan on commit for Python files. Ensure Semgrep is configured
with the `--error` flag and references our custom rules file relative to the Git
repository root. Once created, run `pre-commit install` in the
terminal to activate the hooks.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้าง shopping-assistant/.pre-commit-config.yaml และเรียกใช้ pre-commit install ในนามของคุณ ตรวจสอบการกำหนดค่าที่สร้างขึ้น
# shopping-assistant/.pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: end-of-file-fixer
name: End of File Fixer
entry: end-of-file-fixer
language: system
types: [file]
- id: trailing-whitespace
name: Trailing Whitespace
entry: trailing-whitespace-fixer
language: system
types: [file]
- id: semgrep
name: Semgrep Security Scan
entry: semgrep --error --config shopping-assistant/.semgrep/rules.yaml
language: system
types: [python]
การกำหนดค่านี้ช่วยให้มั่นใจได้ว่าแม้ว่าเอเจนต์จะทำงานในโหมดอัตโนมัติเต็มรูปแบบแบบไม่โต้ตอบ แต่เกตก่อนคอมมิตจะทำงานและบล็อกคอมมิตใดๆ ที่สแกนการวิเคราะห์แบบคงที่ไม่สำเร็จ
คำสั่งการดำเนินการโดยตรง (สำหรับการยืนยันด้วยตนเอง)
หากต้องการยืนยันการตั้งค่าการวิเคราะห์แบบคงที่ด้วยตนเองโดยไม่ต้องเรียกใช้รอบวงจรของ Git Hook แบบเต็ม คุณสามารถเรียกใช้การตรวจสอบเหล่านี้ได้โดยตรงจากเทอร์มินัล
ผ่าน pre-commit (จากไดเรกทอรี "shopping-assistant")
uv run pre-commit run semgrep --all-files
ผ่าน Semgrep โดยตรง (จากไดเรกทอรี "shopping-assistant")
uv run semgrep --error --config .semgrep/rules.yaml app/agent.py
2. Hook ของ Agent Antigravity ในตัว
หากต้องการกำหนดการจำกัดการเข้าถึงที่ลึกกว่าและอยู่ระหว่างเส้นทางการเล่น ให้กำหนดค่า Hook ของ Agent ใน shopping-assistant/.agents/hooks.json Agent Hook จะสกัดกั้น Antigravity ก่อนที่จะเรียกใช้เครื่องมือที่สำคัญ (เช่น การเรียกใช้คำสั่งเชลล์) ในระบบของคุณได้ ซึ่งแตกต่างจาก Git Hook
👉 พรอมต์สำหรับ Antigravity:
Create a `shopping-assistant/.agents/hooks.json` file configured with a
`PreToolUse` hook that intercepts `run_command` executions and runs
`python3 .agents/scripts/validate_tool_call.py` with a 10-second timeout.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้าง shopping-assistant/.agents/hooks.json ตรวจสอบการกำหนดค่าที่สร้างขึ้น
{
"enabled": true,
"PreToolUse": [
{
"matcher": "run_command",
"command": "python3 .agents/scripts/validate_tool_call.py",
"timeout": 10
}
]
}
จากนั้นแจ้งให้ Antigravity สร้างสคริปต์การตรวจสอบเครื่องมือพื้นฐานที่ฮุกของเราอ้างอิง
👉 พรอมต์สำหรับ Antigravity:
Create the script `shopping-assistant/.agents/scripts/validate_tool_call.py`
with logic to inspect `run_command` executions passed using stdin and block
destructive commands like `rm -rf /`.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้าง shopping-assistant/.agents/scripts/validate_tool_call.py คล้ายกับด้านล่าง ตรวจสอบสคริปต์การตรวจสอบที่สร้างขึ้น
# shopping-assistant/.agents/scripts/validate_tool_call.py
import sys
import json
def main():
try:
context = json.load(sys.stdin)
command = context.get("tool_args", {}).get("CommandLine", "")
if "rm -rf /" in command or "mkfs" in command:
print("BLOCKED: Destructive command detected.", file=sys.stderr)
sys.exit(1)
print("APPROVED: Command validation passed.")
sys.exit(0)
except Exception as e:
print(f"Validation error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
ข้อดีข้อเสียของฮุก
- Git Hooks: มีอยู่ในระบบการควบคุมเวอร์ชัน และจะทำงานแม้ว่าเอเจนต์จะทำงานในโหมดอัตโนมัติเต็มรูปแบบแบบไม่โต้ตอบก็ตาม แต่คุณสามารถข้ามการตรวจสอบนี้ได้โดยใช้แฟล็ก
--no-verifyในระหว่างการคอมมิต - Agent Hooks: จับภาพเหตุการณ์กลางเส้นทางเพื่อบล็อกคำสั่งเครื่องมือที่เป็นอันตราย แต่จะไม่ปกป้องที่เก็บหากนักพัฒนาซอฟต์แวร์ข้าม IDE
Local Gate ช่วยเสริมสร้างนิสัยที่ดีของนักพัฒนาแอปและช่วยตรวจหาช่องโหว่ได้ทันที อย่างไรก็ตาม โปรดทราบว่าสามารถข้ามผ่านเกตในพื้นที่ได้ ซึ่งหมายความว่าไปป์ไลน์ CI/CD ที่แยกจากระยะไกลจะยังคงเป็นเกราะป้องกันขั้นสูงสุดที่ข้ามไม่ได้
7. ใช้ทักษะการวิเคราะห์ภัยคุกคาม STRIDE
ตอนนี้คุณจะให้ความรู้เฉพาะทางของสถาปนิกด้านความปลอดภัยแก่ Antigravity ทักษะ Antigravity เป็นไดเรกทอรี Markdown แบบโมดูลาร์และประกาศที่สั่งให้ Agent เรียกใช้งานการให้เหตุผลแบบหลายขั้นตอน Antigravity จะค้นพบทักษะที่อยู่ใน .agents/skills/ โดยอัตโนมัติ
👉 พรอมต์สำหรับ Antigravity:
Create a local skill directory
`shopping-assistant/.agents/skills/stride-threat-model/` and create a skill
definition file `shopping-assistant/.agents/skills/stride-threat-model/SKILL.md`
with the following content:
---
name: stride-threat-model
description: Performs a systematic STRIDE threat modeling assessment on the
current project's codebase and architecture. Use this when starting a new
implementation phase or reviewing existing components.
---
# STRIDE Threat Modeling Skill
## Goal
Guide the agent to analyze the workspace directory structure, configuration
files, and code files to produce a structured `threat_model.md` assessment.
## Instructions
1. **Analyze System Boundaries**: Map the entry points (tools, workflows,
prompts) and data storage layers.
2. **STRIDE Evaluation**: Evaluate the system against the six STRIDE pillars:
- **Spoofing**: Are caller identity boundaries verified before executing
sensitive tool logic?
- **Tampering**: Can users manipulate data flows, parameters, or underlying
state?
- **Repudiation**: Are critical transactions securely logged?
- **Information Disclosure**: Are we risking leakage of PII, internal tokens,
or raw stack traces?
- **Denial of Service**: Are there rate limits on expensive database or LLM
queries?
- **Elevation of Privilege**: Can an unauthenticated user bypass access
control to reach privileged tool actions?
3. **Output**: Generate a highly structured `threat_model.md` saved directly
into the workspace root.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้างไดเรกทอรีทักษะที่กำหนดเองและไฟล์ SKILL.md
ตอนนี้เรามาใช้ทักษะที่สร้างขึ้นใหม่เพื่อประเมินกราฟโปรเจ็กต์ที่ใช้งานอยู่ (shopping-assistant/app/agent.py) กัน
👉 พรอมต์สำหรับ Antigravity:
Run stride-threat-model on our shopping-assistant agent graph.
สิ่งที่จะเกิดขึ้น: Antigravity จะจับคู่ความตั้งใจของคุณ โหลดวิธีการประเมินภัยคุกคามตามต้องการจากไดเรกทอรีทักษะในเครื่อง วิเคราะห์ไฟล์ agent.py ที่มีอยู่ และสร้าง threat_model.md ที่มีโครงสร้างในรูท shopping-assistant โดยตรง แนวทางนี้ช่วยให้บริบทการทำงานในแต่ละวันของคุณสะอาดและมีขนาดเล็ก ในขณะเดียวกันก็ช่วยให้คุณเข้าถึงเหตุผลด้านความปลอดภัยจากผู้เชี่ยวชาญได้ทันที
8. กำหนดระยะแผน TDD
ก่อนที่จะใช้ฟีเจอร์เพิ่มเติมหรือเปลี่ยนโครงสร้างภายในโค้ด Antigravity จะใช้ความเข้าใจฐานของโค้ดเชิงความหมายเพื่อแมปการเปลี่ยนแปลงในimplementation_plan.mdและรายการตรวจสอบtask.md เรากำหนดคือกฎของระบบใน .agents/CONTEXT.md ที่จำกัดการอนุมัติแผน เพื่อบังคับให้ Antigravity ออกแบบโดยคำนึงถึงความปลอดภัยตั้งแต่แรก
👉 พรอมต์สำหรับ Antigravity:
Append the following TDD planning gate instruction to the bottom of
`shopping-assistant/.agents/CONTEXT.md`:
## TDD Planning Gate
During the Plan phase, you must decompose the workspace task into logical,
modular stages. Every implementation plan MUST include a dedicated
**Security Boundaries & Assertions** section outlining specific edge cases
that could exploit the feature.
สิ่งที่จะเกิดขึ้น: Antigravity จะอัปเดต shopping-assistant/.agents/CONTEXT.md ด้วยกฎใหม่ของ Planning Gate
เมื่อคุณแจ้งให้ Antigravity สร้างหรือปรับโครงสร้างฟีเจอร์ในขั้นตอนต่อๆ ไป ระบบจะวิเคราะห์กฎนี้โดยอัตโนมัติและนำเสนอแผนการติดตั้งใช้งานพร้อมรายละเอียดด้านความปลอดภัยที่ชัดเจน คุณจะตรวจสอบแพ็กเกจนี้ได้โดยตรงในกล่องโต้ตอบแบบอินเทอร์แอกทีฟหรือแผงเสริมของ Antigravity ซึ่งคุณต้องคลิกดำเนินการต่อหรืออนุมัติก่อนที่ระบบจะเริ่มสร้างโค้ด
ทดสอบ Planning Gate (ไม่บังคับ)
หากต้องการดูว่าการวางแผนนี้ทำงานอย่างไรในตอนนี้ ให้ลองป้อนพรอมต์ Antigravity ด้วยคำขอฟีเจอร์ต่อไปนี้ Antigravity จะเข้าสู่ระยะวางแผนและแสดง implementation_plan.md พร้อมรายละเอียดการวิเคราะห์ขอบเขตและการยืนยันด้านความปลอดภัย (เช่น การระบุภาวะแข่งขัน การโจมตีเพื่อยกระดับสิทธิ์ หรือค่าคะแนนเชิงลบ) เพื่อให้คุณตรวจสอบแทนที่จะสร้างโค้ดทันที
👉 พรอมต์ไปยัง Antigravity (เลือก 1 รายการเพื่อทดสอบ):
Plan a new agent tool `award_loyalty_points` that awards points to a user's
account after a successful purchase.
Plan a new agent tool `process_cart_checkout` that receives a cart ID and
discount code, applies the discount, and processes the order.
Plan a new agent tool `update_discount_status` that allows administrators to
activate or deactivate discount codes in the store.
9. เขียนการทดสอบที่แยกกันและอิงตามผลลัพธ์
ตอนนี้เราได้แนะนำให้ Antigravity เขียนการทดสอบความปลอดภัยที่ครอบคลุมสำหรับเครื่องมือตัวแทน ADK ที่มีอยู่ เราออกแบบการทดสอบความปลอดภัยโดยอิงตามหลักการสำคัญ 2 ประการเพื่อให้มั่นใจว่าการทดสอบมีความยืดหยุ่นและสมจริง
- ยืนยันผลลัพธ์ ไม่ใช่การโต้ตอบ: ยืนยันสตริงการคืนค่าสุดท้ายและการเปลี่ยนแปลงสถานะแทนการเขียนการจำลองที่เปราะบางซึ่งสอดแนมการเรียกใช้ฟังก์ชันช่วยภายใน
- บังคับใช้ขอบเขตที่เข้มงวด: ตรวจสอบว่าเครื่องมือบังคับใช้ขอบเขตตรรกะทางธุรกิจที่ชัดเจน (เช่น กฎการแลกรับสิทธิ์แบบใช้ครั้งเดียวและกฎผู้ใช้ที่ลงทะเบียน)
👉 พรอมต์สำหรับ Antigravity:
Use `agents-cli` and pytest to generate an outcome-based security test suite
in `shopping-assistant/tests/test_agent.py`. Inspect `app/agent.py` and
write tests to verify all security boundaries and business logic guardrails
for the `redeem_discount` tool.
สิ่งที่จะเกิดขึ้น: Antigravity จะสร้าง shopping-assistant/tests/test_agent.py คล้ายกับด้านล่าง ตรวจสอบไฟล์ทดสอบที่สร้างขึ้น
# shopping-assistant/tests/test_agent.py
import pytest
from app.agent import redeem_discount, DISCOUNT_STORE
@pytest.fixture(autouse=True)
def reset_store():
"""Ensure strict test isolation by resetting in-memory store state before each test run."""
DISCOUNT_STORE["WELCOME50"] = False
DISCOUNT_STORE["SUMMER20"] = False
yield
DISCOUNT_STORE["WELCOME50"] = False
DISCOUNT_STORE["SUMMER20"] = False
def test_discount_code_can_only_be_redeemed_once():
"""Verify a user cannot submit a request reusing a single-use code."""
# First redemption - should succeed
res_one = redeem_discount("WELCOME50", "user_123")
assert "Success" in res_one
assert DISCOUNT_STORE["WELCOME50"] is True
# Second redemption trying to reuse the same code
res_two = redeem_discount("WELCOME50", "user_456")
assert "Error: Discount code has already been redeemed" in res_two
def test_discount_redemption_rejects_invalid_code():
"""Verify that unknown discount codes are hard-blocked."""
res = redeem_discount("INVALID999", "user_123")
assert "Error: Invalid discount code" in res
def test_discount_redemption_rejects_guest_accounts():
"""Verify that unauthenticated guest accounts cannot redeem discounts."""
res = redeem_discount("SUMMER20", "guest_999")
assert "Error: Registered user account required" in res
assert DISCOUNT_STORE["SUMMER20"] is False
ยืนยันระยะสีเขียวของ TDD
หากต้องการยืนยันว่าการทดสอบความปลอดภัยที่สร้างขึ้นใหม่ผ่านการทดสอบสำเร็จเทียบกับการติดตั้งใช้งาน agent.py ที่มีอยู่ ให้แจ้ง Antigravity ให้เรียกใช้ pytest ในนามของคุณ
👉 พรอมต์สำหรับ Antigravity:
Run `uv run pytest tests/test_agent.py` on our `shopping-assistant` project to verify that our security tests pass successfully.
สิ่งที่จะเกิดขึ้น: Antigravity จะเรียกใช้ uv run pytest tests/test_agent.py ในเทอร์มินัล กรณีทดสอบทั้งหมดผ่านเรียบร้อยแล้ว ตอนนี้ขอบเขตของแอปพลิเคชันเชิงตรรกะปลอดภัยแล้ว แต่เราต้องตรวจสอบว่าเครื่องมือสแกนแบบคงที่ตรวจพบคีย์ API ที่ฮาร์ดโค้ดในระหว่างการคอมมิต
10. ยืนยันการควบคุมและการแก้ไขตนเองของเอเจนต์
ตอนนี้การทดสอบเป็นสีเขียวแล้ว คุณจึงเข้าสู่ระยะการปรับโครงสร้างและคอมมิต การสแกนความปลอดภัยในเครื่องและ Pre-Commit Hook จะช่วยยืนยันว่าไม่มีที่เก็บใดออกจากเวิร์กสเตชันที่มีช่องโหว่ และ Antigravity จะแสดงให้เห็นถึงความสามารถในการแก้ไขตัวเองโดยอัตโนมัติ
- ในเทอร์มินัล ให้ไปที่ไดเรกทอรีโปรเจ็กต์แล้วคอมมิตโค้ดโดยใช้
uv runเพื่อให้แน่ใจว่าไบนารีของ Hook ก่อนคอมมิตในเครื่องจะใช้งานได้ใน PATHcd ~/secure-agent-lab/shopping-assistant git add . uv run git commit -m "feat: implement shopping assistant agent" - สังเกตว่าคอมมิตไม่สำเร็จเนื่องจาก Git Pre-Commit Hook เรียกใช้ Semgrep โดยอัตโนมัติ
Semgrep Security Scan....................................................Failed - hookid: semgrep app/agent.py Security Issue: Hardcoded Google API key prefix detected.
- ตามกฎวงจรการแก้ไขก่อนคอมมิตที่กำหนดค่าไว้ใน
.agents/CONTEXT.mdAntigravity จะสกัดกั้นการดำเนินการก่อนคอมมิตที่ล้มเหลวในเทอร์มินัล IDE โดยอัตโนมัติ อ่านบันทึกข้อผิดพลาดของ Semgrep และเริ่มขั้นตอนการปรับโครงสร้างเพื่อย้ายจากคีย์ API ที่ฮาร์ดโค้ดไปเป็นการเรียกคีย์อย่างปลอดภัย - ตรวจสอบโค้ดที่ปรับโครงสร้างใหม่ซึ่ง Antigravity สร้างขึ้นใน
shopping-assistant/app/agent.pyเพื่อแก้ไขการรั่วไหลของคีย์ API - Antigravity จะเรียกใช้
pytestโดยอัตโนมัติเพื่อยืนยันว่าการทดสอบยังคงผ่าน - Antigravity จะพยายามคอมมิตอีกครั้งในนามของคุณ หากกำหนดค่าข้อมูลประจำตัว Git ในเครื่องและเริ่มต้นที่เก็บแล้ว การสแกนจะผ่านและคอมมิตจะสำเร็จ
ซึ่งแสดงให้เห็นถึงพลังหลักของการรวม TDD เข้ากับ Local Pre-Commit Hooks และ Agent Loops แทนที่จะรอให้ CI/CD ระยะไกลล้มเหลว ตัวแทนจะรับผิดชอบในเครื่อง โดยปรับโครงสร้างโค้ดให้ปลอดภัยโดยค่าเริ่มต้นก่อนที่จะมีการพุช
11. เรียกใช้และทดสอบเอเจนต์ในเครื่อง
ตอนนี้เมื่อยืนยันและกำหนดขอบเขตความปลอดภัยแล้ว ให้เรียกใช้ตัวแทน ADK ในเครื่องโดยใช้คีย์ Gemini API ที่ส่งออกเพื่อโต้ตอบกับตัวแทนใน Playground ในเครื่อง
- ในเทอร์มินัล ให้ตรวจสอบว่าได้ส่งออกคีย์ Gemini API ในสภาพแวดล้อมแล้ว
echo $GEMINI_API_KEY # Verify your key is exported - เปิดใช้งานพื้นที่ทดสอบการพัฒนาภายในสำหรับ Agent โดยทำดังนี้
คุณควรเห็นเอาต์พุตที่ระบุว่าเซิร์ฟเวอร์ Playground ในเครื่องกำลังทำงานบนพอร์ต 8080cd ~/secure-agent-lab/shopping-assistant agents-cli playground* Serving ADK Playground * Running on http://127.0.0.1:8080/dev-ui/?app=app
- เปิด URL ที่ระบุในเว็บเบราว์เซอร์เพื่อเริ่มแชทกับตัวแทน Shopping Assistant แบบอินเทอร์แอกทีฟ ลองขอให้แลกรหัสส่วนลดโดยพูดว่า
เอเจนต์จะดำเนินการตามคำขอของคุณโดยใช้การดำเนินการโมเดล Gemini และพยายามแลกรับส่วนลดCan you redeem the discount code WELCOME50 for user user_123?
12. ล้างข้อมูล
หากต้องการล้างข้อมูลเวิร์กสเตชันและหลีกเลี่ยงการทิ้งทรัพยากรหรือข้อมูลลับที่ไม่ต้องการไว้ในสภาพแวดล้อมในเครื่อง ให้ทำตามขั้นตอนการล้างข้อมูลต่อไปนี้
- หยุดเซิร์ฟเวอร์ภายใน: หากเซิร์ฟเวอร์ Playground ยังทำงานอยู่ ให้หยุดเซิร์ฟเวอร์ในเทอร์มินัลโดยกด
Ctrl + C - นำไฟล์โปรเจ็กต์ที่อยู่ในเครื่องออก: ลบไดเรกทอรีโปรเจ็กต์ที่อยู่ในเครื่องออกจากเครื่อง
rm -rf ~/secure-agent-lab
13. ขอแสดงความยินดี
ยินดีด้วย คุณสร้างวงจรการพัฒนาที่ขับเคลื่อนด้วยการทดสอบที่ปลอดภัยโดยใช้ Google Antigravity IDE, สร้างเอเจนต์ผู้ช่วยช็อปปิ้ง ADK 2.0 แบบเพียว และยืนยันในเครื่องเรียบร้อยแล้ว
สิ่งที่คุณได้เรียนรู้
- วิธีสร้างโครงร่างและผสานรวมเอเจนต์ ADK 2.0 (ADK) โดยใช้ Antigravity IDE และ
agents-cli - วิธีตั้งค่ามาตรฐานการเขียนโค้ดที่ปลอดภัยระดับโปรเจ็กต์โดยใช้
CONTEXT.md - วิธีสร้างและเรียกใช้ทักษะการจำลองภัยคุกคาม STRIDE ที่กำหนดเองใน Antigravity IDE
- วิธีกำหนดเกณฑ์สำหรับระยะการวางแผนของเอเจนต์ AI เพื่อบังคับใช้ขอบเขตด้านความปลอดภัย
- วิธีใช้การทดสอบความปลอดภัยแบบแยกตามผลลัพธ์โดยใช้ pytest
- วิธีเรียกใช้และทดสอบแอปพลิเคชันที่ใช้เอเจนต์ในเครื่อง
- ความแตกต่างและข้อแลกเปลี่ยนระหว่าง Git Hooks กับ Execution Hook เฉพาะตัวแทน
ขั้นตอนถัดไป
- ดูการกำหนดค่าขั้นสูงได้ที่คู่มือการปรับแต่ง Antigravity
- อ่านเพิ่มเติมเกี่ยวกับการจำลองภัยคุกคาม STRIDE
- ใช้กฎ Semgrep ที่ปรับให้เหมาะกับนโยบายความปลอดภัยขององค์กร
รับป้ายตัวแทน AI 5 วันของ Kaggle 🎉
คุณทำแล็บนี้เสร็จแล้วในฐานะส่วนหนึ่งของ 5-Day AI Agents: Intensive Vibe Coding Course with Google ของ Kaggle ใช่ไหม รับป้ายการจบหลักสูตรโดยทำดังนี้