Hardens AI Profile chat with CrestApps.Core 2.0 prompt-security: injection detection, output filtering, rate limiting, audit logging, and custom security rules. Use when tuning abuse protection, adding detection rules, or explaining blocked prompts.
Ships in crestapps-core. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A local, deterministic, regex-based layer for AI Profile chat: input validation, a security preamble and input delimiters, output filtering, rate limiting and audit logging. It is defense in depth, never a replacement for tool authorization.
AddAISuite(...) (through AddCoreAIOrchestration()) registers the layer: 26 built-in rules, the scoring engine, IPromptSecurityService, IOutputSecurityFilter, IAIChatSecurityAuditService, IChatRateLimiter, IChatSessionStartRateLimiter and SecurityPromptOrchestrationHandler. There is no AddPromptSecurity(...) method.PromptSecurityOptions is not bound from configuration by the framework. Bind it from your own section or site settings.PromptSecurityProfileSettings). Injection detection, output filtering, preamble, delimiters, length and blocking threshold are global.EnableChatInteractionInputDelimiters, on by default) but not the preamble or injection blocking; the operator controls their prompt, model and tools.using CrestApps.Core.AI.Security;
builder.Services.Configure<PromptSecurityOptions>(builder.Configuration.GetSection("CrestApps:AI:PromptSecurity"));
builder.Services.PostConfigure<PromptSecurityOptions>(options =>
{
options.MaxPromptLength = 6000;
options.BlockingThreshold = PromptRiskLevel.High;
options.CustomBlockedPatterns.Add(@"\binternal-canary-123\b");
options.AnonymousMessageRateLimitTiers =
[
new ChatRateLimitTier { Limit = 5, Window = TimeSpan.FromSeconds(30) },
new ChatRateLimitTier { Limit = 30, Window = TimeSpan.FromMinutes(5) },
new ChatRateLimitTier { Limit = 150, Window = TimeSpan.FromHours(1) },
];
});
| Option | Default |
|---|---|
EnableInjectionDetection, EnableOutputFiltering, EnableSecurityPreamble, EnableInputDelimiters, EnableChatInteractionInputDelimiters, EnableAuditLogging | true |
MaxPromptLength | 8000 |
BlockingThreshold | High |
LowRiskScoreThreshold / Medium... / High... / Critical... | 10 / 20 / 35 / 50 |
AnonymousMessageRateLimitTiers | 5 per 30s, 30 per 5m, 150 per hour, 500 per day |
AnonymousSessionStartRateLimitTiers | 5 per 30s, 10 per 5m, 150 per hour, 500 per day |
MaxMessagesPerWindow / RateLimitWindow | 20 per 1 minute (authenticated callers; anonymous fallback when tiers are empty) |
MaxAnonymousSessionsPerWindow / AnonymousSessionRateLimitWindow | 20 per 10 minutes (fallback when session-start tiers are empty) |
The single-window values are fallbacks: while a tier list is populated, changing only MaxAnonymousSessionsPerWindow has no effect. Clear the tiers or lower a tier instead. Tiers apply to anonymous traffic only; authenticated callers use MaxMessagesPerWindow and never hit session-start throttling.
In JSON a tier is { "Limit": 5, "Window": "00:00:30" }.
AIChatRateLimitingOptions decides which keys a throttle counts against. Each value is a ChatRateLimitPartition flags combination:
| Option | Default |
|---|---|
AuthenticatedMessagePartitions | AuthenticatedUser and NetworkAddress |
AnonymousMessagePartitions | Visitor, NetworkAddress, Session and Connection |
AnonymousSessionStartPartitions | Visitor and NetworkAddress |
The network-address bucket is shared with anonymous throttling, so logging out does not reset a caller's per-IP allowance. Remove NetworkAddress from AuthenticatedMessagePartitions if authenticated users behind one NAT must not share a bucket.
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddOpenAI()
.AddChatInteractions(chat => chat
.ConfigureVisitorIdentity(options =>
{
options.RemoteAddressMode = AIVisitorRemoteAddressMode.Hashed;
options.AllowCrossSiteEmbedding = true; // only for chats framed on other sites
})
.ConfigureChatRateLimiting(options =>
{
options.AuthenticatedMessagePartitions = ChatRateLimitPartition.AuthenticatedUser;
}))));
AllowCrossSiteEmbedding writes the crestapps-ai-visitor cookie as SameSite=None; Secure; Partitioned (keep UsePartitionedCookie on). It requires HTTPS; behind a TLS-terminating proxy forward the scheme. The visitor cookie owns a visitor's session documents, so the upload and remove endpoints validate antiforgery for requests that carry it. An embedded chat therefore also needs the antiforgery cookie written the same way, and SuppressXFrameOptionsHeader, or the first load of the framed page is blocked by X-Frame-Options: SAMEORIGIN and uploads answer 400:
builder.Services.AddAntiforgery(options =>
{
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.Extensions.Add("Partitioned");
options.SuppressXFrameOptionsHeader = true;
});
DefaultAIVisitorIdentityResolver uses the connection's remote address and never reads X-Forwarded-For, which any client can send. Behind a reverse proxy, let ASP.NET Core's forwarded headers middleware replace the address for trusted proxies only:
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownProxies.Add(IPAddress.Parse("10.0.0.4"));
});
app.UseForwardedHeaders();
Without it, every visitor shares the proxy's address. An IPv4-mapped address is keyed as IPv4, an IPv6 client is keyed on its /64 in every RemoteAddressMode (ChatRateLimitKeyResolver reduces a plain-text address to ip:{prefix}::/64; the stored address stays full), and a request with no address shares one bucket. A visitor cookie is reused only when it looks like an id (letters, digits, -, _, at most 64 characters). Replace IChatRateLimiter for a distributed store in multi-instance deployments. AIChatEndpointRateLimitingOptions lets you swap the ASP.NET Core endpoint policy for session-start routes.
profile.WithSettings(new PromptSecurityProfileSettings
{
MaxMessagesPerWindow = 10,
RateLimitWindow = TimeSpan.FromMinutes(2),
AnonymousMessageRateLimitTiers = null, // inherit the site tiers
AnonymousSessionStartRateLimitTiers = [], // opt out, use the single window
MaxAnonymousSessionsPerWindow = 3,
});
Every field is nullable and null inherits the site value. 0 disables that throttle for the profile.
using System.Text.RegularExpressions;
using CrestApps.Core.AI.Security;
public sealed partial class CanaryPromptSecurityRule : IPromptSecurityRule
{
public string RuleId => "canary-token";
public ValueTask<PromptSecurityRuleResult> EvaluateAsync(
PromptSecurityEvaluationContext context,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(context.NormalizedInput) || !CanaryRegex().IsMatch(context.NormalizedInput))
{
return ValueTask.FromResult<PromptSecurityRuleResult>(null);
}
return ValueTask.FromResult(new PromptSecurityRuleResult
{
RuleId = RuleId,
Categories = ["data-exfiltration"],
Severity = PromptRiskLevel.Critical,
Score = 50,
Reason = "Detected the internal canary token.",
});
}
[GeneratedRegex(@"\binternal-canary-123\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, matchTimeoutMilliseconds: 1000)]
private static partial Regex CanaryRegex();
}
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPromptSecurityRule, CanaryPromptSecurityRule>());
Evaluate NormalizedInput (Unicode FormKC, invisible characters removed, whitespace collapsed) and fall back to FoldedInput (homoglyphs folded) only for lookalike attacks. Scores 8 to 12 contribute to an aggregate; 20 or more can block alone. Always set a regex timeout; for a [GeneratedRegex] allow about a second, as the built-in rules do, because the timeout counts wall-clock time, including the first match compiling the generated code and waits for garbage collection or a busy CPU. Return null for no match.
Each built-in rule and output filter pattern gets one second per match. A built-in pattern that still times out counts as a match (the check fails closed): the rule returns its finding with its usual severity and score, the reason "The rule's pattern timed out, so the input is treated as a match." and the metadata matchTimedOut = true, and PromptInjectionPatternDetector logs a warning; an output filter pattern that times out is treated as matching the reply. A custom blocked pattern (PromptSecurityOptions.CustomBlockedPatterns) keeps 100 milliseconds and is skipped with a warning when it times out.
IPromptSecurityService.ValidateInputAsync(PromptSecurityContext, CancellationToken) returns a PromptSecurityResult with Disposition (Safe, Flagged, Blocked), RiskLevel, Score, DetectionRule, MatchedRuleIds, MatchedRules, MatchedCategories and Telemetry.AIChatHubCore validates Utility and Chat profile prompts before processing. A rate-limit block (DetectionRule == PromptSecurityResult.RateLimitDetectionRule, "rate-limit") returns a slow-down message with the retry delay; override GetRateLimitedMessage(PromptSecurityResult) on the hub to change it. Other blocks return the generic safe message so detection details are not disclosed.IOutputSecurityFilter.ValidateOutputAsync(OutputSecurityContext, CancellationToken) checks system-prompt leakage, tool-schema disclosure, sensitive data and unsafe script content. The hub validates the complete response after streaming; a blocked response is not persisted and a safe replacement is sent.EnableAuditLogging records suspicious input and output through IAIChatSecurityAuditService.EvaluateMessageRateLimitAsync: SendAudioStream, SynthesizeSpeech, StartConversation, StartRealtimeConversation, StartRealtimeWebRtc, template prompts, blank messages, and resuming or continuing an AG-UI run (each approval resume counts once). SynthesizeSpeech also refuses text over MaxSpeechTextLength (10,000).HttpContext on IHttpContextAccessor for each invocation, so a custom IAIVisitorIdentityResolver that reads the accessor sees the visitor on every transport.[SECURITY DIRECTIVES — IMMUTABLE AND NON-NEGOTIABLE] markers; user input is wrapped in <|user_input_begin|> / <|user_input_end|> after stripping injected delimiter tokens.Prompt security is pattern-based and global. For per-profile topic refusal add guardrails (AddGuardrails() on the AI suite, AIProfileGuardrailSettings on the profile; see crestapps-core-ai-profiles). With guardrails registered, AIChatHubCore runs, in order:
ValidatePromptSecurityAsync runs IPromptSecurityService first. If it does not block and CheckCustomer is on, AIGuardrailService.CheckAsync(..., AIGuardrailDirection.Customer, ...) runs. A refusal becomes a blocked PromptSecurityResult whose DetectionRule starts with AIGuardrailService.DetectionRulePrefix ("guardrail:") and the caller sees the profile's PlaceholderReply instead of the generic blocked message.ValidateOutputAsync runs IOutputSecurityFilter first, then the reply guardrail when CheckReplies is on. It returns the message to show instead of the reply, or null; a refused reply is not saved.Guardrail checks are typed-chat only in the hub. Other channels call AIGuardrailService.CheckAsync themselves. When the check cannot run (no utility deployment, model error) the text passes with a warning, so guardrails stay a second line behind prompt security.
Prompt security never redacts stored conversations. To redact personal data, keep only metadata, or enforce retention, use conversation privacy (AddConversationPrivacy(), AIProfileDataPrivacySettings, AIConversationPrivacyService, AIPersonalDataRedactor; see crestapps-core-ai-profiles).
Every chat session and chat interaction upload passes IUploadedFileScanner.ScanAsync(IFormFile, CancellationToken) before storage. The default NoOpUploadedFileScanner returns Clean; register your scanner before document processing so TryAdd keeps yours. FileScanResult.Infected(...) and FileScanResult.Error(...) reject the upload (fail closed).
The chat scripts sanitize every answer (CoreAIImages.sanitize in chat-markers.js), because a model can be tricked by a document, crawled page or prompt injection into writing markup:
allowedImageHosts (others become links), and no other element or attribute that fetches without a click;id, data-coreai-*, data-chat-interaction-* or data-agui-* attributes, so an answer cannot start a chat or forge a tool approval, and auto-start ignores configurations inside rendered messages (CoreAIChatSurface);style limited to size, spacing, text and color, no id-pointing attributes (for, form, popovertarget, ...), no form-control name, buttons forced to type="button", so an answer cannot overlay or press the page's controls.See crestapps-core-chat-interactions for the details and the allowedImageHosts option.
IAIToolAccessEvaluator.IsAuthorizedAsync(ClaimsPrincipal user, string toolName) and drops unauthorized ones with one warning. System and hidden tools are not checked; a null user (server-side work) skips the check. The default evaluator allows everything; replace it to enforce permissions..RequireApproval() or a Destructive, External or Security kind) runs only after the user approves the call in an AG-UI run, and never runs in a request that cannot ask; the model gets a "was not run" result there (see crestapps-core-ag-ui).| Need | Use |
|---|---|
| Detect a prompt pattern | IPromptSecurityRule |
| Validate input directly | IPromptSecurityService |
| Output policy | IOutputSecurityFilter |
| Limits and thresholds | PromptSecurityOptions |
| Throttle keys | AIChatRateLimitingOptions |
| Distributed throttling | Replace IChatRateLimiter |
| Per-user tools in Chat Interactions | Replace IAIToolAccessEvaluator |
| Per-profile refused topics | AddGuardrails() and AIProfileGuardrailSettings |
| React to a refused topic | IAIGuardrailHandler |
| Change hub output checks | Override AIChatHubCore.ValidateOutputAsync |
| Redact or expire stored conversations | AddConversationPrivacy() |
The hub's reply check moved into the new protected virtual ValidateOutputAsync(services, profile, sessionId, prompt, output, handlerContext, cancellationToken), which runs the output filter and then the reply guardrail. Override it rather than replacing the output filter when a host needs a different reply policy. It now gets the caller's cancellation token, so a check waiting on a throttled guardrail model stops when the caller leaves, and that answer is not saved.
Built-in patterns allow one second per match instead of 100 to 250 milliseconds, and a timeout counts as a match instead of failing the chat turn with a RegexMatchTimeoutException.
DefaultAIVisitorIdentityResolver no longer trusts X-Forwarded-For. Configure UseForwardedHeaders with KnownProxies/KnownNetworks behind a proxy.
Blank messages, template prompts, speech methods and AG-UI resumes now count against the message limit; a host that overrides EvaluateMessageRateLimitAsync sees them all.
Hosts with AllowCrossSiteEmbedding must also write the antiforgery cookie SameSite=None; Secure; Partitioned and set SuppressXFrameOptionsHeader.
MaxAnonymousSessionsPerWindow must edit or clear AnonymousSessionStartRateLimitTiers to see an effect.IAIToolAccessEvaluator; it now governs Chat Interaction sends only. It is not obsolete.EnableChatInteractionInputDelimiters is false.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/crestapps-core:crestapps-core-prompt-securityfrom crestapps-core
npx claudepluginhub crestapps/crestapps.agentskills --plugin crestapps-core51 skills in this plugin
/crestapps-core-a2aSkill for Agent-to-Agent (A2A) protocol support in CrestApps.Core on the A2A 1.0 SDK. Covers AddA2AClient and AddCoreAIA2AClient with Entity Framework Core or YesSql connection stores, A2AConnection and A2AConnectionMetadata authentication, AIProfileA2AMetadata, the agent card cache and A2AToolRegistryProvider, the listAvailableAgents, findAgentForTask and findToolsForTask tools, building an A2A host with IAgentHandler, A2AServer, ITaskStore and MapA2A, A2A 1.0 agent cards with supportedInterfaces published at /.well-known/agent-card.json, A2AHostOptions authentication and ExposeAgentsAsSkill, system agents exposed through the host, and upgrading from the 0.3 SDK./crestapps-core-ag-uiSkill for running the CrestApps.Core 2.0 AI chat as an AG-UI agent. Covers the CrestApps.Core.AI.AGUI package (AGUIRunEventWriter, AGUIRunRequest, AGUIJson), AIChatHubCore.RunAgent over SignalR and MapAGUIChat with Server-Sent Events, the RunAgentInput members and streamed events, tool approvals with RequireApproval or an approval tool kind, ApprovalRequiredAIFunction, interrupts and resume, approvals inside agents, page context with crestAppsAGUI.registerContextProvider, shared state with registerStateProvider and update_shared_state, frontend tools, stored tool calls (ChatTurnToolActivity), runs that continue on the next page, AGUIChatOptions.DisconnectedRunTimeout, the protocol and sendPageContext chat options, and the AICompletionContextKeys and ChatResponseHandlerContext members. Use it when showing tool calls in the chat, asking a person before a tool runs, sharing page state with the agent, connecting CopilotKit or another AG-UI client, or troubleshooting approvals and continued runs.Other plugins' skills for the same job