Skip to content

eBPF-based security platform

tools/xtop: drill into an image by its whole name

John Fastabend8f3e1d2a15c1
diff --git a/tools/xtop/src/lib/search.ts b/tools/xtop/src/lib/search.ts
index 7bd96923..ff6bca36 100644
--- a/tools/xtop/src/lib/search.ts
+++ b/tools/xtop/src/lib/search.ts
@@ -36,10 +36,10 @@ export const parseSearch = (query: string): Search => {
};
};
-/** A search for the words of text as they are, such as a name to drill into. */
+/**
+ * A search for the rows with a field that is text and nothing else, such as
+ * a name to drill into: one term, since separate words could each match
+ * another field, or only a part of one.
+ */
export const literalSearch = (text: string) =>
- text
- .split(/\s+/)
- .filter(Boolean)
- .map((word) => escapeRegExp(word).replace(/^!/, "\\!"))
- .join(" ");
+ `^${text.trim().split(/\s+/).filter(Boolean).map(escapeRegExp).join("\\s")}$`;
diff --git a/tools/xtop/test/search.test.ts b/tools/xtop/test/search.test.ts
index c28405d2..ca534a57 100644
--- a/tools/xtop/test/search.test.ts
+++ b/tools/xtop/test/search.test.ts
@@ -44,11 +44,18 @@ describe("parseSearch", () => {
describe("literalSearch", () => {
test("finds the text as it is", () => {
const query = literalSearch("fleet-os 1.0+b1");
- expect(query).toBe("fleet-os 1\\.0\\+b1");
+ expect(query).toBe("^fleet-os\\s1\\.0\\+b1$");
expect(parseSearch(query).test(["fleet-os 1.0+b1"])).toBe(true);
expect(parseSearch(query).test(["fleet-os 1x00b1"])).toBe(false);
});
+ test("finds a whole field, not words spread over several or a part of one", () => {
+ const search = parseSearch(literalSearch("web 1.2"));
+ expect(search.test(["web-07", "api 3.0", "6.1.2"])).toBe(false);
+ expect(search.test(["edge-01", "web 1.2.1"])).toBe(false);
+ expect(search.test(["edge-01", "web 1.2"])).toBe(true);
+ });
+
test("a word starting with ! is searched for, not excluded", () => {
expect(parseSearch(literalSearch("!x")).test(["!x"])).toBe(true);
});