-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blobs: prevent NULL contentType in db (#1355)
Set `blob."contentType"` values to `application/octet-stream` if no mime type is supplied on upload. Related: #1351
- Loading branch information
Showing
8 changed files
with
158 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/model/migrations/20250113-01-disable-nullable-blob-content-types.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2025 ODK Central Developers | ||
// See the NOTICE file at the top-level directory of this distribution and at | ||
// https://github.com/getodk/central-backend/blob/master/NOTICE. | ||
// This file is part of ODK Central. It is subject to the license terms in | ||
// the LICENSE file found in the top-level directory of this distribution and at | ||
// https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
// including this file, may be copied, modified, propagated, or distributed | ||
// except according to the terms contained in the LICENSE file. | ||
|
||
const up = (db) => db.raw(` | ||
UPDATE blobs SET "contentType"='application/octet-stream' WHERE "contentType" IS NULL; | ||
ALTER TABLE blobs | ||
ALTER COLUMN "contentType" SET DEFAULT 'application/octet-stream', | ||
ALTER COLUMN "contentType" SET NOT NULL | ||
`); | ||
|
||
const down = (db) => db.raw(` | ||
ALTER TABLE blobs | ||
ALTER COLUMN "contentType" DROP NOT NULL, | ||
ALTER COLUMN "contentType" DROP DEFAULT | ||
`); | ||
|
||
module.exports = { up, down }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/db-migrations/20250113-01-disable-nullable-blob-content-types.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const assert = require('node:assert/strict'); | ||
const { hash, randomBytes } = require('node:crypto'); | ||
|
||
const { // eslint-disable-line object-curly-newline | ||
assertTableContents, | ||
describeMigration, | ||
rowsExistFor, | ||
} = require('./utils'); // eslint-disable-line object-curly-newline | ||
|
||
describeMigration('20250113-01-disable-nullable-blob-content-types', ({ runMigrationBeingTested }) => { | ||
const aBlobWith = props => { | ||
const randomContent = randomBytes(100); | ||
const md5 = hash('md5', randomContent); // eslint-disable-line no-multi-spaces | ||
const sha = hash('sha1', randomContent); | ||
return { md5, sha, ...props }; | ||
}; | ||
const aBlob = () => aBlobWith({}); | ||
|
||
const blob1 = aBlobWith({ contentType: null }); | ||
const blob2 = aBlobWith({ contentType: 'text/plain' }); | ||
|
||
before(async () => { | ||
await rowsExistFor('blobs', blob1, blob2); | ||
|
||
await runMigrationBeingTested(); | ||
}); | ||
|
||
it('should change existing NULL contentType values to application/octet-stream, and preserve non-NULL values', async () => { | ||
await assertTableContents('blobs', | ||
{ ...blob1, contentType: 'application/octet-stream' }, | ||
{ ...blob2, contentType: 'text/plain' }, | ||
); | ||
}); | ||
|
||
it(`should create new blobs with contentType 'application/octet-stream' (contentType not supplied)`, async () => { | ||
const { md5, sha } = aBlob(); | ||
|
||
const created = await db.oneFirst(sql` | ||
INSERT INTO blobs (md5, sha) | ||
VALUES(${md5}, ${sha}) | ||
RETURNING "contentType" | ||
`); | ||
|
||
assert.equal(created, 'application/octet-stream'); | ||
}); | ||
|
||
it(`should create new blobs with contentType 'application/octet-stream' (supplied DEFAULT contentType)`, async () => { | ||
const { md5, sha } = aBlob(); | ||
|
||
const created = await db.oneFirst(sql` | ||
INSERT INTO blobs (md5, sha, "contentType") | ||
VALUES(${md5}, ${sha}, DEFAULT) | ||
RETURNING "contentType" | ||
`); | ||
|
||
assert.equal(created, 'application/octet-stream'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters