forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

api.rs136 lines, 4.8 KiBHistoryRawDownload
1//! Which REST paths an AI service may read.
2//!
3//! Deny-by-default: a request the bridge dispatches into the router has to
4//! match one of [`AI_API_ALLOWLIST`]'s patterns exactly, segment for segment.
5//! There is no prefix match and no wildcard tail — `/notes` never admits
6//! `/notes/{id}/files/{file_id}` — so a new route family is unreachable to the
7//! services until somebody adds it to the list on purpose.
8//!
9//! Pure path logic, deliberately independent of the rest of `ai`: it is the
10//! access scope, and a scope that needed a connection to evaluate would be
11//! harder to test than to bypass.
12
13use crate::constant::AI_API_ALLOWLIST;
14
15/// Whether an AI service may `GET` this path.
16///
17/// The path is the router path alone: a query string travels in its own frame
18/// field, so a `?` here means the caller built the request wrong and the
19/// request is refused rather than silently matched on its prefix.
20pub fn path_allowed(path: &str) -> bool {
21 route_template(path).is_some()
22}
23
24/// The allowlist pattern this path matched — the route *template*
25/// (`/notes/{id}`), never the concrete path, which carries record ids. This is
26/// what the bridge puts on a span or a metric label; see [`crate::telemetry`]
27/// for why the concrete path may never leave the process.
28pub fn route_template(path: &str) -> Option<&'static str> {
29 if !path.starts_with('/') || path.contains('?') {
30 return None;
31 }
32 AI_API_ALLOWLIST
33 .iter()
34 .copied()
35 .find(|pattern| pattern_matches(pattern, path))
36}
37
38/// One `/`-separated pattern against one path. A literal segment must be
39/// equal; a `{name}` placeholder takes exactly one non-empty segment.
40fn pattern_matches(pattern: &str, path: &str) -> bool {
41 let mut expected = pattern[1..].split('/');
42 let mut actual = path[1..].split('/');
43 loop {
44 match (expected.next(), actual.next()) {
45 (None, None) => return true,
46 (Some(segment), Some(given)) => {
47 let ok = if segment.starts_with('{') && segment.ends_with('}') {
48 !given.is_empty()
49 } else {
50 segment == given
51 };
52 if !ok {
53 return false;
54 }
55 }
56 // Different segment counts: a trailing slash or a deeper path is a
57 // different route, never a longer match of this one.
58 _ => return false,
59 }
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn literal_path_matches_exactly() {
69 assert!(path_allowed("/auth/me"));
70 assert!(path_allowed("/notes"));
71 assert!(!path_allowed("/auth/m"));
72 assert!(!path_allowed("/AUTH/ME"));
73 }
74
75 #[test]
76 fn placeholder_takes_one_segment() {
77 assert!(path_allowed("/notes/note123"));
78 assert!(path_allowed("/users/user456/profile"));
79 // A placeholder is one segment, not a tail.
80 assert!(!path_allowed("/notes/note123/files"));
81 assert!(!path_allowed("/notes/note123/files/f1"));
82 }
83
84 #[test]
85 fn course_notes_are_readable_but_their_blobs_are_not() {
86 assert!(path_allowed("/course-notes"));
87 assert!(path_allowed("/course-notes/note123"));
88 // The listing of a note's files is JSON; the bytes behind one are not.
89 assert!(path_allowed("/course-notes/note123/files"));
90 assert!(!path_allowed("/course-notes/note123/files/f1"));
91 assert!(!path_allowed("/course-notes/"));
92 }
93
94 #[test]
95 fn trailing_slash_is_a_different_path() {
96 assert!(!path_allowed("/notes/"));
97 assert!(!path_allowed("/auth/me/"));
98 assert!(!path_allowed("/notes/note123/"));
99 }
100
101 #[test]
102 fn empty_segments_never_fill_a_placeholder() {
103 assert!(!path_allowed("/notes//"));
104 assert!(!path_allowed("//notes"));
105 assert!(!path_allowed("/users//profile"));
106 }
107
108 #[test]
109 fn the_template_is_the_pattern_not_the_path() {
110 assert_eq!(route_template("/notes/note123"), Some("/notes/{id}"));
111 assert_eq!(route_template("/auth/me"), Some("/auth/me"));
112 assert_eq!(
113 route_template("/users/user456/profile"),
114 Some("/users/{id}/profile")
115 );
116 assert_eq!(route_template("/users"), None);
117 }
118
119 #[test]
120 fn query_string_is_refused() {
121 assert!(!path_allowed("/notes?limit=10"));
122 assert!(!path_allowed("/notes/note123?x=1"));
123 }
124
125 #[test]
126 fn unlisted_and_malformed_paths_are_denied() {
127 assert!(!path_allowed(""));
128 assert!(!path_allowed("notes"));
129 assert!(!path_allowed("/"));
130 // Real routes that are deliberately out of scope.
131 assert!(!path_allowed("/users"));
132 assert!(!path_allowed("/users/user456/avatar"));
133 assert!(!path_allowed("/settings"));
134 assert!(!path_allowed("/exams"));
135 }
136}