Skip to content

Commit 31860bb

Browse files
committed
Require Content-Type in POST request
1 parent 0c1ce05 commit 31860bb

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

lib/handlers/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function handler (req, res, next) {
5858
const { url: putUrl } = await ldp.resourceMapper.mapFileToUrl(
5959
{ path: ldp.resourceMapper._rootPath + path.join(containerPath, filename), hostname: req.hostname })
6060
try {
61-
await ldp.put(putUrl, file)
61+
await ldp.put(putUrl, file, mimetype)
6262
} catch (err) {
6363
busboy.emit('error', err)
6464
}

lib/handlers/put.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function handler (req, res, next) {
88
res.header('MS-Author-Via', 'SPARQL')
99

1010
try {
11-
await ldp.put(req, req)
11+
await ldp.put(req, req, req.headers['content-type'])
1212
debug('succeded putting the file')
1313

1414
res.sendStatus(201)

lib/ldp.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,18 @@ class LDP {
212212
}
213213

214214
async put (url, stream, contentType) {
215-
const { path: filePath } = await this.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists: true })
216-
217215
// PUT requests not supported on containers. Use POST instead
218-
if (filePath.endsWith('/')) {
216+
if ((url.url || url).endsWith('/')) {
219217
throw error(409,
220218
'PUT not supported on containers, use POST instead')
221219
}
222220

221+
// PUT without content type is forbidden
222+
if (!contentType) {
223+
throw error(415,
224+
'PUT request require a valid content type via the Content-Type header')
225+
}
226+
223227
// First check if we are above quota
224228
let isOverQuota
225229
try {
@@ -232,6 +236,7 @@ class LDP {
232236
}
233237

234238
// Second, create the enclosing directory, if necessary
239+
const { path: filePath } = await this.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists: true })
235240
const dirName = path.dirname(filePath)
236241
try {
237242
await promisify(mkdirp)(dirName)

test/integration/acl-oidc-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe('ACL with WebID+OIDC over HTTP', function () {
9696
const options = {
9797
url: timAccountUri + path,
9898
headers: {
99-
accept: 'text/turtle'
99+
'accept': 'text/turtle',
100+
'content-type': 'text/plain'
100101
}
101102
}
102103
if (user) {

test/integration/acl-tls-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ describe('ACL with WebID+TLS', function () {
7777
var options = {
7878
url: address + path,
7979
headers: {
80-
accept: 'text/turtle'
80+
accept: 'text/turtle',
81+
'content-type': 'text/plain'
8182
}
8283
}
8384
if (user) {

test/integration/ldp-test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('LDP', function () {
124124
describe('put', function () {
125125
it.skip('should write a file in an existing dir', () => {
126126
var stream = stringToStream('hello world')
127-
return ldp.put('/resources/testPut.txt', stream).then(() => {
127+
return ldp.put('/resources/testPut.txt', stream, 'text/plain').then(() => {
128128
var found = read('testPut.txt')
129129
rm('testPut.txt')
130130
assert.equal(found, 'hello world')
@@ -133,11 +133,12 @@ describe('LDP', function () {
133133

134134
it('should fail if a trailing `/` is passed', () => {
135135
var stream = stringToStream('hello world')
136-
return ldp.put('/resources/', stream).catch(err => {
136+
return ldp.put('/resources/', stream, 'text/plain').catch(err => {
137137
assert.equal(err.status, 409)
138138
})
139139
})
140140

141+
141142
it.skip('with a larger file to exceed allowed quota', function () {
142143
var randstream = stringToStream(randomBytes(2100))
143144
return ldp.put('localhost', '/resources/testQuota.txt', randstream).catch((err) => {
@@ -150,6 +151,20 @@ describe('LDP', function () {
150151
assert.equal(err.status, 413)
151152
})
152153
})
154+
155+
it('should fail if a trailing `/` is passed without content type', () => {
156+
var stream = stringToStream('hello world')
157+
return ldp.put('/resources/', stream, null).catch(err => {
158+
assert.equal(err.status, 409)
159+
})
160+
})
161+
162+
it('should fail if no content type is passed', () => {
163+
var stream = stringToStream('hello world')
164+
return ldp.put('/resources/testPut.txt', stream, null).catch(err => {
165+
assert.equal(err.status, 415)
166+
})
167+
})
153168
})
154169

155170
describe('delete', function () {
@@ -160,7 +175,7 @@ describe('LDP', function () {
160175
it.skip('should delete a file in an existing dir', async () => {
161176
// First create a dummy file
162177
var stream = stringToStream('hello world')
163-
await ldp.put('/resources/testPut.txt', stream)
178+
await ldp.put('/resources/testPut.txt', stream, 'text/plain')
164179
// Make sure it exists
165180
fs.stat(ldp.resourceMapper._rootPath + '/resources/testPut.txt', function (err) {
166181
if (err) {
@@ -181,7 +196,7 @@ describe('LDP', function () {
181196
it.skip('should fail to delete a non-empty folder', async () => {
182197
// First create a dummy file
183198
var stream = stringToStream('hello world')
184-
await ldp.put('/resources/dummy/testPutBlocking.txt', stream)
199+
await ldp.put('/resources/dummy/testPutBlocking.txt', stream, 'text/plain')
185200
// Make sure it exists
186201
fs.stat(ldp.resourceMapper._rootPath + '/resources/dummy/testPutBlocking.txt', function (err) {
187202
if (err) {
@@ -196,7 +211,7 @@ describe('LDP', function () {
196211
it.skip('should fail to delete nested non-empty folders', async () => {
197212
// First create a dummy file
198213
var stream = stringToStream('hello world')
199-
await ldp.put('/resources/dummy/dummy2/testPutBlocking.txt', stream)
214+
await ldp.put('/resources/dummy/dummy2/testPutBlocking.txt', stream, 'text/plain')
200215
// Make sure it exists
201216
fs.stat(ldp.resourceMapper._rootPath + '/resources/dummy/dummy2/testPutBlocking.txt', function (err) {
202217
if (err) {

0 commit comments

Comments
 (0)