This page provides tips for writing Service Extensions plugins that are correct,
perform well, and are well isolated. Correctness is critical because plugins run
in a constrained engine sandbox with a limited API surface. Performance is
critical because plugins run during end-user requests, with
[small amounts of resources](https://docs.cloud.google.com/service-extensions/docs/quotas.md.txt#limits). Isolation is
provided at the project level.

## Getting started

**Best practices**   
[Start from the samples](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#samples)  
[Use supported APIs](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#apis)  
[Run functional tests and benchmarks](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#testing)  

### Start from the samples

A good way to get started is browsing our [plugin code samples](https://github.com/GoogleCloudPlatform/service-extensions/tree/main/plugins#docs).
These are examples of common plugin patterns, such as path or query parsing,
header rewrites, custom logging, and custom authentication.

### Use supported APIs

Plugins must be compiled against the [Proxy-Wasm](https://github.com/proxy-wasm)
binary interface (ABI). Service Extensions supports a subset of the Proxy-Wasm ABI
that includes HTTP header and body mutations, local responses, custom logging,
and plugin configuration. Proxy-Wasm also supports a small subset of
[WASI preview 1](https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#modules),
including `stdout` and `stderr` for logging, `clock_time_get` and `random_get`.

Service Extensions doesn't support timers, custom metrics, shared data,
shared queues, or outbound network calls. Service Extensions also doesn't
support plugin callback return values that attempt to pause request processing,
and ignores them.

### Run functional tests and benchmarks

To evaluate correctness and performance, we provide a local plugin tester
tool that can run, test, and benchmark your plugin. You invoke this tool in
a Docker container, passing a plugin binary and a text proto of inputs and
expectations. See the [local tester documentation](https://github.com/GoogleCloudPlatform/service-extensions/tree/main/plugins#docs)
and [test input example](https://github.com/GoogleCloudPlatform/service-extensions/blob/main/plugins/samples/testing/tests.textpb).

## Correctness

**Best practices**   
[Don't rely on clocks](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#frozen-time)  
[Treat header names as case-insensitive](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#header-case)  

### Don't rely on clocks

For security reasons, clock time is set at context creation (for a plugin or a
request) and kept frozen during plugin invocations. This means that WebAssembly
plugins must not sleep; a sleep times out because time doesn't advance. It also
means that plugins can't measure their own execution time, though this
information is available in Cloud Monitoring.

### Treat header names as case-insensitive

According to [HTTP semantics](https://datatracker.ietf.org/doc/html/rfc9110#name-field-names),
HTTP header field names must be treated as case-insensitive. Header case written
by a plugin can be changed before being sent to clients or backends.

## Performance

**Best practices**   
[Avoid plugin crashes](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#no-crashes)  
[Compile for execution speed](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#compile)  
[Precompile regular expressions](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#precompile)  
[Avoid copying data into HTTP contexts](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#no-copies)  

### Avoid plugin crashes

When a plugin crashes, the triggering request receives an error. If this happens
frequently, plugin restarts are throttled, which leads to bursts of errors that
affect multiple users.

To avoid plugin crashes, try the following:

- Avoid assertions of all kinds. Instead, configure error logging or local responses to users.
- In Rust, avoid using methods, such as `unwrap`, that can result in panics. Additionally, you can configure the plugin to handle crashes by using `panic::set_hook`.
- Test your plugin by using the Google-provided [plugin tester](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#testing) and ensure that malformed or empty inputs are handled without the plugin crashing.

### Compile for execution speed

Compile your WebAssembly code to achieve the best execution speed by using
build option `-O3` (for C++) or `opt-level=3` (for Rust).

### Precompile regular expressions

Avoid compute-heavy operations on the per-request path (in HTTP handlers).
Instead, perform any work that's not request-specific at plugin configuration
time, and pass any precomputed state into each HTTP request context (also known
as stream context).

In particular, precompile any regular expressions at plugin configuration time.
Our [regex code sample](https://github.com/GoogleCloudPlatform/service-extensions/tree/main/plugins/samples/regex_rewrite)
shows how to achieve this.

### Avoid copying data into HTTP contexts

When sharing data between the root context and HTTP contexts, avoid expensive
copies or clone calls. Instead, share pointers or references. In C++, this can be
done with `std::shared_ptr` or raw pointers and references because the root
context outlives any HTTP context. In Rust, values can be shared by using
`std::rc::Rc`. In Go, values can be shared by storing pointers to the values,
which are removed by the garbage collector.

## Security

**Best practices**   
[Isolate workloads by project](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#project-isolation)  
[Restrict secrets on Media CDN](https://docs.cloud.google.com/service-extensions/docs/plugin-best-practices.md.txt#media-cdn)  

### Isolate workloads by project

On Google's infrastructure, plugins owned by the same Google Cloud project can
run in the same secure sandbox. This means that the WebAssembly runtime is the
only security barrier between plugins in the same project.

Use separate Google Cloud projects for workloads that need security separation.
This practice also provides separation of resources and permissions.

### Restrict secrets on Media CDN

By design, Media CDN runs on a fleet of hardware operated outside
of Google's physical control. When writing security-related plugins for
Media CDN, use dedicated hostnames or subdomains, and configure
plugins with signing keys that are rotated frequently.