Skip to content

Commit

Permalink
moved invalid suffix logic to LDP class, write test for patch in patc…
Browse files Browse the repository at this point in the history
…h-test and put in http-test, cleaned up package.json
  • Loading branch information
zg009 committed Mar 21, 2024
1 parent 4d029be commit 52480a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
28 changes: 0 additions & 28 deletions lib/handlers/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,12 @@ const getContentType = require('../utils').getContentType
const HTTPError = require('../http-error')
const { stringToStream } = require('../utils')

// TODO: ask alain a better way to get the suffix variables here
const RESERVED_SUFFIXES = ['.acl', '.meta']

/**
* This function is used to make sure a resource or container which contains
* reserved suffixes for auxiliary documents cannot be created.
* @param {string} path - the uri to check for invalid suffixes
* @returns {boolean} true is fail - if the path contains reserved suffixes
*/
function containsInvalidSuffixes (path) {
// if it is a container, no suffix so remove last slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
} else {
// this is a resource, so it either ends with an extension, or just text
const lastFullStop = path.lastIndexOf('.')
if (lastFullStop !== -1) { // contains at least one full stop
path = path.slice(0, lastFullStop)
}
}
return RESERVED_SUFFIXES.some(suffix => path.includes(suffix))
}

async function handler (req, res, next) {
debug(req.originalUrl)
// deprecated kept for compatibility
res.header('MS-Author-Via', 'SPARQL') // is this needed ?
const contentType = req.get('content-type')

// make sure the resource being created does not attempt invalid resource creation
if (containsInvalidSuffixes(req.url)) {
next(new HTTPError(400, `${req.url} contained reserved suffixes in path`))
}

// check whether a folder or resource with same name exists
try {
const ldp = req.app.locals.ldp
Expand Down
24 changes: 24 additions & 0 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,35 @@ class LDP {
} catch (err) { }
}

/**
* This function is used to make sure a resource or container which contains
* reserved suffixes for auxiliary documents cannot be created.
* @param {string} path - the uri to check for invalid suffixes
* @returns {boolean} true is fail - if the path contains reserved suffixes
*/
_containsInvalidSuffixes (path) {
// if it is a container, no suffix so remove last slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
} else {
// this is a resource, so it either ends with an extension, or just text
const lastFullStop = path.lastIndexOf('.')
if (lastFullStop !== -1) { // contains at least one full stop
path = path.slice(0, lastFullStop)
}
}
return AUXILIARY_RESOURCES.some(suffix => path.includes(suffix))
}

// check whether a document (or container) has the same name as another document (or container)
async checkItemName (url) {
let testName, testPath
const { hostname, pathname } = this.resourceMapper._parseUrl(url) // (url.url || url)
let itemUrl = this.resourceMapper.resolveUrl(hostname, pathname)
// make sure the resource being created does not attempt invalid resource creation
if (this._containsInvalidSuffixes(itemUrl)) {
throw error(400, `${itemUrl} contained reserved suffixes in path`)
}
const container = itemUrl.endsWith('/')
try {
const testUrl = container ? itemUrl.slice(0, -1) : itemUrl + '/'
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"validate": "node ./test/validate-turtle.js",
"nyc": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 nyc --reporter=text-summary mocha --recursive test/integration/ test/unit/",
"mocha": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 mocha --recursive test/integration/ test/unit/",
"mocha-http": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 mocha --recursive test/integration/http-test.js",
"prepublishOnly": "npm test",
"postpublish": "git push --follow-tags",
"test": "npm run standard && npm run validate && npm run nyc",
Expand Down
10 changes: 10 additions & 0 deletions test/integration/patch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ describe('PATCH through text/n3', () => {
result: '@prefix : </new.n3#>.\n@prefix tim: </>.\n\ntim:x tim:y tim:z.\n\n'
}))

describe('on an N3 file that has an invalid uri', describePatch({
path: '/foo/bar.acl/test.n3',
exists: false,
patch: `<> a solid:InsertDeletePatch;
solid:inserts { <x> <y> <z>. }.`
}, {
status: 400,
text: '/foo/bar.acl/test.n3 contained reserved suffixes in path'
}))

describe('on a resource with read-only access', describePatch({
path: '/read-only.ttl',
patch: `<> a solid:InsertDeletePatch;
Expand Down

0 comments on commit 52480a3

Please sign in to comment.