bobbai00 commented on code in PR #9580:
URL: https://github.com/apache/texera/pull/9580#discussion_r4687130594
##########
agent-service/src/api/auth-api.ts:
##########
@@ -53,8 +55,23 @@ function isTokenExpired(token: string): boolean {
}
}
+// Whether the dashboard service issued this token: its signature is the
HMAC-SHA256
+// of the first two segments under the shared secret, as JwtAuth.scala writes
it. The
+// header's own `alg` is never consulted, so a token cannot choose a weaker
check.
+function isSignedByTexera(token: string): boolean {
+ const parts = token.split(".");
+ if (parts.length !== 3) {
+ return false;
+ }
+ const expected = createHmac("sha256",
env.AUTH_JWT_SECRET).update(`${parts[0]}.${parts[1]}`).digest();
+ const actual = Buffer.from(parts[2], "base64url");
+ return actual.length === expected.length && timingSafeEqual(actual,
expected);
+}
+
+// A token's claims can be trusted only when this holds: anyone can write the
claims,
+// only the dashboard service can sign them.
export function validateToken(token: string): boolean {
- return !isTokenExpired(token);
+ return isSignedByTexera(token) && !isTokenExpired(token);
}
Review Comment:
Done in 435e4d26d: one `verifyToken(token): UserInfo | undefined` checks the
signature and expiry and reads the user from a single decode, in place of
`validateToken`, `extractUserFromToken` and `userOfToken`. It also refuses a
token whose `userId` isn't a whole number, since owners are matched by that id.
It has the same shape as #9043's `verifyToken`, and #9043 lands after this PR
and takes over the token check, so the service keeps one verifier.
##########
agent-service/src/server.spec.ts:
##########
@@ -26,27 +27,42 @@ import { ConsoleMessageType, OperatorState } from
"./types/execution";
const API = env.API_PREFIX;
const app = buildApp();
-function mintTestToken(): string {
+// Signed as the dashboard service signs a login token, unless a test names
another secret.
+function mintTestToken(userId = 1, secret: string = env.AUTH_JWT_SECRET):
string {
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT"
})).toString("base64url");
const payload = Buffer.from(
JSON.stringify({
sub: "tester",
- userId: 1,
+ userId,
email: "[email protected]",
role: "REGULAR",
exp: Math.floor(Date.now() / 1000) + 3600,
})
).toString("base64url");
- return `${header}.${payload}.test-signature`;
+ const signature = createHmac("sha256",
secret).update(`${header}.${payload}`).digest("base64url");
+ return `${header}.${payload}.${signature}`;
}
Review Comment:
Done in 435e4d26d: `src/api/test-token.ts` exports `signTestToken(claims,
secret)` and `mintTestToken(userId, secret)`, and all three spec files use it.
##########
agent-service/src/server.ts:
##########
@@ -70,6 +72,7 @@ async function createAgentInstance(
agentId,
agentName: customName || "Bob",
userToken: delegateConfig.userToken,
+ ownerUid: owner.uid,
Review Comment:
Done in 435e4d26d: the log now records `userId: owner.uid`.
##########
agent-service/src/server.ts:
##########
@@ -168,40 +187,51 @@ const agentsRouter = new Elysia({ prefix: "/agents" })
set.status = ERROR_STATUS[errorMessage] ?? 500;
return { error: errorMessage || "Internal server error" };
})
- .get("/", () => {
- const agentList = Array.from(agentStore.entries()).map(([id, agent]) =>
getAgentInfo(id, agent));
+ // Every agent route acts as the user in the request's bearer token, which
+ // travels in the Authorization header, never in the payload. A route that
+ // names an agent is served only to that agent's owner.
+ .derive(({ headers, params }) => {
+ const userToken = extractBearerToken(headers.authorization);
+ if (!userToken) {
+ throw new Error("Authorization header with a Bearer token is required");
+ }
+ const user = userOfToken(userToken);
+ if (!user) {
+ throw new Error("Invalid or expired token");
+ }
+ // Routes without a path parameter carry no `params` at all.
+ const agentId = (params as { id?: string } | undefined)?.id;
+ if (agentId !== undefined && !ownedAgent(agentId, user)) {
+ throw new Error("Agent not found");
+ }
Review Comment:
Done in 435e4d26d: the guard now only authenticates, and every route gets
its agent from `getAgent(id, user)`, which returns it to its owner alone. A
later `/:agentId/...` route can't reach an agent without passing the caller.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]