Your fix matches the spec boundary. RFC 7519 says the current time must be before exp, so rejecting when exp <= now is right. The same section also allows a small leeway for clock skew, usually no more than a few minutes, which is why libraries like jsonwebtoken have a clockTolerance option. The problem in the original was where the tolerance lived: 300 seconds written as a bare number inside the comparison, where nobody reviewing it would read it as a policy decision.
One habit worth carrying into the next 43 days: pass the current time into verifySessionToken instead of calling Date.now() inside it. Then you can write boundary tests that never flake. A token with exp one second in the past is rejected, one with exp one second in the future is accepted, and if you later add a named leeway of, say, 30 seconds, a token 31 seconds past exp is rejected. Those tests are what stop a minus 300 from creeping back in.
Thank you, Mike. This is a really helpful observation.
I focused on correcting the expiry boundary to match the specification, but your point about making clock tolerance an explicit policy rather than embedding it in the comparison is something I hadn't considered.
I also appreciate the suggestion to pass the current time into the function. It makes the importance of predictable boundary testing much clearer, especially when testing tokens just before, exactly at, and just after their expiry time.
That's another lesson I'm taking away from this challenge: fixing the immediate bug is important, but making the code easier to test and maintain matters just as much.
Mike Dabydeen
Your fix matches the spec boundary. RFC 7519 says the current time must be before exp, so rejecting when exp <= now is right. The same section also allows a small leeway for clock skew, usually no more than a few minutes, which is why libraries like jsonwebtoken have a clockTolerance option. The problem in the original was where the tolerance lived: 300 seconds written as a bare number inside the comparison, where nobody reviewing it would read it as a policy decision.
One habit worth carrying into the next 43 days: pass the current time into verifySessionToken instead of calling Date.now() inside it. Then you can write boundary tests that never flake. A token with exp one second in the past is rejected, one with exp one second in the future is accepted, and if you later add a named leeway of, say, 30 seconds, a token 31 seconds past exp is rejected. Those tests are what stop a minus 300 from creeping back in.