From b1da724d0d58b8cd049be729944f419893bdf37f Mon Sep 17 00:00:00 2001 From: rabess <26330469+rabesss@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:44:12 +0530 Subject: [PATCH 1/2] fix(instructor): fail closed on restructured feedback Html readback Parse the readback Html with html.parser and accept only the structure the command sent: one attribute-free
holding text and attribute-free
line breaks. Whitespace outside the paragraph, a line break next to a tag,
entity spelling, tag case and
spelling are tolerated; attributes such as
hidden/style/class, other elements, comments, a second paragraph or any other
raw line break now report unknown instead of saved-draft.
Refs #91
---
docs/assignment-feedback.md | 5 +-
lighthouse_cli/assignment_feedback.py | 92 ++++++++++++++++++++++++---
tests/test_assignment_feedback.py | 91 +++++++++++++++++++++++++-
3 files changed, 175 insertions(+), 13 deletions(-)
diff --git a/docs/assignment-feedback.md b/docs/assignment-feedback.md
index 2c211f4..a06cc5a 100644
--- a/docs/assignment-feedback.md
+++ b/docs/assignment-feedback.md
@@ -16,7 +16,7 @@ LE 1.93) and the RichText definition in `basic/conventions.html`:
| Preflight | `GET .../dropbox/folders/{folder}/submissions/user/{user}` (EntityDropbox, 1.88+) | Exactly one block for this user (`EntityType` User, integer `EntityId`); at least one submission; a `Feedback` key present (null or an object); `Status` 1 (Submitted) or 2 (Draft) as an integer or digit string, never 3 (Published), 0 or unknown; existing feedback must be a complete DropboxFeedbackOut with `IsGraded` exactly `false`, no `Score` or `GradedSymbol`, and empty rubric assessments, files and links; existing draft text, or an existing feedback object whose comment is null or empty (`{}`), only with `--replace-draft` |
| Re-check | Both reads again, after an interactive confirmation | A digest of the record's status and existing feedback must match the first preflight; otherwise nothing is sent |
| Save | `POST .../feedback/user/{user}`, one attempt, CSRF header fetched before the preflight | Body `{"Score": null, "Feedback": {"Text": ..., "Html": ...}, "RubricAssessments": [], "IsGraded": false, "GradedSymbol": null}`. The reference marks `Feedback` as RichText. Only HTTP 200, the documented success, counts; the response body is undocumented and ignored |
-| Readback 1 | `GET .../feedback/user/{user}` (DropboxFeedbackOut) | The same draft-only checks as the preflight. Every comment part that is present, even an empty string, must match: `Text` equal to the comment (line endings normalized), `Html` equal to what was sent, or markup made only of `
` and `
` whose text is the comment (entities decoded, non-breaking spaces as spaces, outer line breaks ignored). Any other markup is a mismatch, and at least one part must be present |
+| Readback 1 | `GET .../feedback/user/{user}` (DropboxFeedbackOut) | The same draft-only checks as the preflight. Every comment part that is present, even an empty string, must match: `Text` equal to the comment (line endings normalized), `Html` equal to what was sent, or the same structure: exactly one `
` with no attributes holding only text and attribute-free `
` line breaks, whose text is the comment. Only harmless reformatting is allowed: whitespace outside the paragraph, one line break next to a `
`, `
` or `
" + html.escape(text, quote=True).replace("\n", "
") + "
]*>", re.IGNORECASE) -_CLOSE = re.compile(r"
\s*$", re.IGNORECASE) -_BREAK = re.compile(r"]*>", re.IGNORECASE) +# ASCII whitespace, the only markup-level whitespace allowed outside the paragraph. +_HTML_SPACE = " \t\n\r\f" + + +class _RejectedMarkupError(Exception): + """The readback Html is not the structure the command sent.""" + + +class _CommentMarkup(HTMLParser): + """Line segments of exactly one attribute-free ``
`` holding only text and ``
``.
+
+ Entity spelling is normalized by the parser; tag case and ``
``,
+ ``
`` or ``
`` are the same tokens. Every other tag, attribute,
+ comment, declaration, processing instruction or second paragraph raises
+ :class:`_RejectedMarkupError`.
+ """
+
+ def __init__(self) -> None:
+ super().__init__(convert_charrefs=True)
+ self.state = "before" # before -> inside -> after
+ self.segments = [""]
+
+ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
+ if attrs:
+ raise _RejectedMarkupError
+ if tag == "p" and self.state == "before":
+ self.state = "inside"
+ elif tag == "br" and self.state == "inside":
+ self.segments.append("")
+ else:
+ raise _RejectedMarkupError
+
+ def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
+ if tag != "br":
+ raise _RejectedMarkupError
+ self.handle_starttag(tag, attrs)
+
+ def handle_endtag(self, tag: str) -> None:
+ if tag != "p" or self.state != "inside":
+ raise _RejectedMarkupError
+ self.state = "after"
+
+ def handle_data(self, data: str) -> None:
+ if self.state == "inside":
+ self.segments[-1] += data
+ elif data.strip(_HTML_SPACE):
+ raise _RejectedMarkupError
+
+ def handle_comment(self, data: str) -> None:
+ raise _RejectedMarkupError
+
+ def handle_decl(self, decl: str) -> None:
+ raise _RejectedMarkupError
+
+ def handle_pi(self, data: str) -> None:
+ raise _RejectedMarkupError
+
+ def unknown_decl(self, data: str) -> None:
+ raise _RejectedMarkupError
def _comparable(text: str) -> str:
@@ -99,11 +155,31 @@ def _comparable(text: str) -> str:
def _html_text(markup: str) -> str | None:
- """The text of paragraph/line-break-only markup, or ``None`` if it has any other markup."""
- body = _BREAK.sub("\n", _CLOSE.sub("", _OPEN.sub("", _normalize(markup), count=1), count=1))
- if "<" in body:
+ """The text of markup shaped exactly like :func:`_html` output, or ``None`` for any other structure.
+
+ Allowed reformatting: whitespace outside the paragraph, one line break
+ directly after ``
``/``
`` or before ``
``/``
Synthetic draft note: revisit the second proof.
Line two.
Synthetic draft note: revisit the second proof.
\nLine two.
", - 'Synthetic draft note: revisit the second proof.
Line two.
Synthetic draft note: revisit the second proof.
Line two.
\nSynthetic draft note: revisit the second proof.
\nLine two.\n
hello
", "hello"), + "p-style": ('', "hello"), + "p-class": ('hello
', "hello"), + "p-id": ("hello
", "hello"), + "br-attribute": ('a
b
hello
", "hello"), + "nested-p-in-div": ("hello
hello
hello
", "hello"), + "script-after": ("hello
", "hello"), + "style-inside": ("hello
", "hello"), + "comment-inside": ("hello
", "hello"), + "comment-before": ("hello
", "hello"), + "unfinished-comment": ("hello
", "& literal", + "x", "
", 1) + fake = FakeLMS(happy_routes(get_feedback=[ + lambda: response(200, draft_feedback_out(Feedback={"Text": TEXT, "Html": hidden}))])) + result = invoke(fake, "--file", str(feedback_file), "--yes", "--json") + assert result.exit_code == 1 + assert json.loads(result.stdout)["outcome"] == "unknown" + assert "saved-draft" not in result.stdout + + def test_csrf_bootstrap_failure_means_nothing_was_sent(feedback_file): fake = FakeLMS(happy_routes()) From 68df29eb9f038d4ed00c068b7e8bfbddda25b918 Mon Sep 17 00:00:00 2001 From: rabess <26330469+rabesss@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:59:37 +0530 Subject: [PATCH 2/2] fix(assignment-feedback): validate raw readback Html tokens html.parser drops invalid character references and end-tag junk, and the old comparison ignored outer line breaks, so
hello
,hello
,he>llo
and
hello
` with no attributes holding only text and attribute-free `
` line breaks, whose text is the comment. Only harmless reformatting is allowed: whitespace outside the paragraph, one line break next to a `
`, `
` or `
` with no attributes holding only text and attribute-free `
` line breaks, whose text is the comment. Only harmless reformatting is allowed: whitespace outside the paragraph, one line break next to a `
`, `
` or `
" + html.escape(text, quote=True).replace("\n", "
") + "
`` holding only text and ``
``.
+ """Raw token spans of markup that may be one attribute-free ``
`` holding only ``
``.
- Entity spelling is normalized by the parser; tag case and ``
``,
- ``
`` or ``
`` are the same tokens. Every other tag, attribute,
- comment, declaration, processing instruction or second paragraph raises
- :class:`_RejectedMarkupError`.
+ The parser finds the tokens; the raw text of every tag is then checked
+ exactly, and the text between tags is sliced from the source, so a token
+ the parser drops or rewrites cannot slip through. Comments, declarations,
+ processing instructions and any other tag raise :class:`_RejectedMarkupError`.
"""
- def __init__(self) -> None:
- super().__init__(convert_charrefs=True)
- self.state = "before" # before -> inside -> after
- self.segments = [""]
+ def __init__(self, markup: str) -> None:
+ super().__init__(convert_charrefs=False)
+ self.markup = markup
+ self._line_starts = [0] + [index + 1 for index, char in enumerate(markup) if char == "\n"]
+ self.tags: list[tuple[str, int, int]] = [] # (token, start, end)
+
+ def _add(self, token: str, raw: str) -> None:
+ line, offset = self.getpos()
+ start = self._line_starts[line - 1] + offset
+ end = start + len(raw)
+ if self.markup[start:end] != raw or (self.tags and start < self.tags[-1][2]):
+ raise _RejectedMarkupError
+ self.tags.append((token, start, end))
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
- if attrs:
- raise _RejectedMarkupError
- if tag == "p" and self.state == "before":
- self.state = "inside"
- elif tag == "br" and self.state == "inside":
- self.segments.append("")
- else:
+ raw = self.get_starttag_text() or ""
+ if attrs or tag not in ("p", "br") or not _bare_tag(raw, "<", tag, slash=tag == "br"):
raise _RejectedMarkupError
+ self._add(tag, raw)
def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
if tag != "br":
@@ -127,15 +142,12 @@ def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> N
self.handle_starttag(tag, attrs)
def handle_endtag(self, tag: str) -> None:
- if tag != "p" or self.state != "inside":
- raise _RejectedMarkupError
- self.state = "after"
-
- def handle_data(self, data: str) -> None:
- if self.state == "inside":
- self.segments[-1] += data
- elif data.strip(_HTML_SPACE):
+ line, offset = self.getpos()
+ start = self._line_starts[line - 1] + offset
+ raw = self.markup[start:self.markup.find(">", start) + 1]
+ if tag != "p" or not _bare_tag(raw, "", tag):
raise _RejectedMarkupError
+ self._add("/p", raw)
def handle_comment(self, data: str) -> None:
raise _RejectedMarkupError
@@ -150,8 +162,56 @@ def unknown_decl(self, data: str) -> None:
raise _RejectedMarkupError
+def _character(reference: str) -> str | None:
+ """The character a complete ``&...;`` reference renders as, or ``None`` if it is not a plain, valid one."""
+ body = reference[1:-1]
+ if not body.startswith("#"):
+ return html.entities.html5.get(body + ";")
+ digits, base = (body[2:], 16) if body[1:2] in ("x", "X") else (body[1:], 10)
+ allowed = "0123456789abcdefABCDEF" if base == 16 else "0123456789"
+ if not 0 < len(digits) <= 8 or any(char not in allowed for char in digits):
+ return None
+ number = int(digits, base)
+ if number == 9 or 0x20 <= number < 0x7F or (
+ 0xA0 <= number <= 0x10FFFF and not 0xD800 <= number <= 0xDFFF
+ and not 0xFDD0 <= number <= 0xFDEF and number & 0xFFFE != 0xFFFE
+ ):
+ return chr(number)
+ return None
+
+
+def _decode_text(raw: str) -> str | None:
+ """Text content of raw paragraph text, or ``None`` for markup or an unclear reference.
+
+ A ``<`` that would open a tag, comment or declaration is refused; a ``<``
+ or ``&`` that browsers show as-is stays literal. A reference must be
+ complete (with ``;``) and name a valid, printable character.
+ """
+ out: list[str] = []
+ index = 0
+ while index < len(raw):
+ char, following = raw[index], raw[index + 1:index + 2]
+ if char == "<" and (following == "" or (following.isascii() and (following.isalpha() or following in "/!?"))):
+ return None
+ if char != "&" or not (following == "#" or (following.isascii() and following.isalnum())):
+ out.append(char)
+ index += 1
+ continue
+ end = index + 1
+ while end < len(raw) and raw[end].isascii() and (raw[end].isalnum() or (raw[end] == "#" and end == index + 1)):
+ end += 1
+ if raw[end:end + 1] != ";":
+ return None
+ value = _character(raw[index:end + 1])
+ if value is None:
+ return None
+ out.append(value)
+ index = end + 1
+ return "".join(out)
+
+
def _comparable(text: str) -> str:
- return text.replace("\xa0", " ").strip("\n")
+ return text.replace("\xa0", " ")
def _html_text(markup: str) -> str | None:
@@ -160,25 +220,28 @@ def _html_text(markup: str) -> str | None:
Allowed reformatting: whitespace outside the paragraph, one line break
directly after ``
``/``
`` or before ``
``/``
hello
"}, "hello\n"), ({"Text": None, "Html": "a & b
c
a b
"}, "a\u00a0b"), -], ids=["trailing-newline-dropped-in-html", "html-only-escaped", "nbsp"]) +], ids=["html-only-escaped", "nbsp"]) def test_saved_comment_check_accepts_equivalent_parts(rich, text): _check_saved_comment(draft_feedback_out(Feedback=rich), text) @@ -781,6 +780,27 @@ def test_saved_comment_check_accepts_equivalent_parts(rich, text): "entity-after": ("hello
", "hello"), "raw-newline-inside": ("x\ny
", "x\ny"), "two-newlines-after-br": ("x
\n\ny
hello
", "hello\n"), + "extra-outer-breaks": ("
hello
hello
hello
", "hello"), + "c1-charref": ("hello
", "he\x80llo"), + "zero-charref": ("hello
", "hello"), + "huge-charref": ("hello
", "hello"), + "surrogate-charref": ("hello
", "hello"), + "newline-charref": ("a b
", "a\nb"), + "unterminated-charref": ("& x
", "& x"), + "unknown-entity": ("a&bogus;b
", "ab"), + "unterminated-entity": ("a& b
", "a& b"), + "end-tag-attribute": ("hello
", "hello"), + "end-tag-junk": ("hello
", "hello"), + "end-tag-slash": ("hello
", "hello"), + "empty-end-tag": ("he>llo
", "hello"), + "empty-end-tag-after": ("hello
>", "hello"), + "slash-attribute-p": ("hello
", "hello"), + "slash-junk-br": ("a
b
x", "x"), + "vertical-tab-in-tag": ("
hello
", "hello"), } @@ -802,7 +822,8 @@ def test_saved_comment_check_rejects_html_with_a_different_structure(markup, tex "&&& \"\"\" ''' >>> <3"), "raw-lt-text": ("x < y <3
", "x < y <3"), "nbsp": ("a b c
", "a b c"), - "trailing-break-dropped": ("hello
", "hello\n"), + "bare-ampersand": ("a & b &; x&
", "a & b &; x&"), + "outer-breaks-kept": ("
hello