Skip to content

fix(server): validate expiresAt token value for non existence #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/server/auth/middleware/bearerAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe("requireBearerAuth middleware", () => {
token: "valid-token",
clientId: "client-123",
scopes: ["read", "write"],
expiresAt: Math.floor(Date.now() / 1000) + 3600, // Token expires in an hour
};
mockVerifyAccessToken.mockResolvedValue(validAuthInfo);

Expand All @@ -60,12 +61,15 @@ describe("requireBearerAuth middleware", () => {
expect(mockResponse.json).not.toHaveBeenCalled();
});

it("should reject expired tokens", async () => {
it.each([
[Math.floor(Date.now() / 1000) - 100], // Token expired 100 seconds ago
[0], // Token expires at the same time as now
])("should reject expired tokens (expiresAt: %s)", async (expiresAt: number) => {
const expiredAuthInfo: AuthInfo = {
token: "expired-token",
clientId: "client-123",
scopes: ["read", "write"],
expiresAt: Math.floor(Date.now() / 1000) - 100, // Token expired 100 seconds ago
expiresAt
};
mockVerifyAccessToken.mockResolvedValue(expiredAuthInfo);

Expand All @@ -87,7 +91,38 @@ describe("requireBearerAuth middleware", () => {
);
expect(nextFunction).not.toHaveBeenCalled();
});


it.each([
[undefined], // Token has no expiration time
[NaN], // Token has no expiration time
])("should reject tokens with no expiration time (expiresAt: %s)", async (expiresAt: number | undefined) => {
const noExpirationAuthInfo: AuthInfo = {
token: "no-expiration-token",
clientId: "client-123",
scopes: ["read", "write"],
expiresAt
};
mockVerifyAccessToken.mockResolvedValue(noExpirationAuthInfo);

mockRequest.headers = {
authorization: "Bearer expired-token",
};

const middleware = requireBearerAuth({ provider: mockProvider });
await middleware(mockRequest as Request, mockResponse as Response, nextFunction);

expect(mockVerifyAccessToken).toHaveBeenCalledWith("expired-token");
expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.set).toHaveBeenCalledWith(
"WWW-Authenticate",
expect.stringContaining('Bearer error="invalid_token"')
);
expect(mockResponse.json).toHaveBeenCalledWith(
expect.objectContaining({ error: "invalid_token", error_description: "Token has no expiration time" })
);
expect(nextFunction).not.toHaveBeenCalled();
});

it("should accept non-expired tokens", async () => {
const nonExpiredAuthInfo: AuthInfo = {
token: "valid-token",
Expand Down Expand Up @@ -147,6 +182,7 @@ describe("requireBearerAuth middleware", () => {
token: "valid-token",
clientId: "client-123",
scopes: ["read", "write", "admin"],
expiresAt: Math.floor(Date.now() / 1000) + 3600, // Token expires in an hour
};
mockVerifyAccessToken.mockResolvedValue(authInfo);

Expand Down
6 changes: 4 additions & 2 deletions src/server/auth/middleware/bearerAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export function requireBearerAuth({ provider, requiredScopes = [] }: BearerAuthM
}
}

// Check if the token is expired
if (!!authInfo.expiresAt && authInfo.expiresAt < Date.now() / 1000) {
// Check if the token is set to expire or if it is expired
if (typeof authInfo.expiresAt !== 'number' || isNaN(authInfo.expiresAt)) {
throw new InvalidTokenError("Token has no expiration time");
} else if (authInfo.expiresAt < Date.now() / 1000) {
throw new InvalidTokenError("Token has expired");
}

Expand Down