diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fd5ac432593..b87752acf8c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -49,7 +49,7 @@ jobs: strategy: # Create a matrix of Node versions to test against (in parallel) matrix: - node-version: [18.x, 20.x] + node-version: [20.x, 22.x] # Do NOT exit immediately if one matrix job fails fail-fast: false # These are the actual CI steps to perform per job @@ -97,7 +97,7 @@ jobs: # NOTE: Angular CLI only supports code coverage for specs. See https://github.com/angular/angular-cli/issues/6286 - name: Upload code coverage report to Artifact uses: actions/upload-artifact@v4 - if: matrix.node-version == '18.x' + if: matrix.node-version == '22.x' with: name: coverage-report-${{ matrix.node-version }} path: 'coverage/dspace-angular/lcov.info' @@ -288,6 +288,21 @@ jobs: - name: Stop running app run: kill -9 $(lsof -t -i:4000) + # If any prior step errors out, export the backend logs for help with debugging issues + - name: Export backend logs (on failure only) + if: failure() + run: | + docker compose -f ./docker/docker-compose-ci.yml logs > backend-logs-${{ matrix.node-version }}.txt + + # If any prior step errors out, upload backend logs to artifact + - name: Upload backend logs to artifact (on failure only) + uses: actions/upload-artifact@v4 + if: failure() + with: + name: backend-logs-${{ matrix.node-version }} + path: backend-logs-${{ matrix.node-version }}.txt + retention-days: 14 + - name: Shutdown Docker containers run: docker compose -f ./docker/docker-compose-ci.yml down diff --git a/.github/workflows/playwright-tests.yml b/.github/workflows/playwright-tests.yml index c65a48b4d2d..2d7208efefc 100644 --- a/.github/workflows/playwright-tests.yml +++ b/.github/workflows/playwright-tests.yml @@ -7,7 +7,7 @@ on: jobs: playwright-tests: runs-on: dspace-test-1 - timeout-minutes: 45 + timeout-minutes: 60 steps: - name: Checkout Playwright tests uses: actions/checkout@v4 @@ -20,7 +20,7 @@ jobs: working-directory: dspace-ui-tests/scripts env: HOME_URL: https://dev-5.pc:8443/repository/ - NAME: DEFAULT + NAME: LINDAT PASSWORD_ADMIN: ${{ secrets.DSPACE_ADMIN_PASSWORD }} run: | ./test.sh diff --git a/Dockerfile b/Dockerfile index e1e72cbf43f..563bcffbc92 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # This image will be published as dspace/dspace-angular # See https://github.com/DSpace/dspace-angular/tree/main/docker for usage details -FROM docker.io/node:18-alpine +FROM docker.io/node:22-alpine # Ensure Python and other build tools are available # These are needed to install some node modules, especially on linux/arm64 diff --git a/Dockerfile.dist b/Dockerfile.dist index 4c1fc659f4d..f9ccaf5e874 100644 --- a/Dockerfile.dist +++ b/Dockerfile.dist @@ -4,7 +4,7 @@ # Test build: # docker build -f Dockerfile.dist -t dspace/dspace-angular:dspace-7_x-dist . -FROM docker.io/node:18-alpine AS build +FROM docker.io/node:22-alpine AS build # Ensure Python and other build tools are available # These are needed to install some node modules, especially on linux/arm64 @@ -20,7 +20,7 @@ ADD . /app/ ENV NODE_OPTIONS="--max_old_space_size=4096" RUN yarn build:prod -FROM node:18-alpine +FROM node:22-alpine RUN npm install --global pm2 COPY --chown=node:node --from=build /app/dist /app/dist diff --git a/docker/matomo-w-db.yml b/docker/matomo-w-db.yml index 1e7c23f6c94..eca828e8c49 100644 --- a/docker/matomo-w-db.yml +++ b/docker/matomo-w-db.yml @@ -11,7 +11,7 @@ services: MARIADB_INITDB_SKIP_TZINFO: ${MATOMO_MARIADB_INITDB_SKIP_TZINFO:-1} gui: - image: phpmyadmin/phpmyadmin + image: phpmyadmin ports: - 8148:80 container_name: matomo_phpAdmin diff --git a/server.ts b/server.ts index d92310746e9..a2f2d75aa7a 100644 --- a/server.ts +++ b/server.ts @@ -55,6 +55,7 @@ import { APP_CONFIG, AppConfig } from './src/config/app-config.interface'; import { extendEnvironmentWithAppConfig } from './src/config/config.util'; import { logStartupMessage } from './startup-message'; import { TOKENITEM } from './src/app/core/auth/models/auth-token-info.model'; +import { getSsrCacheKey } from './src/app/shared/ssr-cache-key.util'; import { SsrExcludePatterns } from './src/config/universal-config.interface'; @@ -383,14 +384,14 @@ function anonymousCacheEnabled(): boolean { /** * Check if the currently requested page is in our server-side, in-memory cache. - * Caching is ONLY done for SSR requests. Pages are cached base on their path (e.g. /home or /search?query=test) + * Caching is ONLY done for SSR requests. Pages are cached by path, origin and language (see getSsrCacheKey) */ function cacheCheck(req, res, next) { // Cached copy of page (if found) let cachedCopy; - // If the bot cache is enabled and this request looks like a bot, check the bot cache for a cached page. - if (botCacheEnabled() && isbot(req.get('user-agent'))) { + // If the bot cache is enabled and this request looks like an anonymous bot, check the bot cache for a cached page. + if (botCacheEnabled() && isbot(req.get('user-agent')) && !isUserAuthenticated(req)) { cachedCopy = checkCacheForRequest('bot', botCache, req, res); } else if (anonymousCacheEnabled() && !isUserAuthenticated(req)) { cachedCopy = checkCacheForRequest('anonymous', anonymousCache, req, res); @@ -433,7 +434,7 @@ function cacheCheck(req, res, next) { */ function checkCacheForRequest(cacheName: string, cache: LRU, req, res): any { // Get the cache key for this request - const key = getCacheKey(req); + const key = getSsrCacheKey(req); // Check if this page is in our cache let cachedCopy = cache.get(key); @@ -457,18 +458,6 @@ function checkCacheForRequest(cacheName: string, cache: LRU, req, r return cachedCopy; } -/** - * Create a cache key from the current request. - * The cache key is the URL path (NOTE: this key will also include any querystring params). - * E.g. "/home" or "/search?query=test" - * @param req current request - * @returns cache key to use for this page - */ -function getCacheKey(req): string { - // NOTE: this will return the URL path *without* any baseUrl - return req.url; -} - /** * Save page to server side cache(s), if enabled. If caching is not enabled or a user is authenticated, this is a noop * If multiple caches are enabled, the page will be saved to any caches where it does not yet exist (or is expired). @@ -481,9 +470,9 @@ function saveToCache(req, page: any) { // NOTE: It's not safe to save page data to the cache when a user is authenticated. In that situation, // the page may include sensitive or user-specific materials. As the cache is shared across all users, it can only contain public info. if (!isUserAuthenticated(req)) { - const key = getCacheKey(req); + const key = getSsrCacheKey(req); // Avoid caching "/reload/[random]" paths (these are hard refreshes after logout) - if (key.startsWith('/reload')) { return; } + if (req.url.startsWith('/reload')) { return; } // Avoid caching not successful responses (status code different from 2XX status) if (hasNotSucceeded(req.res.statusCode)) { return; } diff --git a/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.spec.ts b/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.spec.ts index 6dc6805db49..0f323e7eae0 100644 --- a/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.spec.ts +++ b/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.spec.ts @@ -37,7 +37,7 @@ describe('ClarinBitstreamDownloadPageComponent', () => { beforeEach(async () => { authService = jasmine.createSpyObj('authService', { - isAuthenticated: observableOf(true), + isAuthenticated: observableOf(false), setRedirectUrl: {} }); authorizationService = jasmine.createSpyObj('authorizationSerivice', { @@ -70,7 +70,7 @@ describe('ClarinBitstreamDownloadPageComponent', () => { }; router = jasmine.createSpyObj('router', ['navigateByUrl']); requestService = getMockRequestService(); - halService = Object.assign(new HALEndpointServiceStub(url)); + halService = Object.assign(new HALEndpointServiceStub(url), { getRootHref: () => url }) as any; rdbService = getMockRemoteDataBuildService(); await TestBed.configureTestingModule({ @@ -100,4 +100,8 @@ describe('ClarinBitstreamDownloadPageComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + it('should redirect an anonymous user to the bitstream content through HardRedirectService', () => { + expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link'); + }); }); diff --git a/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.ts b/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.ts index 5a4bed21ac6..c36a7515ebe 100644 --- a/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.ts +++ b/src/app/bitstream-page/clarin-bitstream-download-page/clarin-bitstream-download-page.component.ts @@ -132,7 +132,9 @@ export class ClarinBitstreamDownloadPageComponent implements OnInit { window.location.replace(fileLink); } else if ((isAuthorized || isAuthorizedByClarin) && !isLoggedIn) { this.downloadStatus.next(RequestEntryState.Success); - window.location.replace(bitstreamURL); + // Server-side too, so crawlers get a 302. Logged-in users keep the client-side + // redirect: a server 302 right after login left the browser on the login page. + this.hardRedirectService.redirect(bitstreamURL); } else if (!(isAuthorized || isAuthorizedByClarin) && isLoggedIn && this.downloadStatus.value === RequestEntryState.Error) { // this.downloadStatus is `ERROR` - no CLARIN exception is thrown up diff --git a/src/app/bitstream-page/clarin-zip-download-page/clarin-zip-download-page.component.ts b/src/app/bitstream-page/clarin-zip-download-page/clarin-zip-download-page.component.ts index e0f13d16150..51b66672497 100644 --- a/src/app/bitstream-page/clarin-zip-download-page/clarin-zip-download-page.component.ts +++ b/src/app/bitstream-page/clarin-zip-download-page/clarin-zip-download-page.component.ts @@ -20,6 +20,7 @@ import { BitstreamDataService } from '../../core/data/bitstream-data.service'; import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; import { NotificationsService } from '../../shared/notifications/notifications.service'; import { TranslateService } from '@ngx-translate/core'; +import { MAX_PAGE_SIZE } from '../../core/data/find-list-options.model'; /** * Fetch ZIP file from the server as a single file into `bitstreamRD$` property which is extended and then call @@ -59,7 +60,7 @@ export class ClarinZipDownloadPageComponent extends ClarinBitstreamDownloadPageC this.itemRD$.subscribe((itemRD: RemoteData) => { this.bitstreamDataService.findAllByItemAndBundleName(itemRD?.payload, 'ORIGINAL', { currentPage: 1, - elementsPerPage: 9999 + elementsPerPage: MAX_PAGE_SIZE }).pipe( getFirstCompletedRemoteData(), ).subscribe((bitstreamsRD: RemoteData>) => { diff --git a/src/app/clarin-licenses/clarin-all-licenses-page/clarin-all-licenses-page.component.ts b/src/app/clarin-licenses/clarin-all-licenses-page/clarin-all-licenses-page.component.ts index b122dc16f82..1b81ece6473 100644 --- a/src/app/clarin-licenses/clarin-all-licenses-page/clarin-all-licenses-page.component.ts +++ b/src/app/clarin-licenses/clarin-all-licenses-page/clarin-all-licenses-page.component.ts @@ -3,7 +3,7 @@ import { BehaviorSubject } from 'rxjs'; import { ClarinLicense } from '../../core/shared/clarin/clarin-license.model'; import { ClarinLicenseDataService } from '../../core/data/clarin/clarin-license-data.service'; import { getFirstSucceededRemoteListPayload } from '../../core/shared/operators'; -import { FindListOptions } from '../../core/data/find-list-options.model'; +import { FindListOptions, MAX_PAGE_SIZE } from '../../core/data/find-list-options.model'; import { ClarinLicenseRequiredInfo } from '../../core/shared/clarin/clarin-license.resource-type'; import { ClarinLicenseRequiredInfoSerializer } from '../../core/shared/clarin/clarin-license-required-info-serializer'; @@ -36,7 +36,7 @@ export class ClarinAllLicensesPageComponent implements OnInit { const options = new FindListOptions(); options.currentPage = 0; // Load all licenses - options.elementsPerPage = 1000; + options.elementsPerPage = MAX_PAGE_SIZE; return this.clarinLicenseService.findAll(options, false) .pipe(getFirstSucceededRemoteListPayload()) .subscribe(res => { diff --git a/src/app/core/auth/auth.service.spec.ts b/src/app/core/auth/auth.service.spec.ts index 32ceee35d65..27aab85ca7a 100644 --- a/src/app/core/auth/auth.service.spec.ts +++ b/src/app/core/auth/auth.service.spec.ts @@ -13,7 +13,7 @@ import { CookieService } from '../services/cookie.service'; import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service.stub'; import { AuthRequestService } from './auth-request.service'; import { AuthStatus } from './models/auth-status.model'; -import { AuthTokenInfo } from './models/auth-token-info.model'; +import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model'; import { EPerson } from '../eperson/models/eperson.model'; import { EPersonMock } from '../../shared/testing/eperson.mock'; import { AppState } from '../../app.reducer'; @@ -369,6 +369,30 @@ describe('AuthService test', () => { expect(storage.set).toHaveBeenCalled(); }); + it('should store the token with a Secure, SameSite cookie when served over https', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'https:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: true, + sameSite: 'lax', + })); + }); + + it('should store the token without the Secure flag when served over http', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'http:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: false, + sameSite: 'lax', + })); + }); + it('should remove token from storage', () => { authService.removeToken(); expect(storage.remove).toHaveBeenCalled(); diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 6d4ee2de4f6..936efac6d3d 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -456,7 +456,12 @@ export class AuthService { // Set the cookie expire date const expires = new Date(expireDate); - const options: CookieAttributes = {expires: expires}; + // Use Secure on HTTPS; Lax allows external-auth redirects via top-level GET navigation. + const options: CookieAttributes = { + expires: expires, + secure: this._window.nativeWindow.location?.protocol === 'https:', + sameSite: 'lax', + }; // Save cookie with the token return this.storage.set(TOKENITEM, token, options); diff --git a/src/app/core/browse/browse.service.ts b/src/app/core/browse/browse.service.ts index db1edf8b113..4301858f49e 100644 --- a/src/app/core/browse/browse.service.ts +++ b/src/app/core/browse/browse.service.ts @@ -24,6 +24,7 @@ import { followLink, FollowLinkConfig } from '../../shared/utils/follow-link-con import { BrowseDefinitionDataService } from './browse-definition-data.service'; import { SortDirection } from '../cache/models/sort-options.model'; import { environment } from '../../../environments/environment'; +import { MAX_PAGE_SIZE } from '../data/find-list-options.model'; export function getBrowseLinksToFollow(): FollowLinkConfig[] { @@ -69,7 +70,7 @@ export class BrowseService { */ getBrowseDefinitions(): Observable>> { // TODO properly support pagination - return this.browseDefinitionDataService.findAll({ elementsPerPage: 9999 }).pipe( + return this.browseDefinitionDataService.findAll({ elementsPerPage: MAX_PAGE_SIZE }).pipe( getFirstSucceededRemoteData(), ); } diff --git a/src/app/core/data/bundle-data.service.ts b/src/app/core/data/bundle-data.service.ts index 78ae204fe71..e62c5d85a3b 100644 --- a/src/app/core/data/bundle-data.service.ts +++ b/src/app/core/data/bundle-data.service.ts @@ -16,7 +16,7 @@ import { RequestService } from './request.service'; import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model'; import { Bitstream } from '../shared/bitstream.model'; import { RequestEntryState } from './request-entry-state.model'; -import { FindListOptions } from './find-list-options.model'; +import { FindListOptions, MAX_PAGE_SIZE } from './find-list-options.model'; import { IdentifiableDataService } from './base/identifiable-data.service'; import { PatchData, PatchDataImpl } from './base/patch-data'; import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; @@ -81,7 +81,7 @@ export class BundleDataService extends IdentifiableDataService implement findByItemAndName(item: Item, bundleName: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, options?: FindListOptions, ...linksToFollow: FollowLinkConfig[]): Observable> { //Since we filter by bundleName where the pagination options are not indicated we need to load all the possible bundles. // This is a workaround, in substitution of the previously recursive call with expand - const paginationOptions = options ?? { elementsPerPage: 9999 }; + const paginationOptions = options ?? { elementsPerPage: MAX_PAGE_SIZE }; return this.findAllByItem(item, paginationOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow).pipe( map((rd: RemoteData>) => { if (hasValue(rd.payload) && hasValue(rd.payload.page)) { diff --git a/src/app/core/data/dspace-rest-response-parsing.service.spec.ts b/src/app/core/data/dspace-rest-response-parsing.service.spec.ts new file mode 100644 index 00000000000..4d5b8060e79 --- /dev/null +++ b/src/app/core/data/dspace-rest-response-parsing.service.spec.ts @@ -0,0 +1,248 @@ +import { DspaceRestResponseParsingService } from './dspace-rest-response-parsing.service'; +import { RestRequest } from './rest-request.model'; +import { GetRequest, PostRequest } from './request.models'; +import { RawRestResponse } from '../dspace-rest/raw-rest-response.model'; +import { ObjectCacheService } from '../cache/object-cache.service'; + +/** + * Exposes the protected {@link DspaceRestResponseParsingService#ensureSelfLink} so it can be + * tested in isolation. + */ +class TestParsingService extends DspaceRestResponseParsingService { + public callEnsureSelfLink(request: RestRequest, response: RawRestResponse): RawRestResponse { + return this.ensureSelfLink(request, response); + } +} + +describe('DspaceRestResponseParsingService', () => { + let service: TestParsingService; + let objectCache: ObjectCacheService; + + const MISMATCH = jasmine.stringMatching(/These don't match/); + const REDUCED_PAGE = jasmine.stringMatching(/asked for a page of 9999 elements, but the REST API served 1000/); + const NO_SELF_LINK = jasmine.stringMatching(/doesn't have a self link/); + + const requestFor = (href: string): RestRequest => + new GetRequest('c4f0b1b7-3ffa-4b1a-9f5f-8bd6b1c4de71', href); + + const responseWithSelfLink = (href: string, page?: any): RawRestResponse => ({ + payload: { + _links: { + self: { href }, + }, + ...(page ? { page } : {}), + }, + statusCode: 200, + statusText: 'OK', + }); + + beforeEach(() => { + objectCache = jasmine.createSpyObj('objectCache', ['add', 'remove']); + service = new TestParsingService(objectCache); + spyOn(console, 'warn'); + }); + + describe('ensureSelfLink', () => { + + describe('differences the REST API is expected to introduce', () => { + + it('should not warn when the self link matches the requested url', () => { + const href = 'https://rest.api/core/bundles/9d18168a/bitstreams?page=0&size=5'; + const response = service.callEnsureSelfLink(requestFor(href), responseWithSelfLink(href)); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href).toBe(href); + }); + + it('should not warn when the self link only echoes the embed params of the request', () => { + // https://github.com/dataquest-dev/dspace-customers/issues/862 + const href = 'https://rest.api/core/bundles/9d18168a/bitstreams?page=0&embed=accessStatus&size=5'; + const response = service.callEnsureSelfLink(requestFor(href), responseWithSelfLink(href)); + + expect(console.warn).not.toHaveBeenCalled(); + // the self link is still normalized, because that's the url the response is cached under + expect(response.payload._links.self.href).toBe('https://rest.api/core/bundles/9d18168a/bitstreams?page=0&size=5'); + }); + + it('should not warn when the self link echoes embed params and the request has no other params', () => { + const href = 'https://rest.api/core/items/eba1c085/bundles?embed=primaryBitstream&embed=bitstreams/format&embed.size=bitstreams=5'; + const response = service.callEnsureSelfLink(requestFor(href), responseWithSelfLink(href)); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles'); + }); + + it('should not warn when the self link only percent decoded a param value', () => { + // https://github.com/dataquest-dev/dspace-customers/issues/862 + const request = requestFor('https://rest.api/statistics/usagereports/search/object?page=-1&size=10&uri=https%3A%2F%2Frest.api%2Fcore%2Fsites%2F8f842a80'); + const response = service.callEnsureSelfLink(request, responseWithSelfLink( + 'https://rest.api/statistics/usagereports/search/object?page=-1&size=10&uri=https://rest.api/core/sites/8f842a80')); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href) + .toBe('https://rest.api/statistics/usagereports/search/object?page=-1&size=10&uri=https%3A%2F%2Frest.api%2Fcore%2Fsites%2F8f842a80'); + }); + + it('should not warn or normalize when params are only in a different order', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&size=5'); + const response = service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?size=5&page=0')); + + expect(console.warn).not.toHaveBeenCalled(); + // the urls hold the same params, so nothing is rewritten here + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles?size=5&page=0'); + }); + + }); + + describe('differences that point at a problem with the endpoint', () => { + + it('should say so when the REST API reduced the requested page size', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?size=9999'); + service.callEnsureSelfLink(request, responseWithSelfLink( + 'https://rest.api/core/items/eba1c085/bundles?size=1000', + { number: 0, size: 1000, totalPages: 1, totalElements: 2 })); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(REDUCED_PAGE); + }); + + it('should report a reduced page size alongside other params without confusing the two', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&size=9999&sort=name,ASC'); + service.callEnsureSelfLink(request, responseWithSelfLink( + 'https://rest.api/core/items/eba1c085/bundles?page=0&size=1000&sort=name,ASC')); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(REDUCED_PAGE); + }); + + it('should fall back to the generic warning when more than the page size differs', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&size=9999'); + service.callEnsureSelfLink(request, responseWithSelfLink( + 'https://rest.api/core/items/eba1c085/bundles?page=3&size=1000')); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(MISMATCH); + }); + + it('should use the generic warning when the returned page size is larger than requested', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?size=5'); + service.callEnsureSelfLink(request, responseWithSelfLink( + 'https://rest.api/core/items/eba1c085/bundles?size=50', + { number: 0, size: 50, totalPages: 1, totalElements: 2 })); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(MISMATCH); + }); + + it('should still warn when a param value differs beyond its encoding', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?uri=https%3A%2F%2Frest.api%2Fcore%2Fsites%2Faaa'); + service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?uri=https://rest.api/core/sites/bbb')); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(MISMATCH); + }); + + it('should report the normalized request url and the raw self link in the warning', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&embed=primaryBitstream&size=5'); + service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?page=3&embed=primaryBitstream&size=5')); + + expect(console.warn).toHaveBeenCalledWith( + 'The response for \'https://rest.api/core/items/eba1c085/bundles?page=0&size=5\' has the self link ' + + '\'https://rest.api/core/items/eba1c085/bundles?page=3&embed=primaryBitstream&size=5\'. ' + + 'These don\'t match. This could mean there\'s an issue with the REST endpoint'); + }); + + it('should warn when a non-embed param differs', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&embed=primaryBitstream&size=5'); + service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?page=3&embed=primaryBitstream&size=5')); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(MISMATCH); + }); + + it('should warn when the self link has a param the request did not have', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?size=5'); + service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?size=5&sort=name,ASC')); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(MISMATCH); + }); + + it('should warn and fill in the requested url when the response has no self link', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?embed=primaryBitstream&size=5'); + const response = service.callEnsureSelfLink(request, { + payload: { _links: {} }, + statusCode: 200, + statusText: 'OK', + }); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith(NO_SELF_LINK); + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles?size=5'); + }); + + }); + + describe('normalization of the self link', () => { + + it('should normalize the self link when it differs, so it matches the cache key', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&embed=primaryBitstream&size=5'); + const response = service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?page=3&embed=primaryBitstream&size=5')); + + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles?page=0&size=5'); + }); + + it('should keep the other links when it normalizes the self link', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?page=0&size=5'); + const response = service.callEnsureSelfLink(request, { + payload: { + _links: { + self: { href: 'https://rest.api/core/items/eba1c085/bundles?page=3&size=5' }, + primaryBitstream: { href: 'https://rest.api/core/bitstreams/6a5f' }, + }, + }, + statusCode: 200, + statusText: 'OK', + }); + + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles?page=0&size=5'); + expect(response.payload._links.primaryBitstream.href).toBe('https://rest.api/core/bitstreams/6a5f'); + }); + + it('should not touch a self link on a different host', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?size=5'); + const response = service.callEnsureSelfLink(request, + responseWithSelfLink('https://other.api/core/items/eba1c085/bundles?size=5')); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href).toBe('https://other.api/core/items/eba1c085/bundles?size=5'); + }); + + it('should not touch a self link that points at a different path', () => { + const request = requestFor('https://rest.api/core/items/eba1c085/bundles?size=5'); + const response = service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085?size=5')); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085?size=5'); + }); + + it('should leave non-GET requests alone', () => { + const request = new PostRequest('c4f0b1b7-3ffa-4b1a-9f5f-8bd6b1c4de71', 'https://rest.api/core/items/eba1c085/bundles?size=5'); + const response = service.callEnsureSelfLink(request, + responseWithSelfLink('https://rest.api/core/items/eba1c085/bundles?size=1000')); + + expect(console.warn).not.toHaveBeenCalled(); + expect(response.payload._links.self.href).toBe('https://rest.api/core/items/eba1c085/bundles?size=1000'); + }); + + }); + + }); +}); diff --git a/src/app/core/data/dspace-rest-response-parsing.service.ts b/src/app/core/data/dspace-rest-response-parsing.service.ts index c0e1c70cae9..7f4125a4a6c 100644 --- a/src/app/core/data/dspace-rest-response-parsing.service.ts +++ b/src/app/core/data/dspace-rest-response-parsing.service.ts @@ -43,6 +43,11 @@ export function isRestPaginatedList(halObj: any): boolean { hasValue(halObj.page.number); } +/** + * The url param holding the page size + */ +const PAGE_SIZE_PARAM = 'size='; + /** * Split a url into parts * @@ -54,6 +59,64 @@ const splitUrlInParts = (url: string): string[] => { .reduce((combined, current) => [...combined, ...current]); }; +/** + * Return true if two lists of url parts don't hold the same parts, ignoring their order + */ +const urlPartsDiffer = (expected: string[], actual: string[]): boolean => { + return expected.some((part: string) => !actual.includes(part)) + || actual.some((part: string) => !expected.includes(part)); +}; + +/** + * Percent decode each url part, so `uri=http%3A%2F%2Fx` and `uri=http://x` compare equal. Decoding + * after the split keeps a decoded `&` from merging two params. + */ +const decodeUrlParts = (parts: string[]): string[] => { + return parts.map((part: string) => { + try { + return decodeURIComponent(part); + } catch (e) { + return part; + } + }); +}; + +/** + * The page size a url asks for, or undefined when it doesn't ask for a usable one + */ +const getPageSize = (parts: string[]): number | undefined => { + return parts.filter((part: string) => part.startsWith(PAGE_SIZE_PARAM)) + .map((part: string) => Number(part.substring(PAGE_SIZE_PARAM.length))) + .find((size: number) => Number.isInteger(size) && size > 0); +}; + +/** + * Return the parts without the one holding the page size + */ +const withoutPageSize = (parts: string[]): string[] => { + return parts.filter((part: string) => !part.startsWith(PAGE_SIZE_PARAM)); +}; + +/** + * Return the warning to log for a self link, or undefined when it describes the same request as the + * url it was requested with. A reduced page size gets its own message, since the generic one blames + * the endpoint for something the caller did. + */ +const selfLinkWarning = (requestedUrl: string, requestedUrlParts: string[], selfLink: string): string | undefined => { + const expected = decodeUrlParts(requestedUrlParts); + const actual = decodeUrlParts(splitUrlInParts(getUrlWithoutEmbedParams(selfLink))); + if (!urlPartsDiffer(expected, actual)) { + return undefined; + } + const requestedSize = getPageSize(expected); + const servedSize = getPageSize(actual); + if (hasValue(requestedSize) && hasValue(servedSize) && servedSize < requestedSize + && !urlPartsDiffer(withoutPageSize(expected), withoutPageSize(actual))) { + return `The request for '${requestedUrl}' asked for a page of ${requestedSize} elements, but the REST API served ${servedSize}. Ask for at most MAX_PAGE_SIZE elements`; + } + return `The response for '${requestedUrl}' has the self link '${selfLink}'. These don't match. This could mean there's an issue with the REST endpoint`; +}; + @Injectable({ providedIn: 'root' }) export class DspaceRestResponseParsingService implements ResponseParsingService { protected serializerConstructor: GenericConstructor> = DSpaceSerializer; @@ -156,10 +219,15 @@ export class DspaceRestResponseParsingService implements ResponseParsingService }); } else { + const selfLink = response.payload._links.self.href; const expected = splitUrlInParts(urlWithoutEmbedParams); - const actual = splitUrlInParts(response.payload._links.self.href); - if (expected[0] === actual[0] && (expected.some((e) => !actual.includes(e)) || actual.some((e) => !expected.includes(e)))) { - console.warn(`The response for '${urlWithoutEmbedParams}' has the self link '${response.payload._links.self.href}'. These don't match. This could mean there's an issue with the REST endpoint`); + const actual = splitUrlInParts(selfLink); + if (expected[0] === actual[0] && urlPartsDiffer(expected, actual)) { + // the self link is normalized either way, only the warning is conditional + const warning = selfLinkWarning(urlWithoutEmbedParams, expected, selfLink); + if (hasValue(warning)) { + console.warn(warning); + } response.payload._links = Object.assign({}, response.payload._links, { self: { href: urlWithoutEmbedParams diff --git a/src/app/core/data/find-list-options.model.ts b/src/app/core/data/find-list-options.model.ts index dc567d4b531..04a4c3c417d 100644 --- a/src/app/core/data/find-list-options.model.ts +++ b/src/app/core/data/find-list-options.model.ts @@ -1,6 +1,16 @@ import { SortOptions } from '../cache/models/sort-options.model'; import { RequestParam } from '../cache/models/request-param.model'; +/** + * The largest page the REST API will serve. Asking for more is not an error: the API silently + * reduces the size to this maximum, so a bigger number returns the exact same page while making the + * request claim something the API never honours. + * + * The limit is Spring Data REST's `spring.data.rest.max-page-size`, which DSpace leaves at its + * default. Use this instead of an arbitrary large number when a caller needs "everything". + */ +export const MAX_PAGE_SIZE = 1000; + /** * The options for a find list request */ diff --git a/src/app/core/registry/registry.service.ts b/src/app/core/registry/registry.service.ts index bbdf41d3ea8..79ce8cd8456 100644 --- a/src/app/core/registry/registry.service.ts +++ b/src/app/core/registry/registry.service.ts @@ -30,7 +30,7 @@ import { MetadataBitstreamDataService } from '../data/metadata-bitstream-data.se import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; import { RequestParam } from '../cache/models/request-param.model'; import { NoContent } from '../shared/NoContent.model'; -import { FindListOptions } from '../data/find-list-options.model'; +import { FindListOptions, MAX_PAGE_SIZE } from '../data/find-list-options.model'; import { MetadataBitstream } from '../metadata/metadata-bitstream.model'; const metadataRegistryStateSelector = (state: AppState) => state.metadataRegistry; @@ -81,7 +81,7 @@ export class RegistryService { public getMetadataSchemaByPrefix(prefix: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig[]): Observable> { // Temporary options to get ALL metadataschemas until there's a rest api endpoint for fetching a specific schema const options: FindListOptions = Object.assign(new FindListOptions(), { - elementsPerPage: 10000 + elementsPerPage: MAX_PAGE_SIZE }); return this.getMetadataSchemas(options).pipe( getFirstSucceededRemoteDataPayload(), diff --git a/src/app/core/url-serializer/bitstream-url-serializer.spec.ts b/src/app/core/url-serializer/bitstream-url-serializer.spec.ts index a66e47b3cd5..a8d5a903f2f 100644 --- a/src/app/core/url-serializer/bitstream-url-serializer.spec.ts +++ b/src/app/core/url-serializer/bitstream-url-serializer.spec.ts @@ -5,6 +5,10 @@ import { DefaultUrlSerializer, UrlTree } from '@angular/router'; describe('BitstreamUrlSerializer', () => { let serializer: BitstreamUrlSerializer; + function pathSegments(tree: UrlTree): string[] { + return tree.root.children.primary.segments.map((segment) => segment.path); + } + beforeEach(() => { TestBed.configureTestingModule({ providers: [BitstreamUrlSerializer] @@ -51,4 +55,18 @@ describe('BitstreamUrlSerializer', () => { const resultUrl = new DefaultUrlSerializer().serialize(result); expect(resultUrl).toBe(expectedUrl); }); + + it('should keep the whole query string when a query value contains a raw "?"', () => { + const result: UrlTree = serializer.parse('/bitstream/handle/123/456/file.pdf?isAllowed=y?x&sequenceId=2'); + + expect(pathSegments(result)).toEqual(['bitstream', 'handle', '123', '456', 'file.pdf']); + expect(result.queryParams).toEqual({ isAllowed: 'y?x', sequenceId: '2' }); + }); + + it('should not encode a fragment into the filename', () => { + const result: UrlTree = serializer.parse('/bitstream/handle/123/456/file.pdf#page=2'); + + expect(pathSegments(result)).toEqual(['bitstream', 'handle', '123', '456', 'file.pdf']); + expect(result.fragment).toBe('page=2'); + }); }); diff --git a/src/app/core/url-serializer/bitstream-url-serializer.ts b/src/app/core/url-serializer/bitstream-url-serializer.ts index 7872732e46a..24b6dfcb171 100644 --- a/src/app/core/url-serializer/bitstream-url-serializer.ts +++ b/src/app/core/url-serializer/bitstream-url-serializer.ts @@ -12,8 +12,10 @@ export class BitstreamUrlSerializer extends DefaultUrlSerializer { // Intercept parsing of every URL parse(url: string): UrlTree { if (url.startsWith('/bitstream/')) { - // Separate the path from the query string - const [path, query] = url.split('?'); + // Separate the path from the query string and fragment (everything from the first raw '?' or '#') + const queryIndex = url.search(/[?#]/); + const path = queryIndex === -1 ? url : url.substring(0, queryIndex); + const queryAndFragment = queryIndex === -1 ? '' : url.substring(queryIndex); // Split the path to isolate the filename const parts = path.split('/'); @@ -24,8 +26,8 @@ export class BitstreamUrlSerializer extends DefaultUrlSerializer { // Reconstruct the path with the encoded filename const newPath = [...parts.slice(0, this.FILENAME_INDEX), encodedFilename].join('/'); - // Reattach query string if present - url = query ? `${newPath}?${query}` : newPath; + // Reattach the query string and fragment unchanged + url = newPath + queryAndFragment; } } return super.parse(url); diff --git a/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.spec.ts b/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.spec.ts new file mode 100644 index 00000000000..9be03b198f6 --- /dev/null +++ b/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.spec.ts @@ -0,0 +1,56 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { TranslateModule } from '@ngx-translate/core'; +import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap'; +import { of as observableOf } from 'rxjs'; +import { ClarinRefCitationComponent } from './clarin-ref-citation.component'; +import { ConfigurationDataService } from '../../core/data/configuration-data.service'; +import { RequestService } from '../../core/data/request.service'; +import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; +import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; +import { Item } from '../../core/shared/item.model'; +import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils'; + +describe('ClarinRefCitationComponent', () => { + const handleUrl = 'https://hdl.handle.net/123456789/1'; + const displayText = 'Doe, 2024, Title, ' + + 'bad link, ' + handleUrl + '.' + + ''; + + let fixture: ComponentFixture; + let citation: HTMLElement; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ClarinRefCitationComponent], + imports: [TranslateModule.forRoot(), NgbTooltipModule], + providers: [ + { provide: ConfigurationDataService, useValue: {} }, + { provide: RequestService, useValue: jasmine.createSpyObj('requestService', ['generateRequestId', 'send']) }, + { provide: HALEndpointService, useValue: { getRootHref: () => 'https://rest.test/server/api' } }, + { + provide: RemoteDataBuildService, useValue: jasmine.createSpyObj('rdbService', { + buildFromRequestUUID: observableOf(createSuccessfulRemoteDataObject({ displayText })), + }) + }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(ClarinRefCitationComponent); + fixture.componentInstance.item = Object.assign(new Item(), { handle: '123456789/1', metadata: {} }); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + citation = fixture.nativeElement.querySelector('.clarin-ref-box-text'); + }); + + it('should not render scripts or event handlers from the display text', () => { + expect(citation.querySelector('script')).toBeNull(); + expect(citation.querySelector('[onerror]')).toBeNull(); + expect(citation.querySelector('a[href^="javascript:"]')).toBeNull(); + }); + + it('should keep the citation formatting', () => { + expect(citation.querySelector('i').textContent).toBe('Title'); + expect(citation.querySelector('a[href="' + handleUrl + '"]').textContent).toBe(handleUrl); + }); +}); diff --git a/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.ts b/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.ts index 94980315738..b014bf8bedb 100644 --- a/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.ts +++ b/src/app/item-page/clarin-ref-citation/clarin-ref-citation.component.ts @@ -10,7 +10,6 @@ import { RequestService } from '../../core/data/request.service'; import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; import { BehaviorSubject } from 'rxjs'; -import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; /** @@ -42,9 +41,9 @@ export class ClarinRefCitationComponent implements OnInit { /** * The content of the reference box, which will be displayed in the tooltip. - * This content is fetched from the RefBox Controller. + * This content is fetched from the RefBox Controller and sanitized by Angular when rendered. */ - refboxContent: BehaviorSubject = new BehaviorSubject(null); + refboxContent: BehaviorSubject = new BehaviorSubject(null); /** * The raw content of the reference box, which is fetched from the RefBox Controller. @@ -62,8 +61,7 @@ export class ClarinRefCitationComponent implements OnInit { private modalService: NgbModal, private requestService: RequestService, protected rdbService: RemoteDataBuildService, - protected halService: HALEndpointService, - private sanitizer: DomSanitizer) { + protected halService: HALEndpointService) { // Configure the tooltip to show on click - `Copied` message config.triggers = 'click'; } @@ -72,7 +70,7 @@ export class ClarinRefCitationComponent implements OnInit { void this.fetchRefBoxContent() .then((content) => { this.refboxCopyContent = content; // Store raw HTML - this.refboxContent.next(this.sanitizer.bypassSecurityTrustHtml(content)); + this.refboxContent.next(content); }).catch((error) => { console.error('Failed to fetch refbox content:', error); this.refboxCopyContent = this.EMPTY_CONTENT; diff --git a/src/app/item-page/edit-item-page/item-bitstreams/item-bitstreams.service.ts b/src/app/item-page/edit-item-page/item-bitstreams/item-bitstreams.service.ts index 8f63e9ab6e4..c222b0f21ac 100644 --- a/src/app/item-page/edit-item-page/item-bitstreams/item-bitstreams.service.ts +++ b/src/app/item-page/edit-item-page/item-bitstreams/item-bitstreams.service.ts @@ -24,6 +24,7 @@ import { MoveOperation } from 'fast-json-patch'; import { BundleDataService } from '../../../core/data/bundle-data.service'; import { RequestService } from '../../../core/data/request.service'; import { LiveRegionService } from '../../../shared/live-region/live-region.service'; +import { MAX_PAGE_SIZE } from '../../../core/data/find-list-options.model'; export const MOVE_KEY = 'item.edit.bitstreams.notifications.move'; @@ -344,7 +345,7 @@ export class ItemBitstreamsService { return Object.assign(new PaginationComponentOptions(), { id: 'bundles-pagination-options', currentPage: 1, - pageSize: 9999 + pageSize: MAX_PAGE_SIZE }); } diff --git a/src/app/item-page/edit-item-page/item-license-mapper/item-license-mapper.component.ts b/src/app/item-page/edit-item-page/item-license-mapper/item-license-mapper.component.ts index d999bed8678..d9e06d455cd 100644 --- a/src/app/item-page/edit-item-page/item-license-mapper/item-license-mapper.component.ts +++ b/src/app/item-page/edit-item-page/item-license-mapper/item-license-mapper.component.ts @@ -8,7 +8,7 @@ import { ClarinLicenseDataService } from '../../../core/data/clarin/clarin-licen import { getFirstCompletedRemoteData, getFirstSucceededRemoteListPayload } from '../../../core/shared/operators'; import { PaginatedList } from '../../../core/data/paginated-list.model'; import { ClarinLicense } from '../../../core/shared/clarin/clarin-license.model'; -import { FindListOptions } from '../../../core/data/find-list-options.model'; +import { FindListOptions, MAX_PAGE_SIZE } from '../../../core/data/find-list-options.model'; import { PutRequest } from '../../../core/data/request.models'; import { HALEndpointService } from '../../../core/shared/hal-endpoint.service'; import { RequestService } from '../../../core/data/request.service'; @@ -90,7 +90,7 @@ export class ItemLicenseMapperComponent implements OnInit { const options = new FindListOptions(); options.currentPage = 0; // Load all licenses - options.elementsPerPage = 1000; + options.elementsPerPage = MAX_PAGE_SIZE; this.clarinLicenseService.findAll(options, false) .pipe( diff --git a/src/app/item-page/simple/field-components/clarin-item-versions-field/clarin-item-versions-field.component.ts b/src/app/item-page/simple/field-components/clarin-item-versions-field/clarin-item-versions-field.component.ts index d0507103c12..d2b4216e5b6 100644 --- a/src/app/item-page/simple/field-components/clarin-item-versions-field/clarin-item-versions-field.component.ts +++ b/src/app/item-page/simple/field-components/clarin-item-versions-field/clarin-item-versions-field.component.ts @@ -5,6 +5,7 @@ import { ItemVersionsComponent } from '../../../versions/item-versions.component import { Item } from '../../../../core/shared/item.model'; import { Version } from '../../../../core/shared/version.model'; import { RemoteData } from '../../../../core/data/remote-data'; +import { MAX_PAGE_SIZE } from '../../../../core/data/find-list-options.model'; /** * Local type definition matching the parent component's VersionsDTO structure @@ -43,7 +44,7 @@ export class ClarinItemVersionsFieldComponent extends ItemVersionsComponent impl /** * Maximum number of versions to fetch at once for the dropdown display. */ - private readonly MAX_VERSIONS_TO_DISPLAY = 9999; + private readonly MAX_VERSIONS_TO_DISPLAY = MAX_PAGE_SIZE; /** * Icon name for the clarin field diff --git a/src/app/shared/clarin-item-box-view/clarin-item-box-view.component.ts b/src/app/shared/clarin-item-box-view/clarin-item-box-view.component.ts index e63961ee6ea..448c4f1840f 100644 --- a/src/app/shared/clarin-item-box-view/clarin-item-box-view.component.ts +++ b/src/app/shared/clarin-item-box-view/clarin-item-box-view.component.ts @@ -29,7 +29,7 @@ import { ListableObject } from '../object-collection/shared/listable-object.mode import { ItemSearchResult } from '../object-collection/shared/item-search-result.model'; import { getItemPageRoute } from '../../item-page/item-page-routing-paths'; import { metadataLangToBcp47 } from '../utils/metadata-language.util'; -import { FindListOptions } from '../../core/data/find-list-options.model'; +import { FindListOptions, MAX_PAGE_SIZE } from '../../core/data/find-list-options.model'; import { ClarinDateService } from '../clarin-date.service'; import { AUTHOR_METADATA_FIELDS } from '../../core/shared/clarin/constants'; import {RequestParam} from '../../core/cache/models/request-param.model'; @@ -181,7 +181,7 @@ export class ClarinItemBoxViewComponent implements OnInit { return; } const configAllElements: FindListOptions = Object.assign(new FindListOptions(), { - elementsPerPage: 9999 + elementsPerPage: MAX_PAGE_SIZE }); this.bundleService.findByItemAndName(this.item, 'ORIGINAL', true, true, diff --git a/src/app/shared/html-content.service.spec.ts b/src/app/shared/html-content.service.spec.ts index dbc04dcf4e4..9f459638e2c 100644 --- a/src/app/shared/html-content.service.spec.ts +++ b/src/app/shared/html-content.service.spec.ts @@ -1,5 +1,7 @@ import { fakeAsync, TestBed, tick } from '@angular/core/testing'; +import { PLATFORM_ID, Provider } from '@angular/core'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { REQUEST } from '@nguniversal/express-engine/tokens'; import { firstValueFrom } from 'rxjs'; import { HtmlContentService } from './html-content.service'; @@ -19,7 +21,7 @@ describe('HtmlContentService', () => { let httpMock: HttpTestingController; let localeService: LocaleServiceStub; - function setup(nameSpace: string): void { + function setup(nameSpace: string, serverProviders: Provider[] = []): void { TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [ @@ -28,9 +30,10 @@ describe('HtmlContentService', () => { { provide: APP_CONFIG, useValue: { - ui: { nameSpace }, + ui: { nameSpace, baseUrl: `http://localhost:4000${nameSpace}` }, }, }, + ...serverProviders, ], }); @@ -112,6 +115,27 @@ describe('HtmlContentService', () => { expect(content).toBe('
English Content
'); })); + it('should fetch static content from the configured UI origin, not the request Host, during SSR', async () => { + setup('/repository', [ + { provide: PLATFORM_ID, useValue: 'server' }, + { provide: REQUEST, useValue: { protocol: 'https', get: () => 'attacker.example' } }, + ]); + + const promise = service.getHmtlContentByPathAndLocale('about'); + + httpMock.expectOne('http://localhost:4000/repository/static-files/about.html').flush('Trusted content'); + expect(await promise).toBe('Trusted content'); + }); + + it('should not request a file name that leaves static-files once decoded', async () => { + setup('/repository'); + + const promise = service.getHmtlContentByPathAndLocale('..%2Findex'); + + httpMock.expectNone(() => true); + expect(await promise).toBeUndefined(); + }); + it('should return empty string from getHtmlContent when request fails', async () => { setup('/repository'); diff --git a/src/app/shared/html-content.service.ts b/src/app/shared/html-content.service.ts index 664eaf71e22..82d40ccfb7c 100644 --- a/src/app/shared/html-content.service.ts +++ b/src/app/shared/html-content.service.ts @@ -1,5 +1,5 @@ import { isPlatformServer } from '@angular/common'; -import { Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core'; +import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; import { HttpClient, HttpResponse } from '@angular/common/http'; import { catchError } from 'rxjs/operators'; import { firstValueFrom, of as observableOf } from 'rxjs'; @@ -7,7 +7,6 @@ import { HTML_SUFFIX, STATIC_FILES_PROJECT_PATH } from '../static-page/static-pa import { isEmpty } from './empty.util'; import { LocaleService } from '../core/locale/locale.service'; import { APP_CONFIG, AppConfig } from '../../config/app-config.interface'; -import { REQUEST } from '@nguniversal/express-engine/tokens'; /** * Service for loading static `.html` files stored in the `/static-files` folder. @@ -18,7 +17,6 @@ export class HtmlContentService { private localeService: LocaleService, @Inject(APP_CONFIG) protected appConfig?: AppConfig, @Inject(PLATFORM_ID) private platformId?: object, - @Optional() @Inject(REQUEST) private request?: any, ) {} private getNamespacePrefix(): string { @@ -45,17 +43,12 @@ export class HtmlContentService { } private buildRuntimeUrl(path: string): string { - if (!isPlatformServer(this.platformId) || !this.request) { + if (!isPlatformServer(this.platformId)) { return path; } - const protocol = this.request.protocol; - const host = this.request.get?.('host'); - if (!protocol || !host) { - return path; - } - - return `${protocol}://${host}${path}`; + // The request Host header is client-controlled; SSR loads static HTML only from the configured UI origin. + return `${new URL(this.appConfig.ui.baseUrl).origin}${path}`; } getHtmlContent(url: string) { @@ -91,10 +84,26 @@ export class HtmlContentService { return undefined; } + /** + * Whether a file name taken from the URL is still one file name once the server decodes it, + * so it cannot address a file outside `static-files` (e.g. `..%2Findex`). A query part is ignored. + */ + private isPlainFileName(fileName: string): boolean { + try { + return !/[/\\]/.test(decodeURIComponent(fileName.split('?')[0])); + } catch { + // Missing name or malformed percent-encoding: nothing to load. + return false; + } + } + /** * Get the html file content as a string by the file name and the current locale. */ async getHmtlContentByPathAndLocale(fileName: string) { + if (!this.isPlainFileName(fileName)) { + return undefined; + } let url = ''; // Get current language let language = this.localeService.getCurrentLanguageCode(); diff --git a/src/app/shared/ssr-cache-key.util.spec.ts b/src/app/shared/ssr-cache-key.util.spec.ts new file mode 100644 index 00000000000..7c8827fa197 --- /dev/null +++ b/src/app/shared/ssr-cache-key.util.spec.ts @@ -0,0 +1,21 @@ +import { Request } from 'express'; + +import { getSsrCacheKey } from './ssr-cache-key.util'; +import { LANG_COOKIE } from '../core/locale/locale.service'; + +describe('getSsrCacheKey', () => { + function request(protocol: string, host: string, cookies: Record = {}): Request { + return { url: '/static/about', protocol, get: () => host, cookies } as unknown as Request; + } + + it('should separate renders for different language cookies', () => { + expect(getSsrCacheKey(request('https', 'repo.example', { [LANG_COOKIE]: 'cs' }))) + .not.toEqual(getSsrCacheKey(request('https', 'repo.example', { [LANG_COOKIE]: 'en' }))); + }); + + it('should separate renders for different request origins', () => { + const key = getSsrCacheKey(request('https', 'repo.example')); + expect(getSsrCacheKey(request('https', 'attacker.example'))).not.toEqual(key); + expect(getSsrCacheKey(request('http', 'repo.example'))).not.toEqual(key); + }); +}); diff --git a/src/app/shared/ssr-cache-key.util.ts b/src/app/shared/ssr-cache-key.util.ts new file mode 100644 index 00000000000..4f05e1479c5 --- /dev/null +++ b/src/app/shared/ssr-cache-key.util.ts @@ -0,0 +1,12 @@ +import { Request } from 'express'; +import { LANG_COOKIE } from '../core/locale/locale.service'; + +/** + * Key of a server-side rendered page in the SSR page caches (see server.ts). + * Besides path and query, an anonymous render depends on the request origin (protocol and Host header, + * e.g. in `citation_pdf_url`) and on the language cookie, so they are part of the key. + * @param req current request + */ +export function getSsrCacheKey(req: Request): string { + return JSON.stringify([req.protocol, req.get('host'), req.cookies?.[LANG_COOKIE], req.url]); +} diff --git a/src/app/shared/view-mode-switch/view-mode-switch.component.html b/src/app/shared/view-mode-switch/view-mode-switch.component.html index 5d29562a766..0f29e2624ab 100644 --- a/src/app/shared/view-mode-switch/view-mode-switch.component.html +++ b/src/app/shared/view-mode-switch/view-mode-switch.component.html @@ -1,4 +1,4 @@ -
+