fix(mapping): prevent historic unauthenticated drafts being claimed by a signed user
davoodmehraban89 committed
Oct 30, 2026 at 23:05 UTC
47f2b338bd7125e5ce0028d97c8a44fe1e386bec
1 file changed
+19
-8
backend/src/aram_backend/authored_mapping_draft.py
+19
-8
@@ -143,8 +143,23 @@ def stage_authored_unreviewed_batch(
143
session, principal=principal, year_id=snapshot.year_id,
144
required_role="mapping_proposer",
145
)
146
- # The legacy saver intentionally remains an internal API; an existing
147
- # legacy draft is not assigned an author retroactively.
146
+ # Refuse retroactive authorship on legacy proposals. A draft may have
147
+ # existed before the authenticated staging request, so identifying its ID
148
+ # *after* the saver would allow a caller to take ownership of history.
149
+ existing = session.execute(select(mapping_review_drafts.c.id).where(
150
+ mapping_review_drafts.c.year_id == snapshot.year_id,
151
+ mapping_review_drafts.c.request_key == request_key,
152
+ )).scalar_one_or_none()
153
+ if existing is not None:
154
+ previous_author = get_draft_author(
155
+ session, draft_id=existing, year_id=snapshot.year_id,
156
+ )
157
+ if (previous_author.issuer, previous_author.subject) != (
158
+ principal.issuer, principal.subject,
159
+ ):
160
+ raise MappingAuthorConflict(
161
+ "Mapping draft request key belongs to another author"
162
+ )
163
saved = save_unreviewed_batch(session, snapshot, request_key=request_key)
164
values = _values(
165
draft_id=saved.id, year_id=snapshot.year_id,
@@ -164,12 +179,8 @@ def stage_authored_unreviewed_batch(
179
):
180
raise MappingAuthorConflict("Mapping draft request key belongs to another author")
181
return saved
167
- # No old author evidence means this is either a genuinely new staged
168
- # draft or a legacy unbound request. The latter must NEVER be adopted.
169
- # Inspect a freshly created draft's system event in this transaction:
170
- # the caller's request key may have existed before this call. Passing
171
- # only the current saved ID is insufficient; detect preexistence BEFORE
172
- # the legacy saver in the next revision.
182
+ # The request-key lookup above proved this is a new record; do not
183
+ # retrofit authenticated authorship onto legacy unbound drafts.
184
_insert_author_evidence(session, values)
185
return saved
186