rocket/sentinel.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
use std::fmt;
use std::any::TypeId;
use crate::{Rocket, Ignite};
/// An automatic last line of defense against launching an invalid [`Rocket`].
///
/// A sentinel, automatically run on [`ignition`](Rocket::ignite()), can trigger
/// a launch abort should an instance fail to meet arbitrary conditions. Every
/// type that appears in a **mounted** route's type signature is eligible to be
/// a sentinel. Of these, those that implement `Sentinel` have their
/// [`abort()`](Sentinel::abort()) method invoked automatically, immediately
/// after ignition, once for each unique type. Sentinels inspect the finalized
/// instance of `Rocket` and can trigger a launch abort by returning `true`.
///
/// # Built-In Sentinels
///
/// The [`State<T>`] type is a sentinel that triggers an abort if the finalized
/// `Rocket` instance is not managing state for type `T`. Doing so prevents
/// run-time failures of the `State` request guard.
///
/// [`State<T>`]: crate::State
/// [`State`]: crate::State
///
/// ## Example
///
/// As an example, consider the following simple application:
///
/// ```rust
/// # use rocket::*;
/// # type Response = ();
/// #[get("/<id>")]
/// fn index(id: usize, state: &State<String>) -> Response {
/// /* ... */
/// }
///
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build().mount("/", routes![index])
/// }
///
/// # use rocket::{Config, error::ErrorKind};
/// # rocket::async_test(async {
/// # let result = rocket().configure(Config::debug_default()).ignite().await;
/// # assert!(matches!(result.unwrap_err().kind(), ErrorKind::SentinelAborts(..)));
/// # })
/// ```
///
/// At ignition time, effected by the `#[launch]` attribute here, Rocket probes
/// all types in all mounted routes for `Sentinel` implementations. In this
/// example, the types are: `usize`, `State<String>`, and `Response`. Those that
/// implement `Sentinel` are queried for an abort trigger via their
/// [`Sentinel::abort()`] method. In this example, the sentinel types are
/// [`State`] and _potentially_ `Response`, if it implements
/// `Sentinel`. If `abort()` returns true, launch is aborted with a
/// corresponding error.
///
/// In this example, launch will be aborted because state of type `String` is
/// not being managed. To correct the error and allow launching to proceed
/// nominally, a value of type `String` must be managed:
///
/// ```rust
/// # use rocket::*;
/// # type Response = ();
/// # #[get("/<id>")]
/// # fn index(id: usize, state: &State<String>) -> Response {
/// # /* ... */
/// # }
/// #
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build()
/// .mount("/", routes![index])
/// .manage(String::from("my managed string"))
/// }
///
/// # use rocket::{Config, error::ErrorKind};
/// # rocket::async_test(async {
/// # rocket().configure(Config::debug_default()).ignite().await.unwrap();
/// # })
/// ```
///
/// # Embedded Sentinels
///
/// Embedded types -- type parameters of already eligible types -- are also
/// eligible to be sentinels. Consider the following route:
///
/// ```rust
/// # use rocket::*;
/// # use either::Either;
/// # type Inner<T> = Option<T>;
/// # type Foo = ();
/// # type Bar = ();
/// #[get("/")]
/// fn f(guard: Option<&State<String>>) -> Either<Foo, Inner<Bar>> {
/// unimplemented!()
/// }
/// ```
///
/// The directly eligible sentinel types, guard and responders, are:
///
/// * `Option<&State<String>>`
/// * `Either<Foo, Inner<Bar>>`
///
/// In addition, all embedded types are _also_ eligible. These are:
///
/// * `&State<String>`
/// * `State<String>`
/// * `String`
/// * `Foo`
/// * `Inner<Bar>`
/// * `Bar`
///
/// A type, whether embedded or not, is queried if it is a `Sentinel` _and_ none
/// of its parent types are sentinels. Said a different way, if every _directly_
/// eligible type is viewed as the root of an acyclic graph with edges between a
/// type and its type parameters, the _first_ `Sentinel` in breadth-first order
/// is queried:
///
/// ```text
/// 1. Option<&State<String>> Either<Foo, Inner<Bar>>
/// | / \
/// 2. &State<String> Foo Inner<Bar>
/// | |
/// 3. State<String> Bar
/// |
/// 4. String
/// ```
///
/// In each graph above, types are queried from top to bottom, level 1 to 4.
/// Querying continues down paths where the parents were _not_ sentinels. For
/// example, if `Option` is a sentinel but `Either` is not, then querying stops
/// for the left subgraph (`Option`) but continues for the right subgraph
/// `Either`.
///
/// # Limitations
///
/// Because Rocket must know which `Sentinel` implementation to query based on
/// its _written_ type, generally only explicitly written, resolved, concrete
/// types are eligible to be sentinels. A typical application will only work
/// with such types, but there are several common cases to be aware of.
///
/// ## `impl Trait`
///
/// Occasionally an existential `impl Trait` may find its way into return types:
///
/// ```rust
/// # use rocket::*;
/// # use either::Either;
/// use rocket::response::Responder;
/// # type AnotherSentinel = ();
///
/// #[get("/")]
/// fn f<'r>() -> Either<impl Responder<'r, 'static>, AnotherSentinel> {
/// /* ... */
/// # Either::Left(())
/// }
/// ```
///
/// **Note:** _Rocket actively discourages using `impl Trait` in route
/// signatures. In addition to impeding sentinel discovery, doing so decreases
/// the ability to gleam a handler's functionality based on its type signature._
///
/// The return type of the route `f` depends on its implementation. At present,
/// it is not possible to name the underlying concrete type of an `impl Trait`
/// at compile-time and thus not possible to determine if it implements
/// `Sentinel`. As such, existentials _are not_ eligible to be sentinels.
///
/// That being said, this limitation only applies _per embedding_: types
/// embedded inside of an `impl Trait` _are_ eligible. As such, in the example
/// above, the named `AnotherSentinel` type continues to be eligible.
///
/// When possible, prefer to name all types:
///
/// ```rust
/// # use rocket::*;
/// # use either::Either;
/// # type AbortingSentinel = ();
/// # type AnotherSentinel = ();
/// #[get("/")]
/// fn f() -> Either<AbortingSentinel, AnotherSentinel> {
/// /* ... */
/// # unimplemented!()
/// }
/// ```
///
/// ## Aliases
///
/// _Embedded_ sentinels made opaque by a type alias will fail to be considered;
/// the aliased type itself _is_ considered. In the example below, only
/// `Result<Foo, Bar>` will be considered, while the embedded `Foo` and `Bar`
/// will not.
///
/// ```rust
/// # use rocket::get;
/// # type Foo = ();
/// # type Bar = ();
/// type SomeAlias = Result<Foo, Bar>;
///
/// #[get("/")]
/// fn f() -> SomeAlias {
/// /* ... */
/// # unimplemented!()
/// }
/// ```
///
/// Note, however, that `Option<T>` and [`Debug<T>`](crate::response::Debug) are
/// a sentinels if `T: Sentinel`, and `Result<T, E>` and `Either<T, E>` are
/// sentinels if _both_ `T: Sentinel, E: Sentinel`. Thus, for these specific
/// cases, a type alias _will_ "consider" embeddings. Nevertheless, prefer to
/// write concrete types when possible.
///
/// ## Type Macros
///
/// It is impossible to determine, a priori, what a type macro will expand to.
/// As such, Rocket is unable to determine which sentinels, if any, a type macro
/// references, and thus no sentinels are discovered from type macros.
///
/// Even approximations are impossible. For example, consider the following:
///
/// ```rust
/// # use rocket::*;
/// macro_rules! MyType {
/// (State<'_, u32>) => (&'_ rocket::Config)
/// }
///
/// #[get("/")]
/// fn f(guard: MyType![State<'_, u32>]) {
/// /* ... */
/// }
/// ```
///
/// While the `MyType![State<'_, u32>]` type _appears_ to contain a `State`
/// sentinel, the macro actually expands to `&'_ rocket::Config`, which is _not_
/// the `State` sentinel.
///
/// Because Rocket knows the exact syntax expected by type macros that it
/// exports, such as the [typed stream] macros, discovery in these macros works
/// as expected. You should prefer not to use type macros aside from those
/// exported by Rocket, or if necessary, restrict your use to those that always
/// expand to types without sentinels.
///
/// [typed stream]: crate::response::stream
///
/// # Custom Sentinels
///
/// Any type can implement `Sentinel`, and the implementation can arbitrarily
/// inspect an ignited instance of `Rocket`. For illustration, consider the
/// following implementation of `Sentinel` for a custom `Responder` which
/// requires:
///
/// * state for a type `T` to be managed
/// * a catcher for status code `400` at base `/`
///
/// ```rust
/// use rocket::{Rocket, Ignite, Sentinel};
/// # struct MyResponder;
/// # struct T;
///
/// impl Sentinel for MyResponder {
/// fn abort(rocket: &Rocket<Ignite>) -> bool {
/// if rocket.state::<T>().is_none() {
/// return true;
/// }
///
/// if !rocket.catchers().any(|c| c.code == Some(400) && c.base == "/") {
/// return true;
/// }
///
/// false
/// }
/// }
/// ```
///
/// If a `MyResponder` is returned by any mounted route, its `abort()` method
/// will be invoked. If the required conditions aren't met, signaled by
/// returning `true` from `abort()`, Rocket aborts launch.
pub trait Sentinel {
/// Returns `true` if launch should be aborted and `false` otherwise.
fn abort(rocket: &Rocket<Ignite>) -> bool;
}
impl<T: Sentinel> Sentinel for Option<T> {
fn abort(rocket: &Rocket<Ignite>) -> bool {
T::abort(rocket)
}
}
// In the next impls, we want to run _both_ sentinels _without_ short
// circuiting, for the logs. Ideally we could check if these are the same type
// or not, but `TypeId` only works with `'static`, and adding those bounds to
// `T` and `E` would reduce the types for which the implementations work, which
// would mean more types that we miss in type applies. When the type _isn't_ an
// alias, however, the existence of these implementations is strictly worse.
impl<T: Sentinel, E: Sentinel> Sentinel for Result<T, E> {
fn abort(rocket: &Rocket<Ignite>) -> bool {
let left = T::abort(rocket);
let right = E::abort(rocket);
left || right
}
}
impl<T: Sentinel, E: Sentinel> Sentinel for either::Either<T, E> {
fn abort(rocket: &Rocket<Ignite>) -> bool {
let left = T::abort(rocket);
let right = E::abort(rocket);
left || right
}
}
/// A sentinel that never aborts. The `Responder` impl for `Debug` will never be
/// called, so it's okay to not abort for failing `T: Sentinel`.
impl<T> Sentinel for crate::response::Debug<T> {
fn abort(_: &Rocket<Ignite>) -> bool {
false
}
}
/// The information resolved from a `T: ?Sentinel` by the `resolve!()` macro.
#[derive(Clone, Copy)]
pub struct Sentry {
/// The type ID of `T`.
pub type_id: TypeId,
/// The type name `T` as a string.
pub type_name: &'static str,
/// The type ID of type in which `T` is nested if not a top-level type.
pub parent: Option<TypeId>,
/// The source (file, column, line) location of the resolved `T`.
pub location: (&'static str, u32, u32),
/// The value of `<T as Sentinel>::SPECIALIZED` or the fallback.
///
/// This is `true` when `T: Sentinel` and `false` when `T: !Sentinel`.
pub specialized: bool,
/// The value of `<T as Sentinel>::abort` or the fallback.
pub abort: fn(&Rocket<Ignite>) -> bool,
}
/// Query `sentinels`, once for each unique `type_id`, returning an `Err` of all
/// of the sentinels that triggered an abort or `Ok(())` if none did.
pub(crate) fn query<'s>(
sentinels: impl Iterator<Item = &'s Sentry>,
rocket: &Rocket<Ignite>,
) -> Result<(), Vec<Sentry>> {
use std::collections::{HashMap, VecDeque};
// Build a graph of the sentinels.
let mut roots: VecDeque<&'s Sentry> = VecDeque::new();
let mut map: HashMap<TypeId, VecDeque<&'s Sentry>> = HashMap::new();
for sentinel in sentinels {
match sentinel.parent {
Some(parent) => map.entry(parent).or_default().push_back(sentinel),
None => roots.push_back(sentinel),
}
}
// Traverse the graph in breadth-first order. If we find a specialized
// sentinel, query it (once for a unique type) and don't traverse its
// children. Otherwise, traverse its children. Record queried aborts.
let mut remaining = roots;
let mut visited: HashMap<TypeId, bool> = HashMap::new();
let mut aborted = vec![];
while let Some(sentinel) = remaining.pop_front() {
if sentinel.specialized {
if *visited.entry(sentinel.type_id).or_insert_with(|| (sentinel.abort)(rocket)) {
aborted.push(sentinel);
}
} else if let Some(mut children) = map.remove(&sentinel.type_id) {
remaining.append(&mut children);
}
}
match aborted.is_empty() {
true => Ok(()),
false => Err(aborted.into_iter().cloned().collect())
}
}
impl fmt::Debug for Sentry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sentry")
.field("type_id", &self.type_id)
.field("type_name", &self.type_name)
.field("parent", &self.parent)
.field("location", &self.location)
.field("default", &self.specialized)
.finish()
}
}
/// Resolves a `T` to the specialized or fallback implementation of
/// `Sentinel`, returning a `Sentry` struct with the resolved items.
#[doc(hidden)]
#[macro_export]
macro_rules! resolve {
($T:ty $(, $P:ty)?) => ({
#[allow(unused_imports)]
use $crate::sentinel::resolution::{Resolve, DefaultSentinel as _};
$crate::sentinel::Sentry {
type_id: std::any::TypeId::of::<$T>(),
type_name: std::any::type_name::<$T>(),
parent: None $(.or(Some(std::any::TypeId::of::<$P>())))?,
location: (std::file!(), std::line!(), std::column!()),
specialized: Resolve::<$T>::SPECIALIZED,
abort: Resolve::<$T>::abort,
}
})
}
pub use resolve;
pub mod resolution {
use super::*;
/// The *magic*.
///
/// `Resolve<T>::item` for `T: Sentinel` is `<T as Sentinel>::item`.
/// `Resolve<T>::item` for `T: !Sentinel` is `DefaultSentinel::item`.
///
/// This _must_ be used as `Resolve::<T>:item` for resolution to work. This
/// is a fun, static dispatch hack for "specialization" that works because
/// Rust prefers inherent methods over blanket trait impl methods.
pub struct Resolve<T: ?Sized>(std::marker::PhantomData<T>);
/// Fallback trait "implementing" `Sentinel` for all types. This is what
/// Rust will resolve `Resolve<T>::item` to when `T: !Sentinel`.
pub trait DefaultSentinel {
const SPECIALIZED: bool = false;
fn abort(_: &Rocket<Ignite>) -> bool { false }
}
impl<T: ?Sized> DefaultSentinel for T {}
/// "Specialized" "implementation" of `Sentinel` for `T: Sentinel`. This is
/// what Rust will resolve `Resolve<T>::item` to when `T: Sentinel`.
impl<T: Sentinel + ?Sized> Resolve<T> {
pub const SPECIALIZED: bool = true;
pub fn abort(rocket: &Rocket<Ignite>) -> bool {
T::abort(rocket)
}
}
}
#[cfg(test)]
mod test {
use std::any::TypeId;
use crate::sentinel::resolve;
struct NotASentinel;
struct YesASentinel;
impl super::Sentinel for YesASentinel {
fn abort(_: &crate::Rocket<crate::Ignite>) -> bool {
unimplemented!()
}
}
#[test]
fn check_can_determine() {
let not_a_sentinel = resolve!(NotASentinel);
assert!(not_a_sentinel.type_name.ends_with("NotASentinel"));
assert!(!not_a_sentinel.specialized);
let yes_a_sentinel = resolve!(YesASentinel);
assert!(yes_a_sentinel.type_name.ends_with("YesASentinel"));
assert!(yes_a_sentinel.specialized);
}
struct HasSentinel<T>(T);
#[test]
fn parent_works() {
let child = resolve!(YesASentinel, HasSentinel<YesASentinel>);
assert!(child.type_name.ends_with("YesASentinel"));
assert_eq!(child.parent.unwrap(), TypeId::of::<HasSentinel<YesASentinel>>());
assert!(child.specialized);
let not_a_direct_sentinel = resolve!(HasSentinel<YesASentinel>);
assert!(not_a_direct_sentinel.type_name.contains("HasSentinel"));
assert!(not_a_direct_sentinel.type_name.contains("YesASentinel"));
assert!(not_a_direct_sentinel.parent.is_none());
assert!(!not_a_direct_sentinel.specialized);
}
}