Skip to content

Commit

Permalink
fix(endpoint-token): return 403 if token URL doesn’t match publicatio…
Browse files Browse the repository at this point in the history
…n URL
  • Loading branch information
paulrobertlloyd committed Apr 1, 2022
1 parent 8720945 commit 807690e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion helpers/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const testConfig = async (options) => {
...(options.useSyndicator ? ["@indiekit/syndicator-twitter"] : []),
],
publication: {
me: process.env.TEST_PUBLICATION_URL,
me: options?.publication?.me || process.env.TEST_PUBLICATION_URL,
postTypes,
timeZone: "UTC",
},
Expand Down
32 changes: 19 additions & 13 deletions packages/endpoint-token/lib/controllers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ export const tokenController = (publication) => ({
.split(/\s+/)[1];
const accessToken = jwt.verify(bearerToken, process.env.TOKEN_SECRET);

// Normalize publication and token URLs before comparing
const accessTokenMe = getCanonicalUrl(accessToken.me);
const publicationMe = getCanonicalUrl(publication.me);
const isAuthenticated = accessTokenMe === publicationMe;

// Publication URL does not match that provided by access token
if (!isAuthenticated) {
return next(new HttpError(403, "Publication URL does not match that provided by access token"))
}

if (
getCanonicalUrl(accessToken.me) === getCanonicalUrl(publication.me)
request?.headers?.accept &&
request.headers.accept.includes("application/json")
) {
if (
request?.headers?.accept &&
request.headers.accept.includes("application/json")
) {
response.json(accessToken);
} else {
response.header(
"Content-Type",
"application/x-www-form-urlencoded"
);
response.send(new URLSearchParams(accessToken).toString());
}
response.json(accessToken);
} else {
response.header(
"Content-Type",
"application/x-www-form-urlencoded"
);
response.send(new URLSearchParams(accessToken).toString());
}
} catch (error) {
next(new HttpError(401, `JSON Web Token error: ${error.message}`));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from "ava";
import { testServer } from "@indiekit-test/server";

test("Returns 403 if publication URL doesn’t match URL in token", async (t) => {
const request = await testServer({
publication: {
me: 'https://server.example'
}
});
const result = await request
.get("/token")
.auth(process.env.TEST_TOKEN, { type: "bearer" })
.set("Accept", "application/json");

t.is(result.status, 403);
t.is(result.body.error_description, "Publication URL does not match that provided by access token");
});

0 comments on commit 807690e

Please sign in to comment.