Skip to content

Commit fcea1eb

Browse files
authored
Merge pull request #35 from edx/ashultz0/require-staff-auth
feat: require staff identity to call the summary handler
2 parents b25c504 + d4f9ccf commit fcea1eb

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

CHANGELOG.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ Change Log
1414
Unreleased
1515
**********
1616

17-
feat: [ACADEMIC-16207] Added models to summaryhook_aside (Has migrations)
1817

19-
feat: [ACADEMIC-16177] Catch exceptions in a couple of locations so the aside cannot crash content.
18+
3.0.0 – 2023-07-16
19+
**********************************************
20+
21+
Features
22+
=========
23+
* Summary content handler now requires a staff user identity, otherwise returns 403. This is a breaking change.
24+
* Added models to summaryhook_aside (Has migrations)
25+
* Catch exceptions in a couple of locations so the aside cannot crash content.
2026

2127
2.0.2 – 2023-07-05
2228
**********************************************

ai_aside/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
A plugin containing xblocks and apps supporting GPT and other LLM use on edX.
33
"""
44

5-
__version__ = '2.0.2'
5+
__version__ = '3.0.0'

ai_aside/block.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def _format_date(date):
4343
return date.strftime('%Y-%m-%d %H:%M:%S') if isinstance(date, datetime) else None
4444

4545

46+
def _staff_user(block):
47+
return getattr(block.runtime, 'user_is_staff', False)
48+
49+
4650
def _render_summary(context):
4751
template = Template(summary_fragment)
4852
return template.render(Context(context))
@@ -135,7 +139,13 @@ class SummaryHookAside(XBlockAside):
135139
def summary_handler(self, request=None, suffix=None): # pylint: disable=unused-argument
136140
"""
137141
Extract and return summarizable text from unit children.
142+
143+
Only services and staff users are allowed to fetch summary text, everyone else
144+
gets an unhelpful 403.
138145
"""
146+
if not _staff_user(self):
147+
return Response(status=403)
148+
139149
block = get_block(self.scope_ids.usage_id.usage_key)
140150
valid = self.should_apply_to_block(block)
141151

@@ -199,14 +209,7 @@ def _student_view_can_throw(self, block):
199209
if length < settings.SUMMARY_HOOK_MIN_SIZE:
200210
return fragment
201211

202-
# thirdparty=true connects to the unauthenticated handler for now,
203-
# we will secure it in ACADEMIC-16187
204-
handler_url = self.runtime.handler_url(self, 'summary_handler', thirdparty=True)
205-
206-
# enable ai-spot to see the LMS when they are installed together in devstack
207-
aispot_lms_name = settings.AISPOT_LMS_NAME
208-
if aispot_lms_name != '':
209-
handler_url = handler_url.replace('localhost', aispot_lms_name)
212+
handler_url = self._summary_handler_url()
210213

211214
fragment.add_content(
212215
_render_summary(
@@ -221,6 +224,24 @@ def _student_view_can_throw(self, block):
221224
)
222225
return fragment
223226

227+
def _summary_handler_url(self):
228+
"""
229+
Generate the summary handler URL for this block.
230+
231+
A separate function to handle overrides required
232+
for the unusual use of the handler (non-edx codebase edx service)
233+
and to override the URL for use in devstack.
234+
"""
235+
# thirdparty=true gives the full host name and unauthenticated handler
236+
handler_url = self.runtime.handler_url(self, 'summary_handler', thirdparty=True)
237+
# but we want the authenticated handler
238+
handler_url = handler_url.replace('handler_noauth', 'handler')
239+
# enable ai-spot to see the LMS when they are installed together in devstack
240+
aispot_lms_name = settings.AISPOT_LMS_NAME
241+
if aispot_lms_name != '':
242+
handler_url = handler_url.replace('localhost', aispot_lms_name)
243+
return handler_url
244+
224245
@classmethod
225246
def should_apply_to_block(cls, block):
226247
"""
@@ -245,7 +266,6 @@ def _should_apply_can_throw(cls, block):
245266
if getattr(block, 'category', None) != 'vertical':
246267
return False
247268
course_key = block.scope_ids.usage_id.course_key
248-
if (getattr(block.runtime, 'user_is_staff', False)
249-
and summary_staff_only(course_key)):
269+
if _staff_user(block) and summary_staff_only(course_key):
250270
return True
251271
return summary_enabled(course_key)

0 commit comments

Comments
 (0)