Questions: 300 characters, refused when they look like prompt injection
CI / test (push) Successful in 2m3s
CI / build (push) Skipped
CI / deploy (push) Skipped

Three layers for the text users write into a prompt (questions about an
analysis, tarot questions):
- analysis_prompt::guard cleans it (controls, zero-width characters,
  spaces, empty lines) and refuses links, tags, code fences, chat role
  labels, more than 6 lines and phrases that try to change the model's
  instructions (Hungarian and English, accent-insensitive). The check runs
  before any unit is spent; refusals are logged without the text.
- The prompt puts the question (and earlier ones) between <kerdes> tags,
  with anything that could close the tag neutralized.
- The system prompts say the tagged text is data, not instructions, and
  not to quote or summarize the rules (the tarot prompts now say it too).
The question limit drops from 600 to 300 characters (as for tarot), with a
counter always shown under the box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 11:32:38 +00:00
co-authored by Claude Opus 5.5
parent f8abdac790
commit 3269aaaa35
9 changed files with 320 additions and 30 deletions
+6 -6
View File
@@ -4,7 +4,7 @@
//! free). The worker answers with the question model of the admin page,
//! seeing the chart data, the analysis and the earlier questions about it.
use analysis_prompt::{Kind, QUESTION_MAX_CHARS};
use analysis_prompt::{Kind, QUESTION_MAX_CHARS, guard};
use axum::Json;
use axum::extract::{Path, State};
use axum::http::StatusCode;
@@ -120,11 +120,11 @@ pub async fn ask(
if analysis.status != "done" {
return Err(ApiError::conflict("analysis_not_ready", "Az elemzés még nem készült el."));
}
let question = req.question.trim().replace("\r\n", "\n");
let length = question.chars().count();
if !(3..=QUESTION_MAX_CHARS).contains(&length) {
return Err(ApiError::BadRequest(format!("A kérdés 3–{QUESTION_MAX_CHARS} karakter hosszú lehet.")));
}
// Cleaned, and refused when it looks like instructions to the model (before any unit is spent).
let question = guard::check_question(&req.question, 3, QUESTION_MAX_CHARS).map_err(|refusal| {
tracing::warn!(user = %auth.id(), analysis = %id, reason = refusal.as_str(), "question refused");
ApiError::BadRequest(refusal.message())
})?;
let (open, total): (i64, i64) = sqlx::query_as(
"SELECT count(*) FILTER (WHERE status IN ('pending', 'running')), count(*) FROM questions WHERE analysis_id = $1",
)