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

Allow allowedhosts only in background worker contexts #65

Merged
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
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
useWasi: true,
config: { thing: 'testing' },
allowedHosts: [...hosts],
runInWorker: true,
});

const res = await plugin.call(funcname, new TextEncoder().encode(input));
Expand Down
3 changes: 2 additions & 1 deletion examples/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ async function main() {
const plugin = await createPlugin(filename, {
useWasi: true,
config: { thing: 'testing' },
withAllowedHosts: ['*.typicode.com'],
allowedHosts: ['*.typicode.com'],
runInWorker: true
});

const res = await plugin.call(funcname, new TextEncoder().encode(input));
Expand Down
14 changes: 14 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ export interface ExtismPluginOptions {
*/
functions?: { [key: string]: { [key: string]: (callContext: CallContext, ...args: any[]) => any } } | undefined;
allowedPaths?: { [key: string]: string } | undefined;

/**
* A list of allowed hostnames. Wildcard subdomains are supported via `*`.
*
* Requires the plugin to run in a worker using `runInWorker: true`.
*
* @example
* ```ts
* await createPlugin('path/to/some/wasm', {
* runInWorker: true,
* allowedHosts: ['*.example.com', 'www.dylibso.com']
* })
* ```
*/
allowedHosts?: string[] | undefined;

/**
Expand Down
8 changes: 6 additions & 2 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ export async function createPlugin(
opts.enableWasiOutput ??= opts.useWasi ? CAPABILITIES.extismStdoutEnvVarSet : false;
opts.functions = opts.functions || {};
opts.allowedPaths ??= {};
// TODO(chrisdickinson): reset this to `CAPABILITIES.hasWorkerCapability` once we've fixed https://github.com/extism/js-sdk/issues/46.
opts.runInWorker ??= false;
if (opts.allowedHosts && !opts.runInWorker) {
throw new TypeError('"allowedHosts" requires "runInWorker: true". HTTP functions are only available to plugins running in a worker.')
}

opts.allowedHosts ??= <any>[].concat(opts.allowedHosts || []);
opts.logger ??= console;
opts.config ??= {};
opts.fetch ??= fetch;

// TODO(chrisdickinson): reset this to `CAPABILITIES.hasWorkerCapability` once we've fixed https://github.com/extism/js-sdk/issues/46.
opts.runInWorker ??= false;
if (opts.runInWorker && !CAPABILITIES.hasWorkerCapability) {
throw new Error(
'Cannot enable off-thread wasm; current context is not `crossOriginIsolated` (see https://mdn.io/crossOriginIsolated)',
Expand Down
Loading