Skip to content

Commit dcfb901

Browse files
authored
fix: check anon when accessing brief (#2964)
1 parent 7ec270f commit dcfb901

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

__tests__/posts.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ jest.mock('../src/common/typedPubsub', () => ({
123123
let con: DataSource;
124124
let state: GraphQLTestingState;
125125
let client: GraphQLTestClient;
126-
let loggedUser: string = null;
126+
let loggedUser: string | null = null;
127127
let isTeamMember = false;
128128
let isPlus = false;
129129
let roles: Roles[] = [];
@@ -1245,7 +1245,7 @@ describe('query post', () => {
12451245
);
12461246
});
12471247

1248-
it('should throw not found when brief post is from other user', async () => {
1248+
it('should throw when brief post is from other user', async () => {
12491249
loggedUser = '1';
12501250

12511251
await saveFixtures(con, BriefPost, [
@@ -1269,6 +1269,30 @@ describe('query post', () => {
12691269
);
12701270
});
12711271

1272+
it('should throw for anonymous user accessing brief', async () => {
1273+
loggedUser = null;
1274+
1275+
await saveFixtures(con, BriefPost, [
1276+
{
1277+
id: 'pbriefanotherauthor',
1278+
shortId: 'pbfaa',
1279+
title: 'pbriefanotherauthor',
1280+
score: 0,
1281+
sourceId: BRIEFING_SOURCE,
1282+
createdAt: new Date('2021-09-22T07:15:51.247Z'),
1283+
private: true,
1284+
visible: true,
1285+
authorId: '2',
1286+
},
1287+
]);
1288+
1289+
return testQueryErrorCode(
1290+
client,
1291+
{ query: QUERY('pbriefanotherauthor') },
1292+
'FORBIDDEN',
1293+
);
1294+
});
1295+
12721296
describe('clickbaitTitleDetected', () => {
12731297
const LOCAL_QUERY = /* GraphQL */ `
12741298
query Post($id: ID!) {

src/schema/sources.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,9 @@ export const ensureSourcePermissions = async (
12051205
if (
12061206
permission == SourcePermissions.View &&
12071207
source.id === BRIEFING_SOURCE &&
1208-
ctx.userId &&
12091208
post?.type === PostType.Brief
12101209
) {
1211-
if (post.authorId !== ctx.userId) {
1210+
if (!ctx.userId || post.authorId !== ctx.userId) {
12121211
throw new ForbiddenError('Access denied!');
12131212
}
12141213

0 commit comments

Comments
 (0)