JCU/perf(api): stop asking the API questions an anonymous user cannot answer - #1437
Open
Kasinhou wants to merge 1 commit into
Open
JCU/perf(api): stop asking the API questions an anonymous user cannot answer#1437Kasinhou wants to merge 1 commit into
Kasinhou wants to merge 1 commit into
Conversation
… answer
An anonymous visitor produced 4 failed REST requests on every page and 6 on an
item page. The frontend fires them as feature detection, then discards the
error - nothing is broken, but every page view writes 401s into the backend log
and errors into the browser console, and each one is a round trip nobody needs.
Five call sites asked first and checked the permission afterwards; they now
check first:
export.menu / import.menu /api/system/scripts/metadata-{export,import}
-> only when the user is a site administrator
create-report.menu /api/config/properties/contentreport.enable
-> only when the user is a site administrator
withdrawn-reinstate-item.menu /api/config/correctiontypes/search/findByItem
-> only when the user is logged in
qa-event-notification /api/integration/qualityassurancesources/search/byTarget
-> only when the user may see QA events (canSeeQA)
notify-requests-status /api/ldn/notifyrequests/<uuid>
-> only when COAR Notify is enabled for the user
The authorizations these now depend on are already fetched for other reasons, so
no request is added. Behaviour for a user who does have the permission is
unchanged.
The remaining 404s (google.analytics.key, bulkedit.export.max.items,
authentication-password.domain.valid) come from properties that are unset or not
exposed in the backend and belong in local.cfg, not here.
Fixes the frontend part of L3 from dataquest-dev/dspace-customers#853.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Author
|
will not do it for now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the frontend part of L3 from the dataquest-dev/dspace-customers#853 review.
Reproduced
Anonymous, cold cache, local Docker DSpace 9.3 with the JCU configuration. Every request the review
lists is real, plus one it does not (
google.analytics.key):/api/system/scripts/metadata-export/api/system/scripts/metadata-import/api/config/properties/contentreport.enable/api/config/properties/google.analytics.key/api/config/correctiontypes/search/findByItem/api/integration/qualityassurancesources/search/byTarget/api/ldn/notifyrequests/<uuid>/search/api/config/properties/bulkedit.export.max.items/register/api/config/properties/authentication-password.domain.validThat is 4 failed requests on the home page and 6 on an item page, for every visitor, on every
page view.
Two different causes, and the review's remedy only fits one of them
The review proposes adding the properties to
rest.properties.exposed. That is right for exactly oneof the nine — and the split matters, because it decides which repository the fix belongs in.
The 401s are a frontend problem. Five call sites request something, then check whether the user
was allowed to ask. The permission they check is already known — in every case the corresponding
/authz/authorizations/search/object?feature=…request is fired anyway, for other reasons, beforethese calls go out. So we already had the answer and asked anyway.
ScriptDataServiceeven documentsit: "user needs to be allowed to execute script for this to not throw a 401 Unauthorized".
The 404s are backend configuration, and not the way the review describes:
All four of
contentreport.enable,bulkedit.export.max.items,authentication-password.domain.validandgoogle.analytics.keyare already listed inrest.properties.exposedinmodules/rest.cfg.ConfigurationRestRepository.findOne()throwsResourceNotFoundExceptionwhen the property is not exposed or when!configurationService.hasProperty(property)— and DSpace ships all four commented out. Exposingthem changes nothing; they have to be set. (
google.analytics.keyis a fifth 404 the reviewmissed.)
Those live in
local.cfgon the server, not in this repository, so they are not in this PR. Theexact lines are at the bottom, together with the one that needs a decision from JCU
(
authentication-password.domain.valid— whether registration should be restricted to@jcu.cz).What this PR changes
export.menu.tscombineLatest([isAdmin$, scriptExists$])AdministratorOfimport.menu.tscreate-report.menu.tscombineLatest([contentreportEnabled$, isAdmin$]), duplicated in both methodsisReportMenuAvailable(), config read only whenAdministratorOfwithdrawn-reinstate-item.menu.tsfindByItem()on every item pageisAuthenticated()— you cannot request a withdrawal while logged outqa-event-notification.component.tsgetSourcesByTarget()on every item pageCanSeeQAnotify-requests-status.component.tsgetNotifyRequestsStatus()on every item pageisCoarConfigEnabled()(CoarNotifyEnabled)Nothing is hidden that was visible before: in each case the item was already
visible: falsefor auser without the permission — the fix only stops asking. And no request is added: every
authorization used as the gate was already being fetched.
Two incidental notes:
notify-requests-status.component.tshad itsNotifyRequestsStatusDataServicefield namednotifyInfoService, which is the name of a different service that I now inject next to it.Renamed the field to
notifyRequestsStatusDataService.qa-event-notification.component.tshadcatchError(() => []). RxJS treats a bare array as asource that emits each element, so an empty array emits nothing — the intent was clearly
of([]), which is what it says now. No visible difference (the template renders nothing eitherway), but it is what the code meant.
Verified
Same measurement, same stack, on this branch:
The one remaining is
google.analytics.key, which is the backend config item.Logged in as a site administrator the three admin requests fire again, exactly as before, and the
menus still build — sidebar → Export → Metadata, Batch Export (ZIP).
call sites.
src/app/shared/menu/**+src/app/item-page/**: 692 passing, 11 failing — all 11in
edit-relationship-list.component.spec.ts, which fails identically oncustomer/jcuwithoutthis branch (verified by stashing).
npm run lint→ 0 errors.Still to do in
local.cfg(server side, not this repo) — already verifiedApplied to the local Docker stack, this turns all four 404s into 200s with nothing else regressing,
and with this PR an anonymous visitor then generates zero 4xx REST responses on
/home, an itempage,
/searchand/register.Full write-up, including the H3 name/e-mail change and the OAI cache purge it requires, is in
C:\Users\MatusKasak\jcu-853-H3-L3-local-cfg-patch.md.Upstream and other customers
All six files are byte-for-byte identical to
upstream/dspace-9_x, andupstream/mainstill hasthe same eager
combineLatest([permission$, request$])shape — so this is upstream behaviour, not aJCU regression, and every DSpace 7/8/9/10 instance does it. There is no upstream issue for it
(searched "401 anonymous requests", "unnecessary requests anonymous", "contentreport.enable"). The
change is small and generic and is worth offering upstream; it is a straight win for any repository
whose traffic is mostly anonymous, which is all of them.
None of our other customer branches has it either.
Evidence
Screenshots in
C:\Users\MatusKasak\jcu-853-evidence\(to be attached here):L3-1-before-anonymous-401-404-requests.pngL3-2-after-anonymous-calls-gone.pngL3-3-after-local-item-page-unchanged.pngL3-4-after-local-admin-export-menu-still-works.png