╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
156 try sess.exit(1); 157 return; 158 } 159 160 const repo_opts: rp.RepoOpts(.xit) = .{}; 161 const Repo = rp.Repo(.xit, repo_opts); 162 const create_if_missing = parsed.service == .receive_pack; 163 164 var repo = openOrCreate(Repo, io, allocator, repo_path, create_if_missing) catch |err| switch (err) { 165 error.RepoNotFound => { 166 try writeError(sess, "repo not found"); 167 try sess.exit(1); 168 return; 169 }, 170 else => |e| return e, 171 }; 172 defer repo.deinit(io, allocator); 173 174 // wrap the channel I/O in std.Io.Reader/Writer adapters so the repo's 175 // pack functions can consume them like any other stream. 176 var session_reader_buf: [4096]u8 = undefined; 177 var session_writer_buf: [4096]u8 = undefined; 178 var session_reader = ssh.SessionReader.init(sess, &session_reader_buf); 179 var session_writer = ssh.SessionWriter.init(sess, &session_writer_buf); 180 181 switch (parsed.service) { 182 .upload_pack => try repo.uploadPack(io, allocator, &session_reader.interface, &session_writer.interface, .{}), 183 .receive_pack => try repo.receivePack(io, allocator, &session_reader.interface, &session_writer.interface, .{}), 184 } 185 186 // flush whatever the pack op buffered into our writer adapter 187 session_writer.interface.flush() catch {}; 188 189 try sess.exit(0); 190 } 191 192 fn openOrCreate( 193 comptime Repo: type, 194 io: std.Io, 195 allocator: std.mem.Allocator, 196 repo_path: []const u8, 197 create_if_missing: bool, 198 ) !Repo { 199 return Repo.open(io, allocator, .{ .path = repo_path }) catch |err| switch (err) { 200 error.RepoNotFound => { 201 if (!create_if_missing) return err; 202 var repo = try Repo.init(io, allocator, .{ .path = repo_path }); 203 errdefer repo.deinit(io, allocator); 204 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 205 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 206 return repo; 207 }, 208 else => |e| return e, 209 }; 210 } 211 212 fn parseGitCommand(allocator: std.mem.Allocator, command: []const u8) !ParsedGitCommand { 213 var tokens = try std.process.Args.IteratorGeneral(.{ .single_quotes = true }).init(allocator, command); 214 defer tokens.deinit(); 215 216 const service_token = tokens.next() orelse return error.InvalidCommand; 217 const dir_token = tokens.next() orelse return error.InvalidCommand; 218 219 const service: GitService = 220 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 221 .upload_pack 222 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 223 .receive_pack 224 else 225 return error.UnsupportedService; 226 227 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 228 } 229 230 fn resolveRepoPath(allocator: std.mem.Allocator, repo_root_path: []const u8, dir: []const u8) ![]const u8 { 231 if (std.fs.path.isAbsolute(dir)) return try std.fs.path.resolve(allocator, &.{dir}); 232 return try std.fs.path.resolve(allocator, &.{ repo_root_path, dir }); 233 } 234 235 fn isSubPath(parent: []const u8, child: []const u8) bool { 236 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 237 if (!std.mem.startsWith(u8, child, parent)) return false; 238 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 239 } 240 241 fn writeError(sess: *ssh.SessionCtx, msg: []const u8) !void { 242 var buf: [256]u8 = undefined; 243 const text = try std.fmt.bufPrint(&buf, "haxy ssh: {s}\n", .{msg}); 244 try sess.writeBytes(text); 245 } 246 247 test "parseGitCommand rejects malformed input" { 248 const allocator = std.testing.allocator; 249 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 250 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 251 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 252 } 253 254 test "parseGitCommand happy paths" { 255 const allocator = std.testing.allocator; 256 257 { 258 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 259 defer parsed.deinit(allocator); 260 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 261 try std.testing.expectEqualStrings("some-repo", parsed.dir); 262 } 263 { 264 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 265 defer parsed.deinit(allocator); 266 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 267 try std.testing.expectEqualStrings("user/proj", parsed.dir); 268 } 269 { 270 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 271 defer parsed.deinit(allocator); 272 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 273 try std.testing.expectEqualStrings("repo", parsed.dir); 274 } 275 } 276 277 test "isSubPath rejects prefix collisions and traversal" { 278 const sep = std.fs.path.sep_str; 279 const parent = sep ++ "srv" ++ sep ++ "git"; 280 281 try std.testing.expect(isSubPath(parent, parent ++ sep ++ "repo")); 282 try std.testing.expect(isSubPath(parent, parent)); 283 284 // common-prefix-but-not-subpath: "/srv/git" should NOT swallow 285 // "/srv/git2" or "/srv/gitignore" just because the bytes start the same. 286 try std.testing.expect(!isSubPath(parent, parent ++ "2")); 287 try std.testing.expect(!isSubPath(parent, parent ++ "ignore")); 288 289 // resolveRepoPath normalizes `..` first, so `/srv/git/../etc/passwd` 290 // becomes `/etc/passwd` — isSubPath then rejects it. 291 try std.testing.expect(!isSubPath(parent, sep ++ "etc" ++ sep ++ "passwd")); 292 293 const builtin = @import("builtin"); 294 295 if (.windows != builtin.os.tag) { 296 try std.testing.expect(isSubPath(sep, sep ++ "anything")); 297 try std.testing.expect(isSubPath(sep, sep)); 298 } 299 } 300