From 5898153d8d99a1a66da296899a4e48ff82ff4b43 Mon Sep 17 00:00:00 2001 From: James Ward Date: Wed, 5 Aug 2026 15:32:46 -0400 Subject: [PATCH] chore(ui): drop `secure-src` direcctive (#2255) --- .../security/secure-src.directive.spec.ts | 157 ------------------ .../app/core/security/secure-src.directive.ts | 63 ------- 2 files changed, 220 deletions(-) delete mode 100644 frontend/src/app/core/security/secure-src.directive.spec.ts delete mode 100644 frontend/src/app/core/security/secure-src.directive.ts diff --git a/frontend/src/app/core/security/secure-src.directive.spec.ts b/frontend/src/app/core/security/secure-src.directive.spec.ts deleted file mode 100644 index 01295c62e..000000000 --- a/frontend/src/app/core/security/secure-src.directive.spec.ts +++ /dev/null @@ -1,157 +0,0 @@ -import {Component} from '@angular/core'; -import {provideHttpClient} from '@angular/common/http'; -import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing'; -import {ComponentFixture, TestBed} from '@angular/core/testing'; -import {By} from '@angular/platform-browser'; -import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; - -import {SecureSrcDirective} from './secure-src.directive'; - -@Component({ - standalone: true, - imports: [SecureSrcDirective], - template: `test cover` -}) -class TestHostComponent { - src = ''; -} - -@Component({ - standalone: true, - imports: [SecureSrcDirective], - template: `test cover` -}) -class DefaultFallbackHostComponent { - src = ''; -} - -describe('SecureSrcDirective', () => { - let fixture: ComponentFixture; - let httpTestingController: HttpTestingController; - - beforeEach(() => { - vi.restoreAllMocks(); - TestBed.resetTestingModule(); - TestBed.configureTestingModule({ - imports: [TestHostComponent], - providers: [ - provideHttpClient(), - provideHttpClientTesting(), - ] - }); - - fixture = TestBed.createComponent(TestHostComponent); - httpTestingController = TestBed.inject(HttpTestingController); - }); - - afterEach(() => { - httpTestingController.verify(); - TestBed.resetTestingModule(); - }); - - function image(): HTMLImageElement { - return fixture.debugElement.query(By.css('img')).nativeElement as HTMLImageElement; - } - - it('loads a secure blob URL into the host image', () => { - vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:secure-cover'); - - fixture.componentInstance.src = '/covers/1'; - fixture.detectChanges(); - - const request = httpTestingController.expectOne('/covers/1'); - request.flush(new Blob(['cover'])); - - expect(image().src).toContain('blob:secure-cover'); - }); - - it('skips loading when the source is empty', () => { - fixture.detectChanges(); - - httpTestingController.expectNone(() => true); - expect(image().getAttribute('src')).toBeNull(); - }); - - it('uses the fallback image and suppresses CORS-style console noise for status 0 and 403', () => { - const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); - - fixture.componentInstance.src = '/covers/2'; - fixture.detectChanges(); - - const firstRequest = httpTestingController.expectOne('/covers/2'); - firstRequest.error(new ProgressEvent('error'), {status: 0, statusText: 'Unknown Error'}); - - expect(image().src).toContain('/fallback.png'); - expect(errorSpy).not.toHaveBeenCalled(); - - fixture.componentInstance.src = '/covers/3'; - fixture.changeDetectorRef.markForCheck(); - fixture.detectChanges(); - - const secondRequest = httpTestingController.expectOne('/covers/3'); - secondRequest.flush(new Blob(['forbidden']), {status: 403, statusText: 'Forbidden'}); - - expect(image().src).toContain('/fallback.png'); - expect(errorSpy).not.toHaveBeenCalled(); - }); - - it('logs unexpected failures and revokes prior object URLs during cleanup', () => { - const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); - const revokeSpy = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined); - vi.spyOn(URL, 'createObjectURL').mockReturnValueOnce('blob:first').mockReturnValueOnce('blob:second'); - - fixture.componentInstance.src = '/covers/4'; - fixture.detectChanges(); - httpTestingController.expectOne('/covers/4').flush(new Blob(['first'])); - - fixture.componentInstance.src = '/covers/5'; - fixture.changeDetectorRef.markForCheck(); - fixture.detectChanges(); - - expect(revokeSpy).toHaveBeenCalledWith('blob:first'); - - httpTestingController.expectOne('/covers/5').flush(new Blob(['boom']), {status: 500, statusText: 'Server Error'}); - - expect(image().src).toContain('/fallback.png'); - expect(errorSpy).toHaveBeenCalledOnce(); - - fixture.destroy(); - }); - - it('revokes the active object URL when the directive is destroyed after a successful load', () => { - const revokeSpy = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined); - vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:destroyed'); - - fixture.componentInstance.src = '/covers/7'; - fixture.detectChanges(); - - const request = httpTestingController.expectOne('/covers/7'); - request.flush(new Blob(['cover'])); - - fixture.destroy(); - - expect(revokeSpy).toHaveBeenCalledWith('blob:destroyed'); - }); - - it('uses the default fallback image when no fallback attribute is provided', () => { - TestBed.resetTestingModule(); - TestBed.configureTestingModule({ - imports: [DefaultFallbackHostComponent], - providers: [ - provideHttpClient(), - provideHttpClientTesting(), - ] - }); - - const defaultFixture = TestBed.createComponent(DefaultFallbackHostComponent); - const defaultHttpTestingController = TestBed.inject(HttpTestingController); - defaultFixture.componentInstance.src = '/covers/6'; - defaultFixture.detectChanges(); - - const request = defaultHttpTestingController.expectOne('/covers/6'); - request.error(new ProgressEvent('error'), {status: 500, statusText: 'Server Error'}); - - expect(defaultFixture.debugElement.query(By.css('img')).nativeElement.src).toContain('assets/images/missing-cover.jpg'); - defaultHttpTestingController.verify(); - }); -}); diff --git a/frontend/src/app/core/security/secure-src.directive.ts b/frontend/src/app/core/security/secure-src.directive.ts deleted file mode 100644 index fea9ead40..000000000 --- a/frontend/src/app/core/security/secure-src.directive.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Directive, ElementRef, Input, OnChanges, OnDestroy, inject, SimpleChanges } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -import { Subscription } from 'rxjs'; - -@Directive({ - selector: '[appSecureSrc]', - standalone: true -}) -export class SecureSrcDirective implements OnChanges, OnDestroy { - @Input('appSecureSrc') src!: string; - - private currentUrl: string | null = null; - private subscription: Subscription | null = null; - - private el = inject(ElementRef); - private http = inject(HttpClient); - - ngOnChanges(changes: SimpleChanges): void { - if (changes['src']) { - this.loadImage(); - } - } - - private loadImage(): void { - this.cleanup(); - - if (!this.src) { - return; - } - - this.subscription = this.http.get(this.src, { responseType: 'blob' }).subscribe({ - next: (blob) => { - this.currentUrl = URL.createObjectURL(blob); - this.el.nativeElement.src = this.currentUrl; - }, - error: (err) => { - // Only log non-CORS errors to avoid console spam - // CORS errors are expected in development and will be resolved by backend CORS config - if (err.status !== 0 && err.status !== 403) { - console.error('Error loading secure image:', err); - } - // Set a fallback placeholder image if available - const fallbackSrc = this.el.nativeElement.getAttribute('data-fallback-src') || 'assets/images/missing-cover.jpg'; - this.el.nativeElement.src = fallbackSrc; - } - }); - } - - private cleanup(): void { - if (this.subscription) { - this.subscription.unsubscribe(); - this.subscription = null; - } - if (this.currentUrl) { - URL.revokeObjectURL(this.currentUrl); - this.currentUrl = null; - } - } - - ngOnDestroy(): void { - this.cleanup(); - } -}