| 1 | use std::ffi::OsString; |
| 2 | use std::fs; |
| 3 | use std::os::unix::fs::{PermissionsExt, symlink}; |
| 4 | use std::path::Path; |
| 5 | |
| 6 | use codewhale_plugins::activation::TestPolicyGuard; |
| 7 | use serde_json::json; |
| 8 | use tempfile::tempdir; |
| 9 | use wiremock::MockServer; |
| 10 | |
| 11 | use super::pandoc::PandocConvertTool; |
| 12 | use super::spec::{ToolContext, ToolError, ToolSpec}; |
| 13 | use super::speech::SpeechTool; |
| 14 | use super::speech::test_support::{config, provider}; |
| 15 | use crate::client::CodewhaleClient; |
| 16 | use crate::test_support::{EnvVarGuard, SealedHome, lock_test_env}; |
| 17 | |
| 18 | fn stub_pandoc_path(dir: &Path) -> OsString { |
| 19 | let stub = dir.join("pandoc"); |
| 20 | fs::write( |
| 21 | &stub, |
| 22 | "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then echo 'pandoc 3.1.9'; exit 0; fi\nsrc=\"$2\"\nout=\"\"\nwhile [ $# -gt 0 ]; do if [ \"$1\" = \"--output\" ]; then out=\"$2\"; fi; shift; done\nif [ -n \"$out\" ]; then cat \"$src\" > \"$out\"; else cat \"$src\"; fi\n", |
| 23 | ) |
| 24 | .expect("stub"); |
| 25 | fs::set_permissions(&stub, fs::Permissions::from_mode(0o755)).expect("chmod"); |
| 26 | std::env::join_paths( |
| 27 | std::iter::once(dir.to_path_buf()).chain(std::env::split_paths( |
| 28 | &std::env::var_os("PATH").unwrap_or_default(), |
| 29 | )), |
| 30 | ) |
| 31 | .expect("path") |
| 32 | } |
| 33 | |
| 34 | fn is_link(path: &Path) -> bool { |
| 35 | fs::symlink_metadata(path) |
| 36 | .expect("metadata") |
| 37 | .file_type() |
| 38 | .is_symlink() |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn a_dangling_link_leaving_the_workspace_is_refused() { |
| 43 | let ws = tempdir().unwrap(); |
| 44 | let outside = tempdir().unwrap(); |
| 45 | let ctx = ToolContext::new(ws.path().to_path_buf()); |
| 46 | |
| 47 | symlink( |
| 48 | outside.path().join("missing.txt"), |
| 49 | ws.path().join("dangling.txt"), |
| 50 | ) |
| 51 | .unwrap(); |
| 52 | symlink(outside.path().join("hop.txt"), ws.path().join("hop2")).unwrap(); |
| 53 | symlink(ws.path().join("hop2"), ws.path().join("hop1")).unwrap(); |
| 54 | symlink( |
| 55 | Path::new("../codewhale-missing-target"), |
| 56 | ws.path().join("climb"), |
| 57 | ) |
| 58 | .unwrap(); |
| 59 | symlink(outside.path().join("nodir"), ws.path().join("dirlink")).unwrap(); |
| 60 | |
| 61 | for raw in ["dangling.txt", "hop1", "climb", "dirlink/new.txt"] { |
| 62 | let err = ctx.resolve_path(raw).expect_err(raw); |
| 63 | assert!( |
| 64 | matches!(err, ToolError::PathEscape { .. }), |
| 65 | "{raw}: {err:?}" |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | symlink("selfloop", ws.path().join("selfloop")).unwrap(); |
| 70 | ctx.resolve_path("selfloop").expect_err("a link loop"); |
| 71 | |
| 72 | let outer = tempdir().unwrap(); |
| 73 | let nested_ws = outer.path().join("outer").join("ws"); |
| 74 | fs::create_dir_all(&nested_ws).unwrap(); |
| 75 | let nested_ctx = ToolContext::new(nested_ws.clone()); |
| 76 | symlink("..", nested_ws.join("up")).unwrap(); |
| 77 | symlink("up/../escaped.txt", nested_ws.join("out.txt")).unwrap(); |
| 78 | let err = nested_ctx |
| 79 | .resolve_path("out.txt") |
| 80 | .expect_err("parent traversal after symlink must not resolve inside workspace"); |
| 81 | assert!(matches!(err, ToolError::PathEscape { .. }), "{err:?}"); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn a_dangling_link_inside_the_workspace_still_resolves() { |
| 86 | let ws = tempdir().unwrap(); |
| 87 | let ctx = ToolContext::new(ws.path().to_path_buf()); |
| 88 | let canonical = ws.path().canonicalize().unwrap(); |
| 89 | |
| 90 | symlink(ws.path().join("later.txt"), ws.path().join("absolute")).unwrap(); |
| 91 | symlink("sub/later.txt", ws.path().join("relative")).unwrap(); |
| 92 | |
| 93 | for name in ["absolute", "relative"] { |
| 94 | assert_eq!( |
| 95 | ctx.resolve_path(name).expect(name), |
| 96 | canonical.join(name), |
| 97 | "{name}" |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[tokio::test(flavor = "current_thread")] |
| 103 | async fn pandoc_output_is_published_without_writing_through_a_link() { |
| 104 | let _env = lock_test_env(); |
| 105 | let bin = tempdir().unwrap(); |
| 106 | let _path = EnvVarGuard::set("PATH", stub_pandoc_path(bin.path())); |
| 107 | let ws = tempdir().unwrap(); |
| 108 | let outside = tempdir().unwrap(); |
| 109 | fs::write(ws.path().join("in.md"), "PAYLOAD\n").unwrap(); |
| 110 | let ctx = ToolContext::new(ws.path().to_path_buf()); |
| 111 | let convert = |output: &str| json!({"source_path": "in.md", "target_format": "plain", "output_path": output}); |
| 112 | |
| 113 | symlink( |
| 114 | outside.path().join("created.txt"), |
| 115 | ws.path().join("dangling.txt"), |
| 116 | ) |
| 117 | .unwrap(); |
| 118 | let err = PandocConvertTool |
| 119 | .execute(convert("dangling.txt"), &ctx) |
| 120 | .await |
| 121 | .expect_err("a dangling link out of the workspace"); |
| 122 | assert!(matches!(err, ToolError::PathEscape { .. }), "{err:?}"); |
| 123 | assert!(!outside.path().join("created.txt").exists()); |
| 124 | |
| 125 | symlink(ws.path().join("later.txt"), ws.path().join("alias.txt")).unwrap(); |
| 126 | PandocConvertTool |
| 127 | .execute(convert("alias.txt"), &ctx) |
| 128 | .await |
| 129 | .expect("a dangling link inside the workspace"); |
| 130 | assert!(!is_link(&ws.path().join("alias.txt"))); |
| 131 | assert_eq!( |
| 132 | fs::read_to_string(ws.path().join("alias.txt")).unwrap(), |
| 133 | "PAYLOAD\n" |
| 134 | ); |
| 135 | assert!(!ws.path().join("later.txt").exists()); |
| 136 | |
| 137 | fs::write(outside.path().join("victim.txt"), "keep").unwrap(); |
| 138 | fs::hard_link( |
| 139 | outside.path().join("victim.txt"), |
| 140 | ws.path().join("hard.txt"), |
| 141 | ) |
| 142 | .unwrap(); |
| 143 | PandocConvertTool |
| 144 | .execute(convert("hard.txt"), &ctx) |
| 145 | .await |
| 146 | .expect_err("a hard-linked output"); |
| 147 | assert_eq!( |
| 148 | fs::read_to_string(outside.path().join("victim.txt")).unwrap(), |
| 149 | "keep" |
| 150 | ); |
| 151 | |
| 152 | let err = PandocConvertTool |
| 153 | .execute( |
| 154 | json!({ |
| 155 | "source_path": "in.md", |
| 156 | "target_format": "plain", |
| 157 | "output_path": "spare.txt", |
| 158 | "path": "other.txt" |
| 159 | }), |
| 160 | &ctx, |
| 161 | ) |
| 162 | .await |
| 163 | .expect_err("an unexpected field"); |
| 164 | assert!(matches!(err, ToolError::InvalidInput { .. }), "{err:?}"); |
| 165 | assert!(!ws.path().join("spare.txt").exists()); |
| 166 | |
| 167 | PandocConvertTool |
| 168 | .execute(convert("plain.txt"), &ctx) |
| 169 | .await |
| 170 | .expect("an ordinary output"); |
| 171 | assert_eq!( |
| 172 | fs::read_to_string(ws.path().join("plain.txt")).unwrap(), |
| 173 | "PAYLOAD\n" |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[tokio::test(flavor = "current_thread")] |
| 178 | async fn speech_output_is_published_without_writing_through_a_link() { |
| 179 | let _home = SealedHome::new(); |
| 180 | let _policy = TestPolicyGuard::extension_host(false); |
| 181 | let ws = tempdir().unwrap(); |
| 182 | let outside = tempdir().unwrap(); |
| 183 | let server = MockServer::start().await; |
| 184 | let client = CodewhaleClient::new(&config(&server.uri(), false)).unwrap(); |
| 185 | let tool = SpeechTool::new(Some(client), None); |
| 186 | let ctx = ToolContext::new(ws.path().to_path_buf()); |
| 187 | |
| 188 | symlink( |
| 189 | outside.path().join("created.wav"), |
| 190 | ws.path().join("dangling.wav"), |
| 191 | ) |
| 192 | .unwrap(); |
| 193 | provider(&server, 0).await; |
| 194 | let err = tool |
| 195 | .execute(json!({"text": "hello", "output": "dangling.wav"}), &ctx) |
| 196 | .await |
| 197 | .expect_err("a dangling link out of the workspace"); |
| 198 | assert!(matches!(err, ToolError::PathEscape { .. }), "{err:?}"); |
| 199 | assert!(!outside.path().join("created.wav").exists()); |
| 200 | |
| 201 | fs::write(outside.path().join("victim.wav"), "keep").unwrap(); |
| 202 | symlink( |
| 203 | outside.path().join("victim.wav"), |
| 204 | ws.path().join("speech.wav"), |
| 205 | ) |
| 206 | .unwrap(); |
| 207 | server.reset().await; |
| 208 | provider(&server, 1).await; |
| 209 | tool.execute(json!({"text": "hello"}), &ctx) |
| 210 | .await |
| 211 | .expect("default name"); |
| 212 | assert_eq!( |
| 213 | fs::read_to_string(outside.path().join("victim.wav")).unwrap(), |
| 214 | "keep" |
| 215 | ); |
| 216 | assert!(!is_link(&ws.path().join("speech.wav"))); |
| 217 | assert_eq!(fs::read(ws.path().join("speech.wav")).unwrap(), b"hi"); |
| 218 | |
| 219 | fs::create_dir(ws.path().join("audio")).unwrap(); |
| 220 | fs::write(outside.path().join("victim2.wav"), "keep").unwrap(); |
| 221 | symlink( |
| 222 | outside.path().join("victim2.wav"), |
| 223 | ws.path().join("audio").join("speech.wav"), |
| 224 | ) |
| 225 | .unwrap(); |
| 226 | server.reset().await; |
| 227 | provider(&server, 1).await; |
| 228 | tool.execute(json!({"text": "hello", "output_dir": "audio"}), &ctx) |
| 229 | .await |
| 230 | .expect("output_dir default name"); |
| 231 | assert_eq!( |
| 232 | fs::read_to_string(outside.path().join("victim2.wav")).unwrap(), |
| 233 | "keep" |
| 234 | ); |
| 235 | assert_eq!( |
| 236 | fs::read(ws.path().join("audio").join("speech.wav")).unwrap(), |
| 237 | b"hi" |
| 238 | ); |
| 239 | |
| 240 | fs::write(outside.path().join("victim3.wav"), "keep").unwrap(); |
| 241 | fs::hard_link( |
| 242 | outside.path().join("victim3.wav"), |
| 243 | ws.path().join("hard.wav"), |
| 244 | ) |
| 245 | .unwrap(); |
| 246 | server.reset().await; |
| 247 | provider(&server, 1).await; |
| 248 | tool.execute(json!({"text": "hello", "output": "hard.wav"}), &ctx) |
| 249 | .await |
| 250 | .expect_err("a hard-linked output"); |
| 251 | assert_eq!( |
| 252 | fs::read_to_string(outside.path().join("victim3.wav")).unwrap(), |
| 253 | "keep" |
| 254 | ); |
| 255 | } |
| 256 |