Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/helpers/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const NO_DEST_ERROR = {
body: JSON.stringify({ error: 'No destination provided.' }),
status: 400,
};

export default async function copyHelper(req, daCtx) {
const formData = await req.formData();
if (!formData) return {};
const fullDest = formData.get('destination');
if (!fullDest) return { error: NO_DEST_ERROR };
const continuationToken = formData.get('continuation-token');
const lower = fullDest.slice(1).toLowerCase();
const sanitized = lower.endsWith('/') ? lower.slice(0, -1) : lower;
Expand Down
1 change: 1 addition & 0 deletions src/routes/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { hasPermission } from '../utils/auth.js';

export default async function copyHandler({ req, env, daCtx }) {
const details = await copyHelper(req, daCtx);
if (details.error) return details.error;
if (!hasPermission(daCtx, details.source, 'read')
|| !hasPermission(daCtx, details.destination, 'write')) return { status: 403 };
return copyObject(env, daCtx, details, false);
Expand Down
26 changes: 26 additions & 0 deletions test/routes/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,30 @@ describe('Copy Route', () => {
assert.strictEqual('my/dest2.html', copyCalled[0].d.destination);
assert.strictEqual(false, copyCalled[0].m);
});

it('Test copyHandler - no destination provided', async () => {
const copyCalled = [];
const copyObject = (e, c, d, m) => {
copyCalled.push({
e, c, d, m,
});
return { status: 200 };
};

const copyHandler = await esmock('../../src/routes/copy.js', {
'../../src/storage/object/copy.js': {
default: copyObject,
},
'../../src/utils/auth.js': { hasPermission: () => true },
});

const formdata = new Map();
const req = {
formData: () => formdata,
};

const resp = await copyHandler({ req, env: {}, daCtx: { key: 'my/src.html' } });
assert.strictEqual(400, resp.status);
assert.strictEqual(copyCalled.length, 0);
});
});