Skip to content

Use RFC-compliant HeaderValue type. #287

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ jobs:

- name: Format (rules_rust)
run: |
sed -i'' -E 's/^default = \[\]/default = \[\"header-value\"\]/' Cargo.toml
bazelisk --noworkspace_rc run --noenable_bzlmod //bazel/cargo:crates_vendor
git checkout Cargo.toml
git diff --exit-code

msrv:
Expand Down Expand Up @@ -150,9 +152,18 @@ jobs:
- name: Clippy (wasm32-wasi)
run: cargo clippy --release --all-targets --target=wasm32-wasi

- name: Build (header-value)
run: cargo build --release --all-targets --target=wasm32-wasi --features header-value

- name: Clippy (header-value)
run: cargo clippy --release --all-targets --target=wasm32-wasi --features header-value

- name: Test
run: cargo test

- name: Test (header-value)
run: cargo test --features header-value

- name: Format (rustfmt)
run: cargo fmt -- --check

Expand Down Expand Up @@ -215,9 +226,18 @@ jobs:
- name: Clippy (wasm32-wasip1)
run: cargo clippy --release --all-targets --target=wasm32-wasip1

- name: Build (header-value)
run: cargo build --release --all-targets --target=wasm32-wasip1 --features header-value

- name: Clippy (header-value)
run: cargo clippy --release --all-targets --target=wasm32-wasip1 --features header-value

- name: Test
run: cargo test

- name: Test (header-value)
run: cargo test --features header-value

- name: Format (rustfmt)
run: cargo fmt -- --check

Expand Down Expand Up @@ -281,12 +301,24 @@ jobs:
- name: Clippy (wasm32-wasip1)
run: cargo clippy --release --all-targets --target=wasm32-wasip1

- name: Build (header-value)
run: cargo build --release --all-targets --target=wasm32-wasip1 --features header-value

- name: Clippy (header-value)
run: cargo clippy --release --all-targets --target=wasm32-wasip1 --features header-value

- name: Test
run: cargo test

- name: Test (header-value)
run: cargo test --features header-value

- name: Bench
run: cargo bench

- name: Bench (header-value)
run: cargo bench --features header-value

- name: Format (rustfmt)
run: cargo fmt -- --check

Expand Down
30 changes: 30 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ rust_library(
],
)

rust_library(
name = "proxy_wasm_header_value",
srcs = glob(["src/*.rs"]),
crate_features = ["header-value"],
crate_name = "proxy_wasm",
edition = "2018",
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_build_script",
"//bazel/cargo/remote:bytes",
"//bazel/cargo/remote:hashbrown",
"//bazel/cargo/remote:http",
"//bazel/cargo/remote:log",
],
)

rust_binary(
name = "http_auth_random",
srcs = ["examples/http_auth_random/src/lib.rs"],
Expand All @@ -52,3 +68,17 @@ rust_binary(
"//bazel/cargo/remote:log",
],
)

rust_binary(
name = "grpc_auth_random",
srcs = ["examples/grpc_auth_random/src/lib.rs"],
crate_type = "cdylib",
edition = "2018",
out_binary = True,
rustc_flags = ["-Cstrip=debuginfo"],
visibility = ["//visibility:private"],
deps = [
":proxy_wasm_header_value",
"//bazel/cargo/remote:log",
],
)
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ edition = "2018"
build = "build.rs"

[dependencies]
bytes = { version = "1", optional = true }
hashbrown = "0.15"
http = { version = "1", optional = true }
log = "0.4"

[features]
default = []
header-value = ["dep:bytes", "dep:http"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature could be renamed to valid-header, strict-header or rfc-header to make this reusable in case we want to add restrictions on header name in the future.

Alternatively, we could drop this as a feature and instead add those functions as variants with _typed() suffix.

Thoughts?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_typed() suffix would fit alongside the existing _bytes() suffix, so doesn't seem too bad to me.

No objection to keeping it as a feature, though, assuming we find a solution to the HTTP/2 pseudo-headers problem. If you keep it a feature, strict-header or rfc-header SGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_typed() suffix would fit alongside the existing _bytes() suffix, so doesn't seem too bad to me.

Since the original version without suffix is fundamentally broken for the HTTP use case, I think we should drop it sooner rather than later, so I'll stick to the version from the existing revision of this PR and leave it without suffix behind a feature flag.

No objection to keeping it as a feature, though, assuming we find a solution to the HTTP/2 pseudo-headers problem.

Pseudo-header issue is orthogonal to this, since it only affects HTTP header names, and not values (this PR).

If you keep it a feature, strict-header or rfc-header SGTM.

My original thinking was that we need to have a flag for both: values only (this PR) and for both, so that we could add support for http::HeaderName and http::HeaderMap in a separate PR without making it a breaking change.

Having said that, I suspect that adding support for the latter is going to require splitting pseudo-headers and regular HTTP headers into separate maps, so it might be a much bigger change that'd be done as part of a bigger rewrite and not something simply hidden behind a feature flag.

Using http::HeaderValue is much more important, since the UTF-8 issue is causing panics and is pretty much broken.

Using http::HeaderName and http::HeaderMap only gives us better integration with existing Rust's HTTP ecosystem, but it doesn't really fix any issues, so it's a nice-to-have.

So strict-header-value (this PR) and strict-header (once we have support for both)?


[profile.release]
lto = true
opt-level = 3
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
[license-badge]: https://img.shields.io/github/license/proxy-wasm/proxy-wasm-rust-sdk
[license-link]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/blob/main/LICENSE

## Crate features

This crate supports the following optional features:

- `header-value` - uses RFC-compliant `HeaderValue` instead of UTF-8 `String` for HTTP header and trailer values.
This will become the default in future releases.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this PR doesn't introduce any changes or restrictions on header names in the SDK, since all valid HTTP field names are also valid UTF-8 strings, and proxies are very strict about this, so we never panic deserializing header names.

Having said that, I'm tempted to use HeaderName type while we're here, and turn this into a full typed variant.

Unfortunately, because we're passing HTTP/2 pseudo-headers (:authority, :status, etc.) in the header map, those names are non-compliant with RFC HTTP field names, so we'd need to either:

  1. create wrapper around http::HeaderName type,
  2. create our own more forgiving type,
  3. return a pair of maps, one with pseudo-headers and one with HTTP headers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that http::HeaderName can't accomodate pseudo-headers, since that's a very common case. What does other Rust code do for handling H2 pseudo-headers, just use strings?

If there isn't any good external precedent, maybe (1), in the form of an enum type that can either be an http::HeaderName or a pseudo-header? Or would http::HeaderName plus a pseudoheader bool be sufficient to represent all cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that http::HeaderName can't accomodate pseudo-headers, since that's a very common case. What does other Rust code do for handling H2 pseudo-headers, just use strings?

Pseudo-headers are a wire-format detail, and they are not intermixed with HTTP headers in any library or language that I'm aware of (other than Envoy), so that's not an issue that other people run into.

If there isn't any good external precedent, maybe (1), in the form of an enum type that can either be an http::HeaderName or a pseudo-header? Or would http::HeaderName plus a pseudoheader bool be sufficient to represent all cases?

http::HeaderName cannot contain : character, so a pseudoheader flag wouldn't work.

My other concern was that even if we "patch" http::HeaderName somehow, we still wouldn't be able to use http::HeaderMap that uses original http::HeaderName as long as pseudo-headers are intermixed with the regular HTTP headers.

I think we should separate them, at the very least at the SDK level, otherwise we're just propagating this Envoy-ism and making it hard-to-impossible to interoperate with the generic HTTP libraries available in various languages.

I'm sure this isn't helpful in Go SDK either.


## Examples

- [Hello World](./examples/hello_world/)
Expand Down
31 changes: 31 additions & 0 deletions bazel/cargo/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"

[[package]]
name = "bytes"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"

[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"

[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "foldhash"
version = "0.1.5"
Expand All @@ -31,6 +43,23 @@ dependencies = [
"foldhash",
]

[[package]]
name = "http"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565"
dependencies = [
"bytes",
"fnv",
"itoa",
]

[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"

[[package]]
name = "log"
version = "0.4.27"
Expand All @@ -41,6 +70,8 @@ checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
name = "proxy-wasm"
version = "0.2.4-dev"
dependencies = [
"bytes",
"hashbrown",
"http",
"log",
]
24 changes: 24 additions & 0 deletions bazel/cargo/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ filegroup(
)

# Workspace Member Dependencies
alias(
name = "bytes-1.10.1",
actual = "@crates_vendor__bytes-1.10.1//:bytes",
tags = ["manual"],
)

alias(
name = "bytes",
actual = "@crates_vendor__bytes-1.10.1//:bytes",
tags = ["manual"],
)

alias(
name = "hashbrown-0.15.3",
actual = "@crates_vendor__hashbrown-0.15.3//:hashbrown",
Expand All @@ -43,6 +55,18 @@ alias(
tags = ["manual"],
)

alias(
name = "http-1.3.1",
actual = "@crates_vendor__http-1.3.1//:http",
tags = ["manual"],
)

alias(
name = "http",
actual = "@crates_vendor__http-1.3.1//:http",
tags = ["manual"],
)

alias(
name = "log-0.4.27",
actual = "@crates_vendor__log-0.4.27//:log",
Expand Down
96 changes: 96 additions & 0 deletions bazel/cargo/remote/BUILD.bytes-1.10.1.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//bazel/cargo:crates_vendor
###############################################################################

load("@rules_rust//cargo:defs.bzl", "cargo_toml_env_vars")
load("@rules_rust//rust:defs.bzl", "rust_library")

package(default_visibility = ["//visibility:public"])

cargo_toml_env_vars(
name = "cargo_toml_env_vars",
src = "Cargo.toml",
)

rust_library(
name = "bytes",
srcs = glob(
include = ["**/*.rs"],
allow_empty = True,
),
compile_data = glob(
include = ["**"],
allow_empty = True,
exclude = [
"**/* *",
".tmp_git_root/**/*",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
],
),
crate_features = [
"default",
"std",
],
crate_root = "src/lib.rs",
edition = "2018",
rustc_env_files = [
":cargo_toml_env_vars",
],
rustc_flags = [
"--cap-lints=allow",
],
tags = [
"cargo-bazel",
"crate-name=bytes",
"manual",
"noclippy",
"norustfmt",
],
target_compatible_with = select({
"@rules_rust//rust/platform:aarch64-apple-darwin": [],
"@rules_rust//rust/platform:aarch64-apple-ios": [],
"@rules_rust//rust/platform:aarch64-apple-ios-sim": [],
"@rules_rust//rust/platform:aarch64-linux-android": [],
"@rules_rust//rust/platform:aarch64-pc-windows-msvc": [],
"@rules_rust//rust/platform:aarch64-unknown-fuchsia": [],
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [],
"@rules_rust//rust/platform:aarch64-unknown-nixos-gnu": [],
"@rules_rust//rust/platform:aarch64-unknown-nto-qnx710": [],
"@rules_rust//rust/platform:aarch64-unknown-uefi": [],
"@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [],
"@rules_rust//rust/platform:armv7-linux-androideabi": [],
"@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [],
"@rules_rust//rust/platform:i686-apple-darwin": [],
"@rules_rust//rust/platform:i686-linux-android": [],
"@rules_rust//rust/platform:i686-pc-windows-msvc": [],
"@rules_rust//rust/platform:i686-unknown-freebsd": [],
"@rules_rust//rust/platform:i686-unknown-linux-gnu": [],
"@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [],
"@rules_rust//rust/platform:riscv32imc-unknown-none-elf": [],
"@rules_rust//rust/platform:riscv64gc-unknown-none-elf": [],
"@rules_rust//rust/platform:s390x-unknown-linux-gnu": [],
"@rules_rust//rust/platform:thumbv7em-none-eabi": [],
"@rules_rust//rust/platform:thumbv8m.main-none-eabi": [],
"@rules_rust//rust/platform:wasm32-unknown-unknown": [],
"@rules_rust//rust/platform:wasm32-wasip1": [],
"@rules_rust//rust/platform:x86_64-apple-darwin": [],
"@rules_rust//rust/platform:x86_64-apple-ios": [],
"@rules_rust//rust/platform:x86_64-linux-android": [],
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": [],
"@rules_rust//rust/platform:x86_64-unknown-freebsd": [],
"@rules_rust//rust/platform:x86_64-unknown-fuchsia": [],
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [],
"@rules_rust//rust/platform:x86_64-unknown-nixos-gnu": [],
"@rules_rust//rust/platform:x86_64-unknown-none": [],
"@rules_rust//rust/platform:x86_64-unknown-uefi": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
version = "1.10.1",
)
Loading
Loading