BUG: evaluate-assessments Edge Function crashes on missing/malformed input and silently returns zero score on unmatched answers

Open
#171 0 comments 0 reactions 0 assignees View on GitHub

@Varadraj75 is already working on this.

Since Mar 1, 2026.

  • #172 by @Varadraj75 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
typescript
Domain
api, backend

Research direction

Start in supabase/functions/evaluate-assessments/index.ts by inspecting request parsing and the scoring loop. Run the two curl reproductions from the issue with missing input and unmatched IDs. Done means malformed requests return 400, and requests with no scorable questions return an error instead of a zero score.

Written by the indexing model from the issue text.

Description

Is there an existing issue for this?
  • I have searched the existing issues
What happened?

📌 Issue Overview

The evaluate-assessments Edge Function has two related bugs:

  1. No input validation — if the request body is empty, malformed JSON,
    or missing patient_id, assessment_id, or questions, the function
    crashes with an unhandled TypeError (e.g. questions.map is not a function) before reaching the try/catch, returning a 500 with no useful
    error message.

  2. Silent zero score — if none of the submitted question_id/answer_id
    pairs match the assessment data (e.g. stale client data, wrong IDs), the
    scoring loop silently continues every iteration, totalScore stays 0,
    and the patient is incorrectly told they are not autistic. There is no
    warning that 0 out of N questions were scored.

🔍 Steps to Reproduce

Bug 1 — Null dereference:

curl -X POST https://.supabase.co/functions/v1/evaluate-assessments \
  -H "Content-Type: application/json" \
  -d '{}'
# Returns 500 Internal Server Error — questions is undefined, .map() crashes

Bug 2 — Silent zero score:

curl -X POST https://.supabase.co/functions/v1/evaluate-assessments \
  -H "Content-Type: application/json" \
  -d '{"patient_id":"...","assessment_id":"...","questions":[{"question_id":"wrong-id","answer_id":"wrong-id"}]}'
# Returns 200 with assessment_score: 0 and is_autistic: false — silently wrong

🎯 Expected Behavior

  1. Missing or malformed input should return a 400 Bad Request with a clear
    error message before any processing begins.
  2. If 0 out of N questions were successfully scored, the function should
    return an error rather than a misleading result.

🚨 Actual Behavior

  1. questions.map(...) throws TypeError: Cannot read properties of undefined
    when questions is missing from the request body.
  2. A patient who submits an assessment with entirely unmatched answers receives
    assessment_score: 0 and is_autistic: false — a clinically dangerous
    false negative.

Root cause — supabase/functions/evaluate-assessments/index.ts:

//  No validation — patient_id, assessment_id, questions can all be undefined
const { patient_id, assessment_id, questions } = await req.json();

//  Crashes if questions is undefined
questions.map((q: AssessmentEvaluationQuestionDTO) => ({ ... }))

//  Silent zero — no check that at least 1 question was scored
let totalScore = 0;
for(let i=0; i<answered_questions.questions.length; i++) {
  ...
  if(!matched_question) { continue; } // silently skipped
  if(!answer) { continue; }           // silently skipped
}
// totalScore may still be 0 — no warning issued

📷 Screenshot

N/A

💡 Suggested Improvements

// 1. Validate input early
const body = await req.json().catch(() => null);
if (!body || !body.patient_id || !body.assessment_id || !Array.isArray(body.questions) || body.questions.length === 0) {
  return new Response(JSON.stringify({ error: "Missing or invalid request body" }), { status: 400 });
}

// 2. Check that at least 1 question was scored
let scoredCount = 0;
for (...) {
  ...
  totalScore += answer.score;
  scoredCount++;
}
if (scoredCount === 0) {
  return new Response(JSON.stringify({ error: "No questions could be scored — check question and answer IDs" }), { status: 422 });
}
Record
  • I agree to follow this project's Code of Conduct
  • I want to work on this issue
Dominant language
Dart
Stars
30
Forks
49
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AOSSIE-Org/NeuroTrack

All issues in AOSSIE-Org/NeuroTrack

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.