forge
Explore
Log in

Don't you dare go Hollow

Reject a duration over its window

Browse files at this commit

ea1782899c3eab2f9675011ab651b62cf6789555Ahmet Kaan GümüşParent: bdb4871
  • fix
    • reject a duration over its window
Show full message
🐛 fix(exams): reject a duration over its window

An `async` exam accepted any `duration_ms` inside the global 1min–24h
bounds, even one longer than its own `starts_at`..`ends_at` window. Such
a schedule saved happily and was then silently truncated at attempt
time: a teacher who set 90 minutes inside a 60-minute window saw a
90-minute exam echoed back, while every student actually got 60. The
lie only surfaced once students were already sitting it.

`ExamSchedule::try_new` now rejects that up front with a 400
(`duration_ms: must fit within the starts_at..ends_at window`), so the
impossible schedule never reaches the database at all. Runtime
enforcement was already correct — `ExamAttempt::deadline()` caps the
personal deadline at `ends_at` — and is untouched; this only stops the
teacher from being told something the exam will not honour.

The bound is inclusive on purpose. A duration exactly equal to the
window is a legitimate "the whole slot is yours" exam, and because
`update_exam` re-validates the entire stored schedule on every PATCH, an
exclusive bound would have made an existing row like that unpatchable
for completely unrelated edits (title, kind, attempt limit). Only
`async` can reach the check: `sync` takes no duration and `open` has no
window.

The unit test `schedule_invariants_hold` used to assert the buggy
behaviour, so it flips to the fix plus the inclusive boundary. Two
integration tests happened to be built on now-illegal schedules and were
moved onto legal ones without losing what they cover.
README.mdmodified+6 -5
@@ -740,11 +740,12 @@ consistent unit (validated together on create and after every `PATCH` merge):
740 *optional* — set it for a per-attempt countdown (`started_at +740 *optional* — set it for a per-attempt countdown (`started_at +
741 duration_ms`), omit it for unlimited time (the attempt only ends by741 duration_ms`), omit it for unlimited time (the attempt only ends by
742 submission).742 submission).
743-- `duration_ms` is 1 minute to 24 hours wherever it appears. `ends_at` must be743+- `duration_ms` is 1 minute to 24 hours wherever it appears, and for `async`
744- strictly after `starts_at`, and neither may be *set* in the past — on create744+ cannot exceed the `starts_at`..`ends_at` window (equal to it is fine).
745- or `PATCH` (kept values are exempt, so a running exam stays editable). All745+ `ends_at` must be strictly after `starts_at`, and neither may be *set* in
746- instants are the usual UTC unix-milliseconds, judged only by the server746+ the past — on create or `PATCH` (kept values are exempt, so a running exam
747- clock (`GET /time` for sync).747+ stays editable). All instants are the usual UTC unix-milliseconds, judged
748+ only by the server clock (`GET /time` for sync).
748 749
749 Two per-exam policy knobs ride along, both **live-editable** at any point:750 Two per-exam policy knobs ride along, both **live-editable** at any point:
750 751
src/domain/exam.rsmodified+19 -4
@@ -162,8 +162,9 @@ impl ExamAttemptLimit {
162 /// exam; attempts are rejected),162 /// exam; attempts are rejected),
163 /// - `sync` → `starts_at` + `ends_at`, no duration (everyone's deadline is163 /// - `sync` → `starts_at` + `ends_at`, no duration (everyone's deadline is
164 /// `ends_at`),164 /// `ends_at`),
165-/// - `async` → `starts_at` + `ends_at` + `duration_ms` (a student who starts165+/// - `async` → `starts_at` + `ends_at` + `duration_ms`, with `duration_ms` no
166-/// at `t` gets until `min(t + duration_ms, ends_at)`),166+/// greater than the window (a student who starts at `t` gets until
167+/// `min(t + duration_ms, ends_at)`),
167 /// - `open` → no window; `duration_ms` optional (a student who starts at `t`168 /// - `open` → no window; `duration_ms` optional (a student who starts at `t`
168 /// gets until `t + duration_ms`, or forever when absent),169 /// gets until `t + duration_ms`, or forever when absent),
169 /// - `ends_at` strictly after `starts_at` whenever the window exists.170 /// - `ends_at` strictly after `starts_at` whenever the window exists.
@@ -212,6 +213,17 @@ impl ExamSchedule {
212 if ends_at <= starts_at {213 if ends_at <= starts_at {
213 return Err(invalid("ends_at", "must be after starts_at"));214 return Err(invalid("ends_at", "must be after starts_at"));
214 }215 }
216+ if let (Some(duration), Some(starts), Some(ends)) =
217+ (duration_ms, starts_at, ends_at)
218+ {
219+ let window_ms = ends.as_millis().saturating_sub(starts.as_millis());
220+ if duration.as_millis() > window_ms {
221+ return Err(invalid(
222+ "duration_ms",
223+ "must fit within the starts_at..ends_at window",
224+ ));
225+ }
226+ }
215 match (m.as_str(), duration_ms.is_some()) {227 match (m.as_str(), duration_ms.is_some()) {
216 ("async", false) => {228 ("async", false) => {
217 return Err(invalid("duration_ms", "required for an async exam"));229 return Err(invalid("duration_ms", "required for an async exam"));
@@ -553,8 +565,11 @@ mod tests {
553 assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err());565 assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err());
554 assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), dur).is_err());566 assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), dur).is_err());
555 567
556- // Async: window plus a per-student duration.568+ // Async: window plus a per-student duration, which must fit inside it
557- assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), dur).is_ok());569+ // (equal to the window is fine; the window `at(1), at(2)` above is
570+ // too narrow for `dur`, so give it one exactly `dur` wide instead).
571+ assert!(ExamSchedule::try_new(mode("async"), at(1), at(1 + 90 * 60 * 1000), dur).is_ok());
572+ assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), dur).is_err());
558 assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), None).is_err());573 assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), None).is_err());
559 574
560 // Open: no window, duration optional (limited or unlimited time).575 // Open: no window, duration optional (limited or unlimited time).
src/web/courses.rsmodified+1 -1
@@ -749,7 +749,7 @@ async fn unenroll(
749 request_body = CreateExamInCourse,749 request_body = CreateExamInCourse,
750 responses(750 responses(
751 (status = 201, description = "Exam created", body = ExamResponse),751 (status = 201, description = "Exam created", body = ExamResponse),
752- (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, or times in the past)", body = ErrorResponse),752+ (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, duration exceeding the window, or times in the past)", body = ErrorResponse),
753 (status = 401, description = "Not authenticated", body = ErrorResponse),753 (status = 401, description = "Not authenticated", body = ErrorResponse),
754 (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse),754 (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse),
755 (status = 404, description = "Course not found", body = ErrorResponse),755 (status = 404, description = "Course not found", body = ErrorResponse),
src/web/exams.rsmodified+1 -1
@@ -317,7 +317,7 @@ async fn get_exam(
317 request_body = UpdateExam,317 request_body = UpdateExam,
318 responses(318 responses(
319 (status = 200, description = "Updated exam", body = ExamResponse),319 (status = 200, description = "Updated exam", body = ExamResponse),
320- (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, or newly set times in the past)", body = ErrorResponse),320+ (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, duration exceeding the window, or newly set times in the past)", body = ErrorResponse),
321 (status = 401, description = "Not authenticated", body = ErrorResponse),321 (status = 401, description = "Not authenticated", body = ErrorResponse),
322 (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse),322 (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse),
323 (status = 404, description = "Not found", body = ErrorResponse),323 (status = 404, description = "Not found", body = ErrorResponse),
tests/integration.rsmodified+29 -5
@@ -5624,6 +5624,20 @@ async fn exam_scheduling_validates_and_echoes() {
5624 assert_eq!(res.body["mode"], "async");5624 assert_eq!(res.body["mode"], "async");
5625 assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000));5625 assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000));
5626 5626
5627+ // Async: a duration exactly as long as the window is fine — only
5628+ // strictly exceeding it is rejected (see "duration exceeds window" below).
5629+ scheduled_exam(
5630+ &app,
5631+ &teacher,
5632+ &course,
5633+ json!({
5634+ "title": "exact-fit", "kind": "quiz",
5635+ "mode": "async", "starts_at": now, "ends_at": now + 3_600_000,
5636+ "duration_ms": 3_600_000,
5637+ }),
5638+ )
5639+ .await;
5640+
5627 // Unscheduled: every schedule field stays null (pre-schedule behavior),5641 // Unscheduled: every schedule field stays null (pre-schedule behavior),
5628 // and the attempt policy shows its defaults.5642 // and the attempt policy shows its defaults.
5629 let res = create_exam_with(5643 let res = create_exam_with(
@@ -5711,6 +5725,13 @@ async fn exam_scheduling_validates_and_echoes() {
5711 json!({ "title": "x", "kind": "quiz", "mode": "async",5725 json!({ "title": "x", "kind": "quiz", "mode": "async",
5712 "starts_at": now, "ends_at": now + 90_000_000, "duration_ms": 86_400_001 }),5726 "starts_at": now, "ends_at": now + 90_000_000, "duration_ms": 86_400_001 }),
5713 ),5727 ),
5728+ (
5729+ // Within the global 1min-24h bounds, but longer than its own
5730+ // window: a 90-minute duration inside a 60-minute window.
5731+ "duration exceeds window",
5732+ json!({ "title": "x", "kind": "quiz", "mode": "async",
5733+ "starts_at": now, "ends_at": now + 3_600_000, "duration_ms": 5_400_000 }),
5734+ ),
5714 (5735 (
5715 "past starts_at",5736 "past starts_at",
5716 json!({ "title": "x", "kind": "quiz", "mode": "sync",5737 json!({ "title": "x", "kind": "quiz", "mode": "sync",
@@ -6139,14 +6160,17 @@ async fn async_deadline_is_start_plus_duration_clamped_to_window() {
6139 let started = res.body["started_at"].as_i64().expect("started_at");6160 let started = res.body["started_at"].as_i64().expect("started_at");
6140 assert_eq!(res.body["deadline"].as_i64(), Some(started + 60_000));6161 assert_eq!(res.body["deadline"].as_i64(), Some(started + 60_000));
6141 6162
6142- // Tight window: `ends_at` clamps the personal deadline.6163+ // Tight window: starting partway through it still lets `ends_at` clamp
6143- let ends = now + 30_000;6164+ // the personal deadline (duration_ms can be at most the full window now,
6165+ // so the clamp only bites once a student starts late inside it).
6166+ let starts = now - 40_000;
6167+ let ends = now + 20_000;
6144 let exam = scheduled_exam(6168 let exam = scheduled_exam(
6145 &app,6169 &app,
6146 &teacher,6170 &teacher,
6147 &course,6171 &course,
6148 json!({ "title": "quiz2", "kind": "quiz",6172 json!({ "title": "quiz2", "kind": "quiz",
6149- "mode": "async", "starts_at": now - 1_000, "ends_at": ends,6173+ "mode": "async", "starts_at": starts, "ends_at": ends,
6150 "duration_ms": 60_000 }),6174 "duration_ms": 60_000 }),
6151 )6175 )
6152 .await;6176 .await;
@@ -6393,7 +6417,7 @@ async fn mode_freezes_after_attempts_but_times_extend_live() {
6393 "PATCH",6417 "PATCH",
6394 &format!("/exams/{exam}"),6418 &format!("/exams/{exam}"),
6395 Some(&teacher),6419 Some(&teacher),
6396- Some(json!({ "mode": "async", "duration_ms": 3_600_000 })),6420+ Some(json!({ "mode": "async", "duration_ms": 60_000 })),
6397 )6421 )
6398 .await;6422 .await;
6399 assert_eq!(res.status, StatusCode::OK, "{}", res.body);6423 assert_eq!(res.status, StatusCode::OK, "{}", res.body);
@@ -6446,7 +6470,7 @@ async fn mode_freezes_after_attempts_but_times_extend_live() {
6446 "PATCH",6470 "PATCH",
6447 &format!("/exams/{exam}"),6471 &format!("/exams/{exam}"),
6448 Some(&teacher),6472 Some(&teacher),
6449- Some(json!({ "mode": "async", "duration_ms": 3_600_000 })),6473+ Some(json!({ "mode": "async", "duration_ms": 60_000 })),
6450 )6474 )
6451 .await;6475 .await;
6452 assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);6476 assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);