Skip to content

Commit f4cba19

Browse files
committed
Do not expose rootPath.
1 parent fc4523c commit f4cba19

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

lib/handlers/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function patchHandler (req, res, next) {
6767
const result = await withLock(path, { mustExist: false }, async () => {
6868
const graph = await readGraph(resource)
6969
await applyPatch(patchObject, graph, url)
70-
return writeGraph(graph, resource, ldp.resourceMapper.rootPath, ldp.serverUri)
70+
return writeGraph(graph, resource, ldp.resourceMapper.getBaseFilePath(req.hostname), ldp.serverUri)
7171
})
7272

7373
// Send the result to the client

lib/ldp.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ldpContainer = require('./ldp-container')
1717
const parse = require('./utils').parse
1818
const fetch = require('node-fetch')
1919
const { promisify } = require('util')
20+
const URL = require('url')
2021
const URI = require('urijs')
2122
const withLock = require('./lock')
2223

@@ -153,8 +154,7 @@ class LDP {
153154
originalPath += '/'
154155
}
155156
}
156-
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl(
157-
{ path: this.resourceMapper.rootPath + resourcePath, hostname: host })
157+
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl({ path: resourcePath, hostname: host })
158158

159159
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
160160
// for JSON bodies. So, the stream needs to be reset
@@ -218,7 +218,8 @@ class LDP {
218218
// First check if we are above quota
219219
let isOverQuota
220220
try {
221-
isOverQuota = await overQuota(this.resourceMapper.rootPath, this.serverUri)
221+
const { hostname } = URL.parse(url.url || url)
222+
isOverQuota = await overQuota(this.resourceMapper.getBaseFilePath(hostname), this.serverUri)
222223
} catch (err) {
223224
throw error(500, 'Error finding user quota')
224225
}

lib/resource-mapper.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,29 @@ class ResourceMapper {
3737
}
3838
}
3939

40-
get rootPath () {
41-
return this._rootPath
40+
// Gets the base file path for the given hostname
41+
getBaseFilePath (hostname) {
42+
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
43+
}
44+
45+
// Resolve a URL for the given hostname
46+
// Optionally, a pathname may be passed that will be appended to the baseUrl.
47+
resolveUrl (hostname, pathname = '') {
48+
return !this._includeHost ? `${this._rootUrl}${pathname}`
49+
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}${pathname}`
50+
}
51+
52+
// Maps a given server file to a URL
53+
async mapFileToUrl ({ path, hostname }) {
54+
// Remove the root path if specified
55+
if (path.startsWith(this._rootPath)) {
56+
path = path.substring(this._rootPath.length)
57+
}
58+
59+
// Determine the URL by chopping off everything after the dollar sign
60+
const pathname = this._removeDollarExtension(path)
61+
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname.replace(/\\/g, '/'))}`
62+
return { url, contentType: this._getContentTypeByExtension(path) }
4263
}
4364

4465
// Maps the request for a given resource and representation format to a server file
@@ -100,27 +121,6 @@ class ResourceMapper {
100121
}
101122
}
102123

103-
// Maps a given server file to a URL
104-
async mapFileToUrl ({ path, hostname }) {
105-
// Determine the URL by chopping off everything after the dollar sign
106-
let pathname = this._removeDollarExtension(path.substring(this._rootPath.length))
107-
pathname = this._replaceBackslashes(pathname)
108-
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
109-
return { url, contentType: this._getContentTypeByExtension(path) }
110-
}
111-
112-
// Gets the base file path for the given hostname
113-
getBaseFilePath (hostname) {
114-
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
115-
}
116-
117-
// Resolve a URL for the given hostname
118-
// Optionally, a pathname may be passed that will be appended to the baseUrl.
119-
resolveUrl (hostname, pathname = '') {
120-
return !this._includeHost ? `${this._rootUrl}${pathname}`
121-
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}${pathname}`
122-
}
123-
124124
// Determine the full file path corresponding to a URL
125125
_getFilePath (url) {
126126
const { pathname, hostname } = this._parseUrl(url)
@@ -163,10 +163,6 @@ class ResourceMapper {
163163
const dollarPos = path.lastIndexOf('$')
164164
return dollarPos < 0 ? path : path.substr(0, dollarPos)
165165
}
166-
167-
_replaceBackslashes (path) {
168-
return path.replace(/\\/g, '/')
169-
}
170166
}
171167

172168
module.exports = ResourceMapper

lib/server-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function printDebugInfo (options) {
2020
debug.settings('Config path: ' + options.configPath)
2121
debug.settings('Suffix Acl: ' + options.suffixAcl)
2222
debug.settings('Suffix Meta: ' + options.suffixMeta)
23-
debug.settings('Filesystem Root: ' + (this.resourceMapper ? this.resourceMapper.rootPath : 'none'))
2423
debug.settings('Allow WebID authentication: ' + !!options.webid)
2524
debug.settings('Live-updates: ' + !!options.live)
2625
debug.settings('Multi-user: ' + !!options.multiuser)

0 commit comments

Comments
 (0)