Questions: 300 characters, refused when they look like prompt injection
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:
@@ -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",
|
||||
)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! reading gives its unit back.
|
||||
|
||||
use analysis_prompt::tarot::{self, Spread};
|
||||
use analysis_prompt::{Kind, TAROT_QUESTION_MAX_CHARS};
|
||||
use analysis_prompt::{Kind, TAROT_QUESTION_MAX_CHARS, guard};
|
||||
use axum::Json;
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::StatusCode;
|
||||
@@ -162,12 +162,14 @@ pub async fn create(
|
||||
) -> ApiResult<(StatusCode, Json<Value>)> {
|
||||
let spread = tarot::spread(&req.spread).ok_or_else(|| ApiError::BadRequest("unknown spread".into()))?;
|
||||
let topic = tarot::topic(req.topic.as_deref()).ok_or_else(|| ApiError::BadRequest("unknown topic".into()))?;
|
||||
let question = req.question.map(|q| q.trim().replace("\r\n", "\n")).filter(|q| !q.is_empty());
|
||||
if question.as_ref().is_some_and(|q| q.chars().count() > TAROT_QUESTION_MAX_CHARS) {
|
||||
return Err(ApiError::BadRequest(format!(
|
||||
"the question is at most {TAROT_QUESTION_MAX_CHARS} characters long"
|
||||
)));
|
||||
}
|
||||
// Optional; when given, cleaned and checked as a question about an analysis is.
|
||||
let question = match req.question.as_deref().map(str::trim).filter(|q| !q.is_empty()) {
|
||||
None => None,
|
||||
Some(q) => Some(guard::check_question(q, 3, TAROT_QUESTION_MAX_CHARS).map_err(|refusal| {
|
||||
tracing::warn!(user = %auth.id(), reason = refusal.as_str(), "tarot question refused");
|
||||
ApiError::BadRequest(refusal.message())
|
||||
})?),
|
||||
};
|
||||
let open: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS (SELECT 1 FROM analyses WHERE kind = 'tarot' AND requested_by = $1 \
|
||||
AND status IN ('pending', 'running'))",
|
||||
|
||||
Reference in New Issue
Block a user