Skip to content

Commit e252a3e

Browse files
ref(api): Keep matchOperation to base paths at the start of the path
Sentry isn't supported when served under a path prefix, so drop the prefix search and note in the matcher that /sentry/api/0/... is deliberately not an API path. A test pins that.
1 parent 0145737 commit e252a3e

2 files changed

Lines changed: 10 additions & 17 deletions

File tree

‎static/packages/api/lib/operation-match.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ function decode(segment: string): string {
4949
*
5050
* The matcher takes a method and a concrete path, such as
5151
* `GET /api/0/organizations/acme/issues/`, and returns the operation whose
52-
* template matches it. The path may include `API_BASE_PATH`, also under a
53-
* prefix such as `/sentry/api/0/`, a query string, or an origin, and empty
54-
* segments are ignored, so a missing trailing slash
52+
* template matches it. The path may include `API_BASE_PATH`, a query string,
53+
* or an origin, and empty segments are ignored, so a missing trailing slash
5554
* still matches. A literal segment wins over a parameter at the first segment
5655
* where templates differ, so `/snapshots/latest-base/` wins over
5756
* `/snapshots/{snapshot_id}/`.
@@ -68,12 +67,10 @@ export function createOperationMatcher<Operation extends ApiOperationTemplate>(
6867
root ??= buildIndex(operations);
6968
if (!URL.canParse(path, PLACEHOLDER_ORIGIN)) return;
7069
let pathname = new URL(path, PLACEHOLDER_ORIGIN).pathname;
71-
// Anywhere in the path: a self-hosted install may serve the API under a prefix.
72-
const base = pathname.indexOf(`${API_BASE_PATH}/`);
73-
if (base !== -1) {
74-
pathname = pathname.slice(base + API_BASE_PATH.length);
75-
} else if (pathname.endsWith(API_BASE_PATH)) {
76-
pathname = "/";
70+
// Only at the start of the path. Sentry isn't supported when served under
71+
// a path prefix, so `/sentry/api/0/...` is deliberately not an API path.
72+
if (pathname === API_BASE_PATH || pathname.startsWith(`${API_BASE_PATH}/`)) {
73+
pathname = pathname.slice(API_BASE_PATH.length);
7774
}
7875
return find(root, segments(pathname), 0, [], method.toUpperCase());
7976
};

‎static/packages/api/test/operation-match.test.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ describe("matchOperation", () => {
1717
["without a trailing slash", "/api/0/organizations/acme/issues"],
1818
["with a query string and hash", "/api/0/organizations/acme/issues/?query=is:unresolved#top"],
1919
["as a full URL", "https://us.sentry.io/api/0/organizations/acme/issues/"],
20-
["under a self-hosted prefix", "/sentry/api/0/organizations/acme/issues/"],
21-
["as a full URL under a self-hosted prefix", "https://sentry.example.com/sentry/api/0/organizations/acme/issues/"],
2220
])("accepts a path %s", (_, path) => {
2321
expect(matchOperation("get", path)?.operation.path).toBe(orgIssues);
2422
});
2523

24+
test("does not look for the base path under a path prefix, which Sentry doesn't support", () => {
25+
expect(matchOperation("get", "/sentry/api/0/organizations/acme/issues/")).toBeUndefined();
26+
});
27+
2628
test("decodes parameter values", () => {
2729
expect(matchOperation("GET", "/organizations/my%20org/issues/")?.params).toEqual({ organization_id_or_slug: "my org" });
2830
});
@@ -80,12 +82,6 @@ describe("createOperationMatcher", () => {
8082
);
8183
});
8284

83-
test("strips the base path under a prefix for custom templates too", () => {
84-
const match = createOperationMatcher([{ method: "GET", path: "/" }, { method: "GET", path: "/teams/{team}/" }]);
85-
expect(match("GET", "/sentry/api/0/teams/backend/")?.params).toEqual({ team: "backend" });
86-
expect(match("GET", "https://sentry.example.com/sentry/api/0")?.operation.path).toBe("/");
87-
});
88-
8985
test("keeps templates that differ only in method apart", () => {
9086
const match = createOperationMatcher([
9187
{ method: "GET", path: "/teams/{team}/", id: "read" },

0 commit comments

Comments
 (0)