-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecure.rs
More file actions
410 lines (371 loc) · 14.1 KB
/
Copy pathsecure.rs
File metadata and controls
410 lines (371 loc) · 14.1 KB
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
//! Validated value-object newtypes ("parse, don't validate").
//!
//! This module provides small newtypes that wrap a `String` and can **only** be
//! constructed by running a structural validator from [`crate::validate`]. Once
//! a value has one of these types, it is guaranteed to have passed the relevant
//! path / identifier / ref security checks — so a safe-output tool that types a
//! field as, say, [`RelativeSafePath`] cannot hold an unvalidated, attacker
//! controlled path.
//!
//! Each newtype:
//!
//! - exposes a fallible [`parse`](RelativeSafePath::parse) constructor plus
//! `TryFrom<String>` / `TryFrom<&str>` / [`FromStr`](std::str::FromStr);
//! - validates **at deserialization time** via a hand-written `Deserialize`
//! impl, so invalid JSON/YAML input is rejected before any `validate()` pass;
//! - serializes transparently back to a plain string (`Serialize`) and reports a
//! plain string JSON schema (`JsonSchema`) so they are drop-in replacements for
//! `String` fields in MCP `Params` structs;
//! - derefs to `&str` and implements `AsRef<str>` / `AsRef<Path>` / `Display`.
//!
//! ## Choosing a type
//!
//! - [`RelativeSafePath`] — agent-supplied file paths inside the workspace
//! (allows hidden dotfiles like `.gitignore`; rejects traversal/absolute/`.git`).
//! - [`StrictRelativePath`] — like the above but every component must be a plain
//! segment (no leading `.`, no `:`); use for attachment/artifact staging.
//! - [`PathSegment`] — a single path segment / alias (e.g. a repo checkout alias).
//! - [`GitRefName`] — a git ref obeying `git check-ref-format`.
//! - [`BranchName`] — a git branch ref with extra length / leading-`-` / space rules.
//! - [`CommitSha`] — a full 40-character hex commit SHA.
//! - [`ArtifactName`] — an ADO artifact / attachment name.
//! - [`Identifier`] — an engine agent/model identifier.
//! - [`HostName`] — a DNS-style hostname.
//! - [`RegistryRef`] — a container-registry host or base path.
//! - [`Version`] — a version string (`1.2.3`, `latest`).
//!
//! New safe-output tools that accept paths or identifiers should type those
//! fields with these newtypes instead of raw `String` so the checks are applied
//! automatically and cannot be forgotten.
use std::path::Path;
use crate::validate;
/// Generate a validated string newtype ("parse, don't validate").
///
/// Expands to a `#[serde(transparent)]` tuple struct wrapping a `String` that
/// can only be constructed by running `$validate`, so an instance is a
/// type-level proof the value passed its structural security checks.
///
/// Parameters:
/// - `$name` — the generated newtype identifier.
/// - `$label` — a short noun (e.g. `"path"`, `"branch"`) interpolated into
/// validation error messages.
/// - `$validate` — an expression evaluating to a
/// `fn(&str, &str) -> anyhow::Result<()>` (value, label) that enforces the
/// rules; typically a function from [`crate::validate`].
///
/// Generated impls and why they exist:
/// - `Deserialize` (hand-written) runs `$validate`, so invalid JSON/YAML is
/// rejected at deserialization time rather than in a later `validate()` pass.
/// - `Serialize` / `JsonSchema` are transparent so the newtype is a drop-in
/// replacement for a `String` field in MCP `Params` (same wire/schema shape).
/// - `parse` / `TryFrom` / `FromStr` are the fallible constructors for code
/// paths that build values directly (e.g. tests, internal conversions).
/// - `Deref<Target=str>`, `AsRef<str>`, `AsRef<Path>`, `Display`, and
/// `From<$name> for String` make the validated value ergonomic to read.
macro_rules! validated_string {
(
$(#[$meta:meta])*
$name:ident, $label:literal, $validate:expr
) => {
$(#[$meta])*
#[derive(
Clone, Debug, PartialEq, Eq, Hash,
serde::Serialize, schemars::JsonSchema,
)]
#[serde(transparent)]
#[schemars(transparent)]
pub struct $name(String);
impl $name {
/// Parse and validate, returning the newtype on success.
pub fn parse(value: impl Into<String>) -> anyhow::Result<Self> {
let value = value.into();
let validate_fn: fn(&str, &str) -> anyhow::Result<()> = $validate;
validate_fn(&value, $label)?;
Ok(Self(value))
}
/// Borrow the validated value as a string slice.
pub fn as_str(&self) -> &str {
&self.0
}
/// Consume the newtype, returning the inner `String`.
pub fn into_inner(self) -> String {
self.0
}
}
impl std::ops::Deref for $name {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl AsRef<Path> for $name {
fn as_ref(&self) -> &Path {
Path::new(&self.0)
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<String> for $name {
type Error = anyhow::Error;
fn try_from(value: String) -> anyhow::Result<Self> {
Self::parse(value)
}
}
impl TryFrom<&str> for $name {
type Error = anyhow::Error;
fn try_from(value: &str) -> anyhow::Result<Self> {
Self::parse(value)
}
}
impl std::str::FromStr for $name {
type Err = anyhow::Error;
fn from_str(value: &str) -> anyhow::Result<Self> {
Self::parse(value)
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = String::deserialize(deserializer)?;
Self::parse(raw).map_err(serde::de::Error::custom)
}
}
impl From<$name> for String {
fn from(value: $name) -> String {
value.0
}
}
};
}
validated_string! {
/// A safe relative file path inside the workspace.
///
/// Allows hidden dotfiles (`.gitignore`); rejects empty, null bytes,
/// newlines, pipeline commands, absolute / Windows-drive paths, `..`
/// traversal, and any `.git` component. See
/// [`crate::validate::validate_relative_safe_path`].
RelativeSafePath, "path", validate::validate_relative_safe_path
}
validated_string! {
/// A safe relative path whose every component is a plain segment
/// (no leading `.`, no `:`). See
/// [`crate::validate::validate_relative_segment_path`].
StrictRelativePath, "path", validate::validate_relative_segment_path
}
validated_string! {
/// A single safe path segment / alias (e.g. a repository checkout alias).
PathSegment, "segment", |value: &str, label: &str| {
if validate::is_safe_path_segment(value) {
Ok(())
} else {
anyhow::bail!(
"{label} '{value}' is not a safe path segment \
(no empty, '..', path separators, or leading '.')"
)
}
}
}
validated_string! {
/// A git ref name obeying `git check-ref-format`.
GitRefName, "ref", validate::validate_git_ref_name
}
validated_string! {
/// A git branch ref with additional length / leading-`-` / space rules.
BranchName, "branch_name", |value: &str, label: &str| {
if value.is_empty() {
anyhow::bail!("{label} must not be empty");
}
if value.len() > 200 {
anyhow::bail!("{label} must be at most 200 characters");
}
if value.contains('\0') {
anyhow::bail!("{label} must not contain null bytes");
}
if value.starts_with('-') {
anyhow::bail!("{label} must not start with '-'");
}
if value.contains(' ') {
anyhow::bail!("{label} must not contain spaces");
}
validate::validate_git_ref_name(value, label)
}
}
validated_string! {
/// A full 40-character hex commit SHA.
CommitSha, "commit", validate::validate_commit_sha
}
validated_string! {
/// An Azure DevOps artifact / attachment name.
ArtifactName, "artifact_name", |value: &str, label: &str| {
if value.len() > 100 {
anyhow::bail!("{label} must be at most 100 characters");
}
if value.starts_with('.') {
anyhow::bail!("{label} must not start with '.'");
}
if validate::is_valid_artifact_name(value) {
Ok(())
} else {
anyhow::bail!(
"{label} must be non-empty and contain only alphanumeric characters, '-', '_' or '.'"
)
}
}
}
validated_string! {
/// An engine agent / model identifier.
Identifier, "identifier", |value: &str, label: &str| {
if validate::is_valid_identifier(value) {
Ok(())
} else {
anyhow::bail!(
"{label} '{value}' must be non-empty and contain only [A-Za-z0-9._:-]"
)
}
}
}
validated_string! {
/// A DNS-style hostname.
HostName, "hostname", |value: &str, label: &str| {
if validate::is_valid_hostname(value) {
Ok(())
} else {
anyhow::bail!("{label} '{value}' must be non-empty and contain only [A-Za-z0-9.-]")
}
}
}
validated_string! {
/// A version string (e.g. `1.2.3`, `latest`).
Version, "version", |value: &str, label: &str| {
if validate::is_valid_version(value) {
Ok(())
} else {
anyhow::bail!("{label} '{value}' must be non-empty and contain only [A-Za-z0-9._-]")
}
}
}
validated_string! {
/// An Azure DevOps Artifacts feed reference (`feed` or `project/feed`).
FeedRef, "feed", |value: &str, label: &str| {
if validate::is_valid_feed_ref(value) {
Ok(())
} else {
anyhow::bail!(
"{label} '{value}' must be a feed name or 'project/feed' \
containing only [A-Za-z0-9._/-] (no '..', no leading/trailing '/')"
)
}
}
}
validated_string! {
/// An internal container-registry base path (host plus optional
/// namespace path, e.g. `myacr.azurecr.io/mirror`).
RegistryRef, "registry", |value: &str, label: &str| {
if validate::is_valid_registry_ref(value) {
Ok(())
} else {
anyhow::bail!(
"{label} '{value}' must be a registry host or base path \
containing only [A-Za-z0-9._/-] (no '..', no '//', no \
leading/trailing '/')"
)
}
}
}
validated_string! {
/// An Azure DevOps service-connection name or GUID.
ServiceConnection, "service-connection", |value: &str, label: &str| {
if validate::is_valid_service_connection(value) {
Ok(())
} else {
anyhow::bail!(
"{label} '{value}' must be non-empty, at most 256 characters, \
and free of quotes and control characters"
)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relative_safe_path_parses_and_rejects() {
assert!(RelativeSafePath::parse("src/main.rs").is_ok());
assert!(RelativeSafePath::parse(".gitignore").is_ok());
assert!(RelativeSafePath::parse("../escape").is_err());
assert!(RelativeSafePath::parse(".git/config").is_err());
assert_eq!(
RelativeSafePath::parse("a/b.txt").unwrap().as_str(),
"a/b.txt"
);
}
#[test]
fn strict_relative_path_is_stricter() {
assert!(StrictRelativePath::parse("a/b.txt").is_ok());
assert!(StrictRelativePath::parse(".hidden").is_err());
assert!(StrictRelativePath::parse("a:b").is_err());
}
#[test]
fn path_segment_rejects_separators() {
assert!(PathSegment::parse("my-repo").is_ok());
assert!(PathSegment::parse("a/b").is_err());
assert!(PathSegment::parse("..").is_err());
assert!(PathSegment::parse(".hidden").is_err());
}
#[test]
fn branch_name_rules() {
assert!(BranchName::parse("feature/x").is_ok());
assert!(BranchName::parse("-bad").is_err());
assert!(BranchName::parse("has space").is_err());
assert!(BranchName::parse("a..b").is_err());
assert!(BranchName::parse("x".repeat(201).as_str()).is_err());
}
#[test]
fn commit_sha_rules() {
assert!(CommitSha::parse("0123456789abcdef0123456789abcdef01234567").is_ok());
assert!(CommitSha::parse("short").is_err());
}
#[test]
fn artifact_name_rules() {
assert!(ArtifactName::parse("build-logs_1.0").is_ok());
assert!(ArtifactName::parse(".hidden").is_err());
assert!(ArtifactName::parse("has space").is_err());
assert!(ArtifactName::parse("a".repeat(101).as_str()).is_err());
}
#[test]
fn deserialize_validates() {
// Valid value round-trips.
let ok: RelativeSafePath = serde_json::from_str("\"src/lib.rs\"").unwrap();
assert_eq!(ok.as_str(), "src/lib.rs");
// Invalid value fails at deserialize time.
let err = serde_json::from_str::<RelativeSafePath>("\"../escape\"");
assert!(err.is_err());
}
#[test]
fn serialize_is_transparent() {
let p = RelativeSafePath::parse("a/b.txt").unwrap();
assert_eq!(serde_json::to_string(&p).unwrap(), "\"a/b.txt\"");
}
#[test]
fn json_schema_is_string() {
let schema = schemars::schema_for!(RelativeSafePath);
let json = serde_json::to_value(&schema).unwrap();
assert_eq!(json.get("type").and_then(|t| t.as_str()), Some("string"));
}
#[test]
fn usable_as_path() {
let p = RelativeSafePath::parse("a/b.txt").unwrap();
let joined = Path::new("/base").join(&p);
assert_eq!(joined, Path::new("/base/a/b.txt"));
}
}