Skip to content
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
33 changes: 33 additions & 0 deletions pkgs/id/crates/id-pg/src/allowlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ use serde::{Deserialize, Serialize};
/// (design §3.1: "default 500 rows").
pub const DEFAULT_MAX_LIMIT: u32 = 500;

/// Query-parameter names PostgREST reads as directives or logic
/// operators. A filter or order column with one of these names would be
/// emitted as a parameter key PostgREST interprets as something else.
pub const RESERVED_PARAM_NAMES: &[&str] = &[
"select",
"order",
"limit",
"offset",
"or",
"and",
"not",
"columns",
"on_conflict",
];

/// Whether `name` is a plain ASCII identifier (letters, digits,
/// underscore, not starting with a digit). Anything else would be
/// interpolated raw into a path or a query value.
pub fn is_plain_identifier(name: &str) -> bool {
let mut chars = name.chars();
match chars.next() {
Some(first) if first.is_ascii_alphabetic() || first == '_' => {}
_ => return false,
}
chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// Whether `name` may be used as a filter or order column: a plain
/// identifier that is not a reserved PostgREST parameter name.
pub fn is_usable_field_name(name: &str) -> bool {
is_plain_identifier(name) && !RESERVED_PARAM_NAMES.contains(&name)
}

/// One declared column of a collection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ColumnDecl {
Expand Down
79 changes: 75 additions & 4 deletions pkgs/id/crates/id-pg/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! root: method, target, count, pagination kind, boolean structure,
//! embeds, relation, fields, operators, values, cursor, limit.

use crate::allowlist::CollectionAllowlist;
use crate::allowlist::{CollectionAllowlist, is_plain_identifier, is_usable_field_name};
use crate::query::{
CountPref, Direction, FilterExpr, FilterLeaf, FilterOp, Method, OrderKey, Pagination, Query,
SelectItem, Target,
Expand Down Expand Up @@ -85,22 +85,27 @@ impl Translator {
}
// 7. The relation must be one the owner listed. Unknown and
// unlisted collapse to the same outcome.
if !is_plain_identifier(relation) {
return Err(Refusal::not_available());
}
let collection = self
.collections
.iter()
.find(|c| &c.relation == relation)
.ok_or_else(Refusal::not_available)?;
// 8. Fields: projection, filter, and order columns must be
// declared for the use they are put to. An undeclared column
// and a withheld column are the same refusal (§6.1).
// and a withheld column are the same refusal (§6.1). A filter
// column also becomes a query-parameter key, so a reserved
// PostgREST parameter name or a non-identifier is refused here.
let projection = resolve_projection(collection, &query.select)?;
for leaf in &leaves {
if !collection.is_filterable(&leaf.column) {
if !collection.is_filterable(&leaf.column) || !is_usable_field_name(&leaf.column) {
return Err(Refusal::shape(Feature::FieldUnsupported));
}
}
for key in &query.order {
if !collection.is_orderable(&key.column) {
if !collection.is_orderable(&key.column) || !is_usable_field_name(&key.column) {
return Err(Refusal::shape(Feature::FieldUnsupported));
}
}
Expand Down Expand Up @@ -417,6 +422,72 @@ mod tests {
assert_eq!(param(&request, "title"), Some("in.(\"a,b\",plain)"));
}

fn leaf_query(column: &str) -> Query {
let mut query = Query::read("items");
query.filter = Some(FilterExpr::Leaf(FilterLeaf {
column: column.to_string(),
op: FilterOp::Eq,
value: Value::Int(1),
}));
query
}

fn collection_with_columns(names: &[&str]) -> Translator {
let mut collection = collection();
collection.columns = names
.iter()
.map(|name| ColumnDecl {
name: name.to_string(),
filterable: true,
orderable: true,
})
.collect();
Translator::new(vec![collection])
}

#[test]
fn filter_on_reserved_parameter_name_is_refused() {
let translator = collection_with_columns(&["id", "or", "select"]);
for name in ["or", "select"] {
let refusal = translator.translate(&leaf_query(name)).unwrap_err();
assert_eq!(refusal, Refusal::shape(Feature::FieldUnsupported));
}
}

#[test]
fn order_on_reserved_parameter_name_is_refused() {
let translator = collection_with_columns(&["id", "limit"]);
let mut query = Query::read("items");
query.order = vec![OrderKey {
column: "limit".to_string(),
direction: Direction::Asc,
}];
let refusal = translator.translate(&query).unwrap_err();
assert_eq!(refusal, Refusal::shape(Feature::FieldUnsupported));
}

#[test]
fn filter_on_non_identifier_column_is_refused() {
let translator = collection_with_columns(&["id", "a,b"]);
let refusal = translator.translate(&leaf_query("a,b")).unwrap_err();
assert_eq!(refusal, Refusal::shape(Feature::FieldUnsupported));
}

#[test]
fn unsafe_relation_name_collapses_to_not_available() {
let mut collection = collection();
collection.relation = "items/x".to_string();
let translator = Translator::new(vec![collection]);
let refusal = translator.translate(&Query::read("items/x")).unwrap_err();
assert_eq!(refusal, Refusal::not_available());
}

#[test]
fn plain_filter_column_still_translates() {
let request = translator().translate(&leaf_query("title")).unwrap();
assert_eq!(param(&request, "title"), Some("eq.1"));
}

#[test]
fn is_null_renders_without_value() {
let mut query = Query::read("items");
Expand Down