// Routes: POST /agent/sandbox/* answer 422 on bad input (names, caps, enums) // before any upstream call; GET on a POST route is 405. #include "support/GoogleRig.hpp" using namespace molo::example::test; int main() { const std::string engine = "projects/p1/locations/us-central1/reasoningEngines/1"; const std::string sandbox = engine + "/sandboxEnvironments/2"; GoogleRig rig; Fixture f(rig.client); auto post = [&](const std::string& route, const std::string& body) { return f.client->post(route, body, "application/json"); }; auto bad = [&](const std::string& route, const std::string& body, const std::string& what) { auto res = post(route, body); check(res.status == 422, what + " is 422"); }; bad("/agent/sandbox/create", "{\"engine\":\"nope\",\"display_name\":\"d\"}", "bad engine name"); bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\"}", "missing display name"); bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"kind\":\"gpu\"}", "unknown kind"); bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"ttl_seconds\":\"5\"}", "ttl under the floor"); bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"kind\":\"computer\"}", "computer sandbox without template"); bad("/agent/sandbox/create", "not json", "malformed body"); bad("/agent/sandbox/get", "{\"name\":\"" + engine + "\"}", "engine name used as sandbox name"); bad("/agent/sandbox/list", "{}", "missing engine"); bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"\"}", "empty code"); bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"x\",\"file_name\":\"../a\",\"file_b64\":\"eA==\"}", "path traversal file name"); bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"x\",\"file_name\":\"a\",\"file_b64\":\"!!\"}", "file not base64"); bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"\"}", "empty command"); bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"ls\",\"timeout\":\"9999\"}", "timeout over the cap"); bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"ls\",\"cwd\":\"/a b\"}", "cwd with a space"); bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"fly\"}", "unknown action"); bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"navigate\",\"url\":\"javascript:1\"}", "script url"); bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"click\",\"x\":\"5\"}", "click without y"); bad("/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"read\",\"path\":\"/etc/passwd\"}", "path outside the workspace"); bad("/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"zap\"}", "unknown file op"); bad("/agent/sandbox/snapshot", "{\"name\":\"" + sandbox + "\",\"display_name\":\"s\",\"post_action\":\"STOP\"}", "bad post action"); // Limit cases: the caps hold at the route. std::string huge_cmd(8193, 'a'); bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"" + huge_cmd + "\"}", "command over 8192 bytes"); std::string huge_code(256 * 1024 + 1, 'a'); bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"" + huge_code + "\"}", "code over 256 KiB"); auto del = f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/delete?name=bad"); check(del.status == 422, "delete with a bad name is 422"); check(f.client->get("/agent/sandbox/create").status == 405, "GET on create is 405"); check(f.client->get("/agent/sandbox/execute").status == 405, "GET on execute is 405"); check(f.client->post("/agent/sandbox/delete").status == 405, "POST on delete is 405"); check(rig.mock->send_count() == 0, "side effect: no upstream traffic for bad input"); return finish("agent/sandbox/validation"); }