fix critical and high severity security vulnerabilities (#3060)
This commit is contained in:
@@ -29,7 +29,8 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
public ResponseEntity<ErrorResponse> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
|
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);
|
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +59,8 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||||
public ResponseEntity<ErrorResponse> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
|
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);
|
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,8 +116,8 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
||||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
|
log.error("Unhandled exception: {}", ex.getMessage(), ex);
|
||||||
log.error(ex.getMessage(), ex);
|
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred.");
|
||||||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,8 +110,17 @@ public class BookService {
|
|||||||
|
|
||||||
public List<Book> getBooksByIds(Set<Long> bookIds, boolean withDescription) {
|
public List<Book> getBooksByIds(Set<Long> bookIds, boolean withDescription) {
|
||||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||||
|
boolean isAdmin = user.getPermissions().isAdmin();
|
||||||
|
|
||||||
List<BookEntity> bookEntities = bookQueryService.findAllWithMetadataByIds(bookIds);
|
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());
|
Set<Long> entityIds = bookEntities.stream().map(BookEntity::getId).collect(Collectors.toSet());
|
||||||
|
|
||||||
Map<Long, UserBookProgressEntity> progressMap =
|
Map<Long, UserBookProgressEntity> progressMap =
|
||||||
@@ -363,7 +372,15 @@ public class BookService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public ResponseEntity<BookDeletionResponse> deleteBooks(Set<Long> ids) {
|
public ResponseEntity<BookDeletionResponse> deleteBooks(Set<Long> ids) {
|
||||||
|
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||||
List<BookEntity> books = bookQueryService.findAllWithMetadataByIds(ids);
|
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<>();
|
List<Long> failedFileDeletions = new ArrayList<>();
|
||||||
for (BookEntity book : books) {
|
for (BookEntity book : books) {
|
||||||
for (BookFileEntity bookFile : book.getBookFiles()) {
|
for (BookFileEntity bookFile : book.getBookFiles()) {
|
||||||
|
|||||||
+1
-1
@@ -670,7 +670,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="description-body">
|
<div class="description-body">
|
||||||
<div #descriptionContent [ngClass]="{ 'line-clamp-7': !isExpanded }" class="description-content">
|
<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>
|
</div>
|
||||||
@if (isExpanded || isOverflowing) {
|
@if (isExpanded || isOverflowing) {
|
||||||
<button class="expand-toggle" (click)="toggleExpand()">
|
<button class="expand-toggle" (click)="toggleExpand()">
|
||||||
|
|||||||
+5
@@ -38,6 +38,7 @@ import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/tran
|
|||||||
import {AuthorService} from '../../../../author-browser/service/author.service';
|
import {AuthorService} from '../../../../author-browser/service/author.service';
|
||||||
import {Dialog} from 'primeng/dialog';
|
import {Dialog} from 'primeng/dialog';
|
||||||
import {Checkbox} from 'primeng/checkbox';
|
import {Checkbox} from 'primeng/checkbox';
|
||||||
|
import DOMPurify from 'dompurify';
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -490,6 +491,10 @@ export class MetadataViewerComponent implements OnInit, OnChanges, AfterViewChec
|
|||||||
this.isExpanded = !this.isExpanded;
|
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 {
|
read(bookId: number | undefined, reader?: "epub-streaming", bookType?: BookType): void {
|
||||||
if (bookId) this.bookService.readBook(bookId, reader, bookType);
|
if (bookId) this.bookService.readBook(bookId, reader, bookType);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
|||||||
import {IconService} from '../../services/icon.service';
|
import {IconService} from '../../services/icon.service';
|
||||||
import {IconCacheService} from '../../services/icon-cache.service';
|
import {IconCacheService} from '../../services/icon-cache.service';
|
||||||
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
|
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
|
||||||
|
import DOMPurify from 'dompurify';
|
||||||
import {UrlHelperService} from '../../service/url-helper.service';
|
import {UrlHelperService} from '../../service/url-helper.service';
|
||||||
import {MessageService} from 'primeng/api';
|
import {MessageService} from 'primeng/api';
|
||||||
import {IconCategoriesHelper} from '../../helpers/icon-categories.helper';
|
import {IconCategoriesHelper} from '../../helpers/icon-categories.helper';
|
||||||
@@ -158,7 +159,11 @@ export class IconPickerComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 {
|
} catch {
|
||||||
this.svgPreview = null;
|
this.svgPreview = null;
|
||||||
this.errorMessage = this.ERROR_MESSAGES.PARSE_ERROR;
|
this.errorMessage = this.ERROR_MESSAGES.PARSE_ERROR;
|
||||||
|
|||||||
+3
-4
@@ -1,8 +1,8 @@
|
|||||||
import { Component, inject, OnInit } from '@angular/core';
|
import { Component, inject, OnInit } from '@angular/core';
|
||||||
import { ReleaseNote, VersionService } from '../../../../service/version.service';
|
import { ReleaseNote, VersionService } from '../../../../service/version.service';
|
||||||
|
|
||||||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
|
||||||
import showdown from 'showdown';
|
import showdown from 'showdown';
|
||||||
|
import DOMPurify from 'dompurify';
|
||||||
import {DatePipe} from '@angular/common';
|
import {DatePipe} from '@angular/common';
|
||||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
@@ -22,7 +22,6 @@ import {TranslocoDirective} from '@jsverse/transloco';
|
|||||||
export class VersionChangelogDialogComponent implements OnInit {
|
export class VersionChangelogDialogComponent implements OnInit {
|
||||||
|
|
||||||
private versionService = inject(VersionService);
|
private versionService = inject(VersionService);
|
||||||
private sanitizer = inject(DomSanitizer);
|
|
||||||
dialogRef = inject(DynamicDialogRef);
|
dialogRef = inject(DynamicDialogRef);
|
||||||
|
|
||||||
changelog: ReleaseNote[] = [];
|
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);
|
let html = this.converter.makeHtml(markdown);
|
||||||
html = html.replace(/<h2\b([^>]*)>/g, '<h3$1>').replace(/<\/h2>/g, '</h3>');
|
html = html.replace(/<h2\b([^>]*)>/g, '<h3$1>').replace(/<\/h2>/g, '</h3>');
|
||||||
return this.sanitizer.bypassSecurityTrustHtml(html);
|
return DOMPurify.sanitize(html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user