Skip to content

Commit 52480a3

Browse files
committed
moved invalid suffix logic to LDP class, write test for patch in patch-test and put in http-test, cleaned up package.json
1 parent 4d029be commit 52480a3

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

lib/handlers/put.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,12 @@ 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-
329
async function handler (req, res, next) {
3310
debug(req.originalUrl)
3411
// deprecated kept for compatibility
3512
res.header('MS-Author-Via', 'SPARQL') // is this needed ?
3613
const contentType = req.get('content-type')
3714

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-
4315
// check whether a folder or resource with same name exists
4416
try {
4517
const ldp = req.app.locals.ldp

lib/ldp.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,35 @@ class LDP {
327327
} catch (err) { }
328328
}
329329

330+
/**
331+
* This function is used to make sure a resource or container which contains
332+
* reserved suffixes for auxiliary documents cannot be created.
333+
* @param {string} path - the uri to check for invalid suffixes
334+
* @returns {boolean} true is fail - if the path contains reserved suffixes
335+
*/
336+
_containsInvalidSuffixes (path) {
337+
// if it is a container, no suffix so remove last slash
338+
if (path.endsWith('/')) {
339+
path = path.slice(0, -1)
340+
} else {
341+
// this is a resource, so it either ends with an extension, or just text
342+
const lastFullStop = path.lastIndexOf('.')
343+
if (lastFullStop !== -1) { // contains at least one full stop
344+
path = path.slice(0, lastFullStop)
345+
}
346+
}
347+
return AUXILIARY_RESOURCES.some(suffix => path.includes(suffix))
348+
}
349+
330350
// check whether a document (or container) has the same name as another document (or container)
331351
async checkItemName (url) {
332352
let testName, testPath
333353
const { hostname, pathname } = this.resourceMapper._parseUrl(url) // (url.url || url)
334354
let itemUrl = this.resourceMapper.resolveUrl(hostname, pathname)
355+
// make sure the resource being created does not attempt invalid resource creation
356+
if (this._containsInvalidSuffixes(itemUrl)) {
357+
throw error(400, `${itemUrl} contained reserved suffixes in path`)
358+
}
335359
const container = itemUrl.endsWith('/')
336360
try {
337361
const testUrl = container ? itemUrl.slice(0, -1) : itemUrl + '/'

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
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",
150149
"prepublishOnly": "npm test",
151150
"postpublish": "git push --follow-tags",
152151
"test": "npm run standard && npm run validate && npm run nyc",

test/integration/patch-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ describe('PATCH through text/n3', () => {
131131
result: '@prefix : </new.n3#>.\n@prefix tim: </>.\n\ntim:x tim:y tim:z.\n\n'
132132
}))
133133

134+
describe('on an N3 file that has an invalid uri', describePatch({
135+
path: '/foo/bar.acl/test.n3',
136+
exists: false,
137+
patch: `<> a solid:InsertDeletePatch;
138+
solid:inserts { <x> <y> <z>. }.`
139+
}, {
140+
status: 400,
141+
text: '/foo/bar.acl/test.n3 contained reserved suffixes in path'
142+
}))
143+
134144
describe('on a resource with read-only access', describePatch({
135145
path: '/read-only.ttl',
136146
patch: `<> a solid:InsertDeletePatch;

0 commit comments

Comments
 (0)