Skip to main content

Crate graphql_strip_sensitive_literals

Crate graphql_strip_sensitive_literals 

Source
Expand description

Redact sensitive literals from a GraphQL document before logging or usage reporting.

GraphQL operations often inline user data as literal values. A query like user(email: "a@b.com") carries the email in the operation text. If that text is logged or sent to a usage-reporting service, the data leaks. This crate rewrites those literals to fixed placeholders so the operation shape stays useful while the data is gone.

strip_sensitive_literals returns a redacted copy of a parsed document. Scalar literals are replaced:

  • integers and floats become 0
  • strings become ""

With StripOptions::hide_list_and_object_literals set, list literals become [] and object literals become {}.

Everything else is left as it is. Field names, aliases, variable names and types, fragment names, directives, and the Boolean, Null, Enum, and variable values stay untouched. Booleans, nulls, and enums are not treated as sensitive.

The walk covers the whole document. Operations and fragments are redacted. Type-system definitions are too, so literals in field-argument defaults, input-field defaults, and directive arguments collapse the same way.

The best defense is to avoid inline literals. Pass user data through GraphQL variables so it never appears in the operation text. This crate is the backstop for operations that still inline data.

§apollo-compiler in the public API

The document type is apollo_compiler::ast::Document, re-exported here as Document. It appears in the signature of strip_sensitive_literals, so it is part of this crate’s public API. A caller builds input with Document::parse from apollo-compiler, which means the caller depends on apollo-compiler directly. The re-export names the boundary type so call sites can reach it through one path.

This couples the crate to the apollo-compiler major version. The dependency is pinned to 1.x. A consumer that passes a Document across this boundary must resolve to the same apollo-compiler major. An apollo-compiler 2.0 would change this crate’s public type and require a matching major bump here.

§Example

use apollo_compiler::ast::Document;
use graphql_strip_sensitive_literals::{strip_sensitive_literals, StripOptions};

let doc = Document::parse(
    r#"{ user(name: "Ada", age: 36) { id } }"#,
    "query.graphql",
)
.unwrap();

let redacted = strip_sensitive_literals(&doc, StripOptions::default());
assert_eq!(
    redacted.to_string().trim(),
    "{\n  user(name: \"\", age: 0) {\n    id\n  }\n}",
);

Structs§

Document
AST for a GraphQL Document that can contain executable definitions, type system (schema) definitions, or both.
StripOptions
Controls how aggressively literals are redacted.
StripOptionsBuilder
Builder for StripOptions.

Functions§

strip_sensitive_literals
Return a redacted copy of ast.