Skip to content

Commit 1434e6e

Browse files
committed
fix: no exception if no destination provided
1 parent fdd126d commit 1434e6e

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/helpers/copy.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12+
const NO_DEST_ERROR = {
13+
body: JSON.stringify({ error: 'No destination provided.' }),
14+
status: 400,
15+
};
16+
1217
export default async function copyHelper(req, daCtx) {
1318
const formData = await req.formData();
1419
if (!formData) return {};
1520
const fullDest = formData.get('destination');
21+
if (!fullDest) return { error: NO_DEST_ERROR };
1622
const continuationToken = formData.get('continuation-token');
1723
const lower = fullDest.slice(1).toLowerCase();
1824
const sanitized = lower.endsWith('/') ? lower.slice(0, -1) : lower;

src/routes/copy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { hasPermission } from '../utils/auth.js';
1515

1616
export default async function copyHandler({ req, env, daCtx }) {
1717
const details = await copyHelper(req, daCtx);
18+
if (details.error) return details.error;
1819
if (!hasPermission(daCtx, details.source, 'read')
1920
|| !hasPermission(daCtx, details.destination, 'write')) return { status: 403 };
2021
return copyObject(env, daCtx, details, false);

test/routes/copy.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,30 @@ describe('Copy Route', () => {
6969
assert.strictEqual('my/dest2.html', copyCalled[0].d.destination);
7070
assert.strictEqual(false, copyCalled[0].m);
7171
});
72+
73+
it('Test copyHandler - no destination provided', async () => {
74+
const copyCalled = [];
75+
const copyObject = (e, c, d, m) => {
76+
copyCalled.push({
77+
e, c, d, m,
78+
});
79+
return { status: 200 };
80+
};
81+
82+
const copyHandler = await esmock('../../src/routes/copy.js', {
83+
'../../src/storage/object/copy.js': {
84+
default: copyObject,
85+
},
86+
'../../src/utils/auth.js': { hasPermission: () => true },
87+
});
88+
89+
const formdata = new Map();
90+
const req = {
91+
formData: () => formdata,
92+
};
93+
94+
const resp = await copyHandler({ req, env: {}, daCtx: { key: 'my/src.html' } });
95+
assert.strictEqual(400, resp.status);
96+
assert.strictEqual(copyCalled.length, 0);
97+
});
7298
});

0 commit comments

Comments
 (0)