Skip to content

Commit e949377

Browse files
committed
shaky fix for issue#1692
1 parent f5652f3 commit e949377

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

lib/handlers/put.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,40 @@ const getContentType = require('../utils').getContentType
66
const HTTPError = require('../http-error')
77
const { stringToStream } = require('../utils')
88

9+
// TODO: ask alain a better way to get the suffix variables here
10+
const RESERVED_SUFFIXES = ['.acl', '.meta']
11+
12+
/**
13+
* This function is used to make sure a resource or container which contains
14+
* reserved suffixes for auxiliary documents cannot be created.
15+
* @param {string} path - the uri to check for invalid suffixes
16+
* @returns {boolean} true is fail - if the path contains reserved suffixes
17+
*/
18+
function containsInvalidSuffixes (path) {
19+
// if it is a container, no suffix so remove last slash
20+
if (path.endsWith('/')) {
21+
path = path.slice(0, -1)
22+
} else {
23+
// this is a resource, so it either ends with an extension, or just text
24+
const lastFullStop = path.lastIndexOf('.')
25+
if (lastFullStop !== -1) { // contains at least one full stop
26+
path = path.slice(0, lastFullStop)
27+
}
28+
}
29+
return RESERVED_SUFFIXES.some(suffix => path.includes(suffix))
30+
}
31+
932
async function handler (req, res, next) {
1033
debug(req.originalUrl)
1134
// deprecated kept for compatibility
1235
res.header('MS-Author-Via', 'SPARQL') // is this needed ?
1336
const contentType = req.get('content-type')
1437

38+
// make sure the resource being created does not attempt invalid resource creation
39+
if (containsInvalidSuffixes(req.url)) {
40+
next(new HTTPError(400, `${req.url} contained reserved suffixes in path`))
41+
}
42+
1543
// check whether a folder or resource with same name exists
1644
try {
1745
const ldp = req.app.locals.ldp

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
"validate": "node ./test/validate-turtle.js",
147147
"nyc": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 nyc --reporter=text-summary mocha --recursive test/integration/ test/unit/",
148148
"mocha": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 mocha --recursive test/integration/ test/unit/",
149+
"mocha-http": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 mocha --recursive test/integration/http-test.js",
149150
"prepublishOnly": "npm test",
150151
"postpublish": "git push --follow-tags",
151152
"test": "npm run standard && npm run validate && npm run nyc",

test/integration/http-test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,23 @@ describe('HTTP APIs', function () {
670670
.expect(201, done)
671671
}
672672
)
673+
it('should return a 400 error when trying to put a container that contains a reserved suffix',
674+
function (done) {
675+
server.put('/foo/bar.acl/test/')
676+
.set('content-type', 'text/turtle')
677+
.set('link', '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"')
678+
.expect(400, done)
679+
}
680+
)
681+
it('should return a 400 error when trying to put a resource that contains a reserved suffix',
682+
function (done) {
683+
server.put('/foo/bar.acl/test.ttl')
684+
.send(putRequestBody)
685+
.set('content-type', 'text/turtle')
686+
.set('link', '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"')
687+
.expect(400, done)
688+
}
689+
)
673690
// Cleanup
674691
after(function () {
675692
rm('/foo/')
@@ -846,7 +863,7 @@ describe('HTTP APIs', function () {
846863
if (err) return done(err)
847864
try {
848865
postLocation = res.headers.location
849-
console.log('location ' + postLocation)
866+
// console.log('location ' + postLocation)
850867
const createdDir = fs.statSync(path.join(__dirname, '../resources', postLocation.slice(0, -1)))
851868
assert(createdDir.isDirectory(), 'Container should have been created')
852869
} catch (err) {

test/resources/foo/bar.acl/test.ttl

Whitespace-only changes.

0 commit comments

Comments
 (0)