fix critical and high severity security vulnerabilities (#3060)

This commit is contained in:
ACX
2026-02-26 22:34:01 -07:00
committed by GitHub
parent 3cbb3372c9
commit 46d744318d
6 changed files with 38 additions and 10 deletions
@@ -29,7 +29,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ErrorResponse> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(), "Request body is missing or malformed: " + ex.getMostSpecificCause().getMessage());
log.warn("Malformed request body: {}", ex.getMessage());
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(), "Request body is missing or malformed.");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
@@ -58,7 +59,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<ErrorResponse> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
log.warn("Data integrity violation: {}", ex.getMessage());
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(), "A data conflict occurred. The operation could not be completed.");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
@@ -114,8 +116,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
log.error(ex.getMessage(), ex);
log.error("Unhandled exception: {}", ex.getMessage(), ex);
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred.");
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@@ -110,8 +110,17 @@ public class BookService {
public List<Book> getBooksByIds(Set<Long> bookIds, boolean withDescription) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
boolean isAdmin = user.getPermissions().isAdmin();
List<BookEntity> bookEntities = bookQueryService.findAllWithMetadataByIds(bookIds);
if (!isAdmin) {
Set<Long> userLibraryIds = getUserLibraryIds(user);
bookEntities = bookEntities.stream()
.filter(book -> userLibraryIds.contains(book.getLibrary().getId()))
.toList();
}
Set<Long> entityIds = bookEntities.stream().map(BookEntity::getId).collect(Collectors.toSet());
Map<Long, UserBookProgressEntity> progressMap =
@@ -363,7 +372,15 @@ public class BookService {
@Transactional
public ResponseEntity<BookDeletionResponse> deleteBooks(Set<Long> ids) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
List<BookEntity> books = bookQueryService.findAllWithMetadataByIds(ids);
if (!user.getPermissions().isAdmin()) {
Set<Long> userLibraryIds = getUserLibraryIds(user);
books = books.stream()
.filter(book -> userLibraryIds.contains(book.getLibrary().getId()))
.toList();
}
List<Long> failedFileDeletions = new ArrayList<>();
for (BookEntity book : books) {
for (BookFileEntity bookFile : book.getBookFiles()) {
@@ -670,7 +670,7 @@
</div>
<div class="description-body">
<div #descriptionContent [ngClass]="{ 'line-clamp-7': !isExpanded }" class="description-content">
<div class="description-html" [innerHTML]="book.metadata!.description"></div>
<div class="description-html" [innerHTML]="sanitizeDescription(book.metadata!.description)"></div>
</div>
@if (isExpanded || isOverflowing) {
<button class="expand-toggle" (click)="toggleExpand()">
@@ -38,6 +38,7 @@ import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/tran
import {AuthorService} from '../../../../author-browser/service/author.service';
import {Dialog} from 'primeng/dialog';
import {Checkbox} from 'primeng/checkbox';
import DOMPurify from 'dompurify';
@Component({
@@ -490,6 +491,10 @@ export class MetadataViewerComponent implements OnInit, OnChanges, AfterViewChec
this.isExpanded = !this.isExpanded;
}
sanitizeDescription(html: string | null | undefined): string {
return html ? DOMPurify.sanitize(html) : '';
}
read(bookId: number | undefined, reader?: "epub-streaming", bookType?: BookType): void {
if (bookId) this.bookService.readBook(bookId, reader, bookType);
}
@@ -4,6 +4,7 @@ import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {IconService} from '../../services/icon.service';
import {IconCacheService} from '../../services/icon-cache.service';
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
import DOMPurify from 'dompurify';
import {UrlHelperService} from '../../service/url-helper.service';
import {MessageService} from 'primeng/api';
import {IconCategoriesHelper} from '../../helpers/icon-categories.helper';
@@ -158,7 +159,11 @@ export class IconPickerComponent implements OnInit {
}
try {
this.svgPreview = this.sanitizer.bypassSecurityTrustHtml(this.svgContent);
const sanitized = DOMPurify.sanitize(this.svgContent, {
USE_PROFILES: { svg: true },
FORBID_TAGS: ['script', 'style', 'foreignObject']
});
this.svgPreview = this.sanitizer.bypassSecurityTrustHtml(sanitized);
} catch {
this.svgPreview = null;
this.errorMessage = this.ERROR_MESSAGES.PARSE_ERROR;
@@ -1,8 +1,8 @@
import { Component, inject, OnInit } from '@angular/core';
import { ReleaseNote, VersionService } from '../../../../service/version.service';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import showdown from 'showdown';
import DOMPurify from 'dompurify';
import {DatePipe} from '@angular/common';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {Button} from 'primeng/button';
@@ -22,7 +22,6 @@ import {TranslocoDirective} from '@jsverse/transloco';
export class VersionChangelogDialogComponent implements OnInit {
private versionService = inject(VersionService);
private sanitizer = inject(DomSanitizer);
dialogRef = inject(DynamicDialogRef);
changelog: ReleaseNote[] = [];
@@ -42,9 +41,9 @@ export class VersionChangelogDialogComponent implements OnInit {
});
}
markdownToHtml(markdown: string): SafeHtml {
markdownToHtml(markdown: string): string {
let html = this.converter.makeHtml(markdown);
html = html.replace(/<h2\b([^>]*)>/g, '<h3$1>').replace(/<\/h2>/g, '</h3>');
return this.sanitizer.bypassSecurityTrustHtml(html);
return DOMPurify.sanitize(html);
}
}