Skip to content

Commit

Permalink
[POC] Use wasm build of package-spec for package validation
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Mar 18, 2022
1 parent bbb531a commit 921353f
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 0 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/services/epm/archive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getBufferExtractor } from './extract';
export * from './cache';
export { getBufferExtractor, untarBuffer, unzipBuffer } from './extract';
export { generatePackageInfoFromArchiveBuffer } from './parse';
export { validateZipBufferWithPackageSpec } from './validation/validate';

export interface ArchiveEntry {
path: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import fs from 'fs';
import path from 'path';

// Provides global.Go runtime
import './wasm_exec';
// @ts-expect-error
const go = new Go();

const wasmBuffer = fs.readFileSync(path.join(__dirname, 'validator.wasm'));
/**
* Validates a zip archive using the package-spec
*/
export async function validateZipBufferWithPackageSpec(
name: string,
size: number,
buffer: Uint8Array
): Promise<void> {
const validator = await WebAssembly.instantiate(wasmBuffer, go.importObject);
go.run(validator.instance);

try {
// @ts-expect-error
await global.elasticPackageSpec.validateFromZipReader(name, size, buffer);
} finally {
// @ts-expect-error
global.elasticPackageSpec.stop();
}
}
Binary file not shown.
Loading

0 comments on commit 921353f

Please sign in to comment.