Skip to content

Commit 2fbe582

Browse files
authored
Merge pull request #1014 from rubensworks/change/delete-permissions
Make DELETE require write permissions on container
2 parents d5315f6 + 8e90a7d commit 2fbe582

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/handlers/allow.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module.exports = allow
22

33
const $rdf = require('rdflib')
4+
const path = require('path')
45
const ACL = require('../acl-checker')
56
const debug = require('../debug.js').ACL
67
const fs = require('fs')
78
const { promisify } = require('util')
89
const HTTPError = require('../http-error')
910

10-
function allow (mode) {
11+
function allow (mode, checkPermissionsForDirectory) {
1112
return async function allowHandler (req, res, next) {
1213
const ldp = req.app.locals.ldp || {}
1314
if (!ldp.webid) {
@@ -19,26 +20,31 @@ function allow (mode) {
1920

2021
// Determine the actual path of the request
2122
// (This is used as an ugly hack to check the ACL status of other resources.)
22-
let reqPath = res && res.locals && res.locals.path
23+
let resourcePath = res && res.locals && res.locals.path
2324
? res.locals.path
2425
: req.path
2526

27+
// Check permissions of the directory instead of the file itself.
28+
if (checkPermissionsForDirectory) {
29+
resourcePath = path.dirname(resourcePath)
30+
}
31+
2632
// Check whether the resource exists
2733
let stat
2834
try {
29-
const ret = await ldp.exists(req.hostname, reqPath)
35+
const ret = await ldp.exists(req.hostname, resourcePath)
3036
stat = ret.stream
3137
} catch (err) {
3238
stat = null
3339
}
3440

3541
// Ensure directories always end in a slash
36-
if (!reqPath.endsWith('/') && stat && stat.isDirectory()) {
37-
reqPath += '/'
42+
if (!resourcePath.endsWith('/') && stat && stat.isDirectory()) {
43+
resourcePath += '/'
3844
}
3945

4046
// Obtain and store the ACL of the requested resource
41-
req.acl = new ACL(rootUrl + reqPath, {
47+
req.acl = new ACL(rootUrl + resourcePath, {
4248
agentOrigin: req.get('origin'),
4349
// host: req.get('host'),
4450
fetch: fetchFromLdp(ldp.resourceMapper),

lib/ldp-middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function LdpMiddleware (corsSettings) {
2626
router.post('/*', allow('Append'), post)
2727
router.patch('/*', allow('Append'), patch)
2828
router.put('/*', allow('Write'), put)
29-
router.delete('/*', allow('Write'), del)
29+
router.delete('/*', allow('Write', true), del)
3030

3131
return router
3232
}

0 commit comments

Comments
 (0)