Skip to content

Commit df98f3e

Browse files
fix: GH-221 - Disable anonymous access (#225)
* GH-221 - Disable anonymous access * Return 412 if any user is considered anonymous * Updated tests to pass a user so existing test cases pass * Added new tests to cover 412 Resolves: GH-221 * Change to 401 for anonymous users
1 parent 911b42c commit df98f3e

File tree

6 files changed

+166
-42
lines changed

6 files changed

+166
-42
lines changed

.dev.vars.it

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Integration test environment variables
2+
# These override .dev.vars when running with --env it
3+
4+
S3_DEF_URL=http://localhost:4569
5+
S3_ACCESS_KEY_ID=S3RVER
6+
S3_SECRET_ACCESS_KEY=S3RVER
7+
S3_FORCE_PATH_STYLE=true
8+
IMS_ORIGIN=http://localhost:9999
9+
AEM_ADMIN_MEDIA_API_KEY=test-key

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,5 @@ dist
138138

139139
.vscode
140140

141+
# Integration test - S3rver generated files
142+
test/it/bucket/

package-lock.json

Lines changed: 1 addition & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ export default {
3939
return daResp({ status: 500 });
4040
}
4141

42-
const { authorized, key } = daCtx;
43-
if (!authorized) {
44-
const status = daCtx.users[0].email === 'anonymous' ? 401 : 403;
45-
return daResp({ status });
46-
}
42+
const { users, authorized, key } = daCtx;
43+
44+
// Anonymous users are not permitted
45+
const anon = users.some((user) => user.email === 'anonymous');
46+
if (anon) return daResp({ status: 401 });
47+
48+
if (!authorized) return daResp({ status: 403 });
49+
4750
if (key?.startsWith('.da-versions')) {
4851
return daResp({ status: 404 });
4952
}

test/index.test.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ describe('fetch', () => {
2424
});
2525

2626
it('should return a response object for unknown', async () => {
27-
const resp = await handler.fetch({ url: 'https://www.example.com/endpoint/repo/path/file.html', method: 'BLAH' }, {});
27+
const hnd = await esmock('../src/index.js', {
28+
'../src/utils/daCtx.js': {
29+
default: async () => ({ authorized: true, users: [{ email: '[email protected]' }], path: '/endpoint/repo/path/file.html' }),
30+
},
31+
});
32+
33+
const resp = await hnd.fetch({ url: 'https://www.example.com/endpoint/repo/path/file.html', method: 'BLAH' }, {});
2834
assert.strictEqual(resp.status, 405);
2935
});
3036

31-
it('should return 401 when not authorized and not logged in', async () => {
37+
it('should return 401 when user is anonymous', async () => {
3238
const hnd = await esmock('../src/index.js', {
3339
'../src/utils/daCtx.js': {
3440
default: async () => ({ authorized: false, users: [{ email: 'anonymous' }] }),
@@ -39,6 +45,17 @@ describe('fetch', () => {
3945
assert.strictEqual(resp.status, 401);
4046
});
4147

48+
it('should return 401 when not authorized and not logged in', async () => {
49+
const hnd = await esmock('../src/index.js', {
50+
'../src/utils/daCtx.js': {
51+
default: async () => ({ authorized: false, users: [{ email: '[email protected]' }] }),
52+
},
53+
});
54+
55+
const resp = await hnd.fetch({ method: 'GET' }, {});
56+
assert.strictEqual(resp.status, 403);
57+
});
58+
4259
it('should return 403 when logged in but not authorized', async () => {
4360
const hnd = await esmock('../src/index.js', {
4461
'../src/utils/daCtx.js': {
@@ -51,7 +68,13 @@ describe('fetch', () => {
5168
});
5269

5370
it('return 404 for unknown get route', async () => {
54-
const resp = await handler.fetch({ method: 'GET', url: 'http://www.example.com/' }, {});
71+
const hnd = await esmock('../src/index.js', {
72+
'../src/utils/daCtx.js': {
73+
default: async () => ({ authorized: true, users: [{ email: '[email protected]' }], path: '/' }),
74+
},
75+
});
76+
77+
const resp = await hnd.fetch({ method: 'GET', url: 'http://www.example.com/' }, {});
5578
assert.strictEqual(resp.status, 404);
5679
});
5780

@@ -70,8 +93,32 @@ describe('fetch', () => {
7093
});
7194

7295
describe('invalid routes', () => {
96+
let hnd;
97+
98+
before(async () => {
99+
hnd = await esmock('../src/index.js', {
100+
'../src/utils/daCtx.js': {
101+
default: async (req) => {
102+
const { pathname } = new URL(req.url);
103+
// For invalid paths, throw the error that getDaCtx would throw
104+
if (pathname.includes('//')) {
105+
throw new Error('Invalid path');
106+
}
107+
return {
108+
authorized: true,
109+
users: [{ email: '[email protected]' }],
110+
path: pathname,
111+
api: 'source',
112+
org: 'owner',
113+
key: 'repo/path/file.html',
114+
};
115+
},
116+
},
117+
});
118+
});
119+
73120
const fetchStatus = async (path, method) => {
74-
const resp = await handler.fetch({ method, url: `http://www.sample.com${path}` }, {});
121+
const resp = await hnd.fetch({ method, url: `http://www.sample.com${path}` }, {});
75122
return resp.status;
76123
};
77124

0 commit comments

Comments
 (0)