Files

96 lines
7.8 KiB
C++
Raw Permalink Normal View History

// 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");
// Data plane, CDP, stream, operations, resources, templates.
const std::string op = sandbox + "/operations/1";
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"fly\"}", "unknown data plane action");
bad("/agent/sandbox/dataplane", "{\"name\":\"" + engine + "\",\"action\":\"health\"}", "engine used as sandbox name");
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"activate\",\"tab_id\":\"../x\"}", "tab id with dots");
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"activate\"}", "missing tab id");
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"command\",\"command\":\"nope\"}", "command without a domain");
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"command\",\"command\":\"Page.reload\",\"params\":\"[1]\"}", "params that are not an object");
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"\"}", "empty CDP method");
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"Page.a.b\"}", "CDP method with two dots");
bad("/agent/sandbox/cdp", "{\"name\":\"x\",\"method\":\"Page.reload\"}", "bad sandbox name for CDP");
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"Page.reload\",\"params\":\"{bad\"}", "params that are not JSON");
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"vm\",\"input\":\"x\"}", "unknown stream mode");
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"shell\",\"input\":\"\"}", "empty stream command");
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"code\",\"input\":\"\"}", "empty stream code");
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"delete\"}", "delete is not a POST op");
bad("/agent/sandbox/operations", "{\"name\":\"" + sandbox + "\",\"op\":\"get\"}", "sandbox name used as operation name");
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"wait\",\"timeout\":\"0\"}", "wait timeout under the floor");
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"wait\",\"timeout\":\"121\"}", "wait timeout over the cap");
bad("/agent/sandbox/operations", "{\"name\":\"" + engine + "\",\"op\":\"list\"}", "engine used as operations parent");
bad("/agent/sandbox/operations", "{\"name\":\"" + sandbox + "\",\"op\":\"list\",\"page_size\":\"101\"}", "page size over the cap");
bad("/agent/sandbox/resource", "{\"name\":\"" + sandbox + "\",\"kind\":\"template\"}", "sandbox name as template");
bad("/agent/sandbox/resource", "{\"name\":\"" + sandbox + "\",\"kind\":\"disk\"}", "unknown resource kind");
bad("/agent/sandbox/template", "{\"engine\":\"" + engine + "\",\"display_name\":\"t\",\"category\":\"gpu\"}", "unknown template category");
bad("/agent/sandbox/template", "{\"engine\":\"" + engine + "\",\"display_name\":\"t\"}", "template without a category or image");
bad("/agent/sandbox/template", "{\"engine\":\"x\",\"display_name\":\"t\",\"category\":\"shell\"}", "bad engine for template");
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/operation?name=bad").status == 422,
"operation delete with a bad name is 422");
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/resource?kind=disk&name=x").status == 422,
"resource delete with an unknown kind is 422");
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/resource?kind=snapshot&name=bad").status == 422,
"resource delete with a bad name is 422");
check(f.client->get("/agent/sandbox/dataplane").status == 405, "GET on dataplane is 405");
check(f.client->get("/agent/sandbox/cdp").status == 405, "GET on cdp is 405");
check(f.client->get("/agent/sandbox/stream").status == 405, "GET on stream is 405");
check(f.client->get("/agent/sandbox/operations").status == 405, "GET on operations is 405");
check(f.client->post("/agent/sandbox/operation").status == 405, "POST on operation delete is 405");
// 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");
}