Skip to content

Commit 09f99de

Browse files
authored
Merge pull request #7708 from freedomofpress/7619-fix-tabid
Add check for valid tab IDs when creating sources
2 parents d01008b + ff271d0 commit 09f99de

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

securedrop/source_app/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,19 @@ def create() -> werkzeug.Response:
108108
if not date_codenames_expire or datetime.now(timezone.utc) >= date_codenames_expire:
109109
return clear_session_and_redirect_to_logged_out_page(flask_session=session)
110110

111-
tab_id = request.form["tab_id"]
111+
tab_id = request.form.get("tab_id")
112+
if not tab_id or tab_id not in session.get("codenames", {}):
113+
# Use generic error message text for source creation issue
114+
session.clear()
115+
flash_msg(
116+
"error",
117+
None,
118+
gettext(
119+
"There was a temporary problem creating your account. Please try again."
120+
),
121+
)
122+
return redirect(url_for("main.index"))
123+
112124
codename = session["codenames"][tab_id]
113125
del session["codenames"]
114126

securedrop/tests/test_source.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ def test_create_new_source(source_app):
145145
assert "codenames" not in session
146146

147147

148+
def test_create_no_tab_id(source_app):
149+
with source_app.test_client() as app:
150+
resp = app.post(url_for("main.generate"), data=GENERATE_DATA)
151+
assert resp.status_code == 200
152+
resp = app.post(url_for("main.create"), follow_redirects=True)
153+
assert not SessionManager.is_user_logged_in(db_session=db.session)
154+
155+
# should be redirected to /lookup
156+
text = resp.data.decode("utf-8")
157+
assert "There was a temporary problem" in text
158+
assert "Get Started" in text
159+
assert "codenames" not in session
160+
161+
162+
def test_create_bad_tab_id(source_app):
163+
with source_app.test_client() as app:
164+
resp = app.post(url_for("main.generate"), data=GENERATE_DATA)
165+
assert resp.status_code == 200
166+
resp = app.post(url_for("main.create"), data={"tab_id": "ohno"}, follow_redirects=True)
167+
assert not SessionManager.is_user_logged_in(db_session=db.session)
168+
169+
# should be redirected to /lookup
170+
text = resp.data.decode("utf-8")
171+
assert "There was a temporary problem" in text
172+
assert "Get Started" in text
173+
assert "codenames" not in session
174+
175+
148176
def test_generate_as_post(source_app):
149177
with source_app.test_client() as app:
150178
resp = app.post(url_for("main.generate"), data=GENERATE_DATA)

0 commit comments

Comments
 (0)