IntelliTect / IntelliTect/EssentialCSharp.Web
Security: No Content Safety Filtering or Output Guardrails on AI Responses
- Ngôn ngữ chính
- HTML
- Star
- 8
- Fork
- 8
- Merge trung bình
- 17 giờ 23 phút
- Pull request đã merge (30 ngày)
- 61
Mô tả
## Summary
The AI chat pipeline returns raw Azure OpenAI output directly to users with no output validation, PII detection, content safety filtering, or structured output enforcement. This means harmful, policy-violating, or sensitive content generated by the model would be passed straight through.
## Affected Code
**`EssentialCSharp.Web/Controllers/ChatController.cs`** — both `/api/chat/message` and `/api/chat/stream`:
```csharp
// Non-streaming: raw model text returned without any inspection
return Ok(new ChatMessageResponse { Response = response, ... });
// Streaming: raw delta text written directly to SSE stream
var eventData = JsonSerializer.Serialize(new { type = "text", data = text });
await Response.WriteAsync($"data: {eventData}\n\n", cancellationToken);
```
**`AIChatService.cs`** — `GetChatCompletionCore` returns `responseText` from the model with no post-processing.
There is also no content safety check on user **input** before it is sent to the model.
## Risk
**OWASP AI Agent Security — Risk #5: Output Validation & Guardrails / Risk #8: Sensitive Data Exposure**
- A jailbroken or misconfigured model could return harmful content, credentials from its training data, or PII.
- No defense against the model leaking content from its system prompt or injected context back to the user.
- No enforcement that outputs conform to expected shape (e.g., markdown response about C# topics only).
## Recommended Mitigations
1. **Integrate Azure AI Content Safety** for both input (before sending to model) and output (before returning to client):
```csharp
// In AIChatService or a middleware wrapper:
var inputSafety = await contentSafetyClient.AnalyzeTextAsync(prompt);
if (inputSafety.Value.CategoriesAnalysis.Any(c => c.Severity > 2))
return ("Content policy violation.", null);
```
2. **Apply a system prompt instruction** bounding output scope: "Only respond about C# and Essential C# book topics."
3. **Screen output for PII patterns** (emails, phone numbers, SSNs) before streaming/returning:
```csharp
private static readonly Regex PiiPattern = new(@"\b\d{3}-\d{2}-\d{4}\b|\b[\w.]+@[\w.]+\b", RegexOptions.Compiled);
```
4. **Set `max_output_tokens`** on the `ResponseCreationOptions` to enforce an output length cap and prevent runaway generation costs.
## References
- [OWASP AI Agent Security Cheat Sheet — §5 Output Validation & Guardrails](https://cheatsheetseries.owasp.org/cheatsheets/AI_Agent_Security_Cheat_Sheet.html#5-output-validation-guardrails)
- [Azure AI Content Safety](https://learn.microsoft.com/en-us/azure/ai-services/content-safety/overview)
- [OWASP LLM Top 10 — LLM02: Insecure Output Handling](https://owasp.org/www-project-top-10-for-large-language-model-applications/)
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu trong EssentialCSharp.Web/Controllers/ChatController.cs bằng cách lần theo các luồng phản hồi không streaming và streaming, sau đó đọc AIChatService.cs và phương thức GetChatCompletionCore của nó. So sánh các luồng đầu vào và đầu ra với các biện pháp giảm thiểu Azure AI Content Safety, PII screening, output-scope và token-limit được liệt kê trong issue. Công việc được hoàn thành khi cả hai chat endpoint và các đầu vào của model đều áp dụng các biện pháp kiểm soát an toàn và đầu ra đã thống nhất.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- azure, csharp
- Lĩnh vực
- ai, api, security
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100