Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api, frontend: autocheck file integrity #1940

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/src/service/domain/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export interface ExternalLinkReference {

export type DocumentOrExternalLinkReference = DocumentReference | ExternalLinkReference;

export type DocumentWithAvailability = DocumentOrExternalLinkReference & {
isValidHash?: boolean;
message?: string;
};

export const documentReferenceSchema = Joi.alternatives([
Joi.object({
id: Joi.string().required(),
Expand Down
33 changes: 30 additions & 3 deletions api/src/service/domain/workflow/workflowitem_get_details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NotAuthorized } from "../errors/not_authorized";
import { NotFound } from "../errors/not_found";
import { ServiceUser } from "../organization/service_user";
import * as Workflowitem from "./workflowitem";
import { DocumentReference } from "../document/document";

interface Repository {
getWorkflowitem(): Promise<Result.Type<Workflowitem.Workflowitem>>;
Expand Down Expand Up @@ -43,13 +44,39 @@ export async function getWorkflowitemDetails(
async function setDocumentAvailability(
documents: WorkflowitemDocument.DocumentOrExternalLinkReference[],
repository: Repository,
): Promise<WorkflowitemDocument.DocumentOrExternalLinkReference[]> {
const docsWithAvailability: WorkflowitemDocument.DocumentOrExternalLinkReference[] = [];
): Promise<WorkflowitemDocument.DocumentWithAvailability[]> {
const docsWithAvailability: WorkflowitemDocument.DocumentWithAvailability[] = [];

for (const doc of documents) {
const result = await repository.downloadDocument(doc.id);
docsWithAvailability.push({ ...doc, available: Result.isOk(result) });
if (Result.isOk(result) && isDocumentReference(doc)) {
const actualHash = await WorkflowitemDocument.hashBase64String(result.base64);
const isIdentical = isSameHash(doc, actualHash);
if (!isIdentical) {
logger.warn(
`Document ${doc.id} has a different hash than expected. Expected: ${
(doc as DocumentReference).hash
}, actual: ${actualHash}`,
);
}
docsWithAvailability.push({
...doc,
available: Result.isOk(result),
isValidHash: isIdentical,
});
} else {
docsWithAvailability.push({ ...doc, available: Result.isOk(result) });
}
}

return docsWithAvailability;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isDocumentReference(doc: any): doc is DocumentReference {
return doc && typeof doc.hash === "string";
}

function isSameHash(doc: DocumentReference, actualHash: string): boolean {
return doc.hash === actualHash;
}
6 changes: 3 additions & 3 deletions api/src/workflowitem_view_details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Ctx } from "./lib/ctx";
import { toUnixTimestampStr } from "./lib/datetime";
import { isNonemptyString } from "./lib/validation";
import * as Result from "./result";
import { DocumentOrExternalLinkReference } from "./service/domain/document/document";
import { DocumentWithAvailability } from "./service/domain/document/document";
import { ServiceUser } from "./service/domain/organization/service_user";
import * as Workflowitem from "./service/domain/workflow/workflowitem";
import Type from "./service/domain/workflowitem_types/types";
Expand Down Expand Up @@ -100,10 +100,10 @@ function mkSwaggerSchema(server: AugmentedFastifyInstance): Object {
id: {
type: "string",
example: "abc-cde-adf",
additionalProperties: true,
},
available: { type: "boolean", example: true },
},
additionalProperties: true,
},
},
},
Expand Down Expand Up @@ -137,7 +137,7 @@ interface ExposedWorkflowitem {
billingDate: string | null | undefined;
dueDate: string | null | undefined;
exchangeRate: string | null | undefined;
documents: DocumentOrExternalLinkReference[];
documents: DocumentWithAvailability[];
additionalData: object;
workflowitemType: Type | undefined;
};
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/pages/Documents/DocumentOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ class DocumentOverview extends Component {
const header = this.generateDocumentListHeader();
const rows = documents.map((document, index) => {
let validated = undefined;
const { id, fileName, hash } = document;
const { id, fileName, hash, isValidHash } = document;
const fingerPrintClassName =
isValidHash === false ? "finger-print-container invalid-hash" : "finger-print-container";
const fingerPrintText = isValidHash === false ? `Invalid hash ${hash}. File corrupt.` : hash;
validated = validatedDocuments[id];

return (
Expand All @@ -195,9 +198,9 @@ class DocumentOverview extends Component {
<div className="document-link">{document.link}</div>
</Tooltip>
) : (
<div className="finger-print-container">
<div className={fingerPrintClassName}>
<FingerPrint className="finger-print" />
<OverflowTooltip text={hash} maxWidth="4.375rem" />
<OverflowTooltip text={fingerPrintText} />
</div>
)}
</TableCell>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/pages/Documents/DocumentOverview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@
padding-right: 0.625rem;
padding-bottom: 0.625rem;
}

.invalid-hash {
color: red;
}
Loading