kunwp1 commented on code in PR #9580:
URL: https://github.com/apache/texera/pull/9580#discussion_r4661690353
##########
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:
`mintTestToken` is repeated word for word in `server.ws.spec.ts`, and
`auth-api.spec.ts` signs tokens the same way in `tokenWith`. They could share
one helper, e.g. `src/api/test-token.ts` exporting `signTestToken(claims,
secret)` and `mintTestToken(userId, secret)`, so a change to the token format
is made once.
##########
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:
#9043 adds its own HS256 verifier in `agent-service/src/config/jwt.ts`, with
a different way of resolving the secret. Whichever of the two lands second
should reuse the first's verifier, so the service doesn't end up with two.
Separately, `userOfToken` in `server.ts` runs `validateToken` and then
`extractUserFromToken`, so the payload is decoded twice. A single
`verifyToken(token): UserInfo | undefined` here would do it in one pass.
Optional.
##########
agent-service/src/server.ts:
##########
@@ -70,6 +72,7 @@ async function createAgentInstance(
agentId,
agentName: customName || "Bob",
userToken: delegateConfig.userToken,
+ ownerUid: owner.uid,
Review Comment:
The `"created agent"` log at line 103 still logs
`delegateConfig.userInfo?.uid`, which is undefined for an agent created without
a delegate. `owner.uid` is always set now, so it could log `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:
The ownership check runs only when the route's param is named `id`. If a
later route is written as `/:agentId/...`, `agentId` here is undefined, so the
request gets a `user` but skips the owner check. Having `derive` return the
owned agent, or having the routes call a `getAgent(id, user)` that checks
ownership, would make the check hard to miss. Not blocking.
--
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]