Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZMS-172 #733

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b3d48ce
Added submission api endpoint to api docs generation
NickOvt Apr 23, 2024
c01170f
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Apr 29, 2024
3f1c3e0
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Apr 29, 2024
1a0272d
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 6, 2024
b2aaba5
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 9, 2024
7d4eb9f
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 9, 2024
3bc4530
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 16, 2024
8fdc0d3
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 27, 2024
2607896
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 30, 2024
10836eb
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Jun 5, 2024
ea9e4e0
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Jul 25, 2024
4c2f07d
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Aug 1, 2024
c0f0660
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Sep 2, 2024
ceeacc7
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Sep 5, 2024
6a9bae9
on attachment upload calculate file content hash
NickOvt Sep 13, 2024
2d8b782
make stream into separate file, refactor
NickOvt Sep 13, 2024
e3afdec
create stream during the try to store. otherwise getting stuck
NickOvt Sep 13, 2024
6515b27
refactor file content hash update
NickOvt Sep 16, 2024
daa9767
safer file content hash handling
NickOvt Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/attachments/gridstore-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const errors = require('../errors');
const log = require('npmlog');
const crypto = require('crypto');
const base64Offset = require('./base64-offset');
const FileHashCalculatorStream = require('../filehash-stream');

// Set to false to disable base64 decoding feature
const FEATURE_DECODE_ATTACHMENTS = true;
Expand Down Expand Up @@ -129,6 +130,7 @@ class GridstoreStorage {
let storeLock;

let attachmentCallback = (...args) => {
// store finished uploading, add the hash of the file contents to file metadata
if (storeLock) {
log.silly('GridStore', '[%s] UNLOCK lock=%s status=%s', instance, lockId, storeLock.success ? 'locked' : 'empty');
if (storeLock.success) {
Expand All @@ -137,6 +139,25 @@ class GridstoreStorage {
// might be already finished if retrying after delay
return;
}
if (args.length > 2) {
const calculatedFileContentHash = args[2];

if (calculatedFileContentHash) {
this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
},
{
$set: {
'metadata.fileContentHash': calculatedFileContentHash
}
},
{
returnDocument: 'after'
}
);
}
}
callback(...args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a potential race condition if we want to use the sha256 hash immediately after the attachment has been uploaded - there's a chance that the findOneAndUpdate call is not finished yet, and thus, the value is not available.

});
// unset variable to prevent double releasing
Expand All @@ -159,6 +180,8 @@ class GridstoreStorage {
return;
}

let fileHashCalculator = new FileHashCalculatorStream();

this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
Expand Down Expand Up @@ -282,13 +305,13 @@ class GridstoreStorage {
attachmentCallback(err);
});

store.once('finish', () => attachmentCallback(null, id));
store.once('finish', () => attachmentCallback(null, id, fileHashCalculator.hash));

if (!metadata.decoded) {
store.end(attachment.body);
} else {
let decoder = new libbase64.Decoder();
decoder.pipe(store);
decoder.pipe(fileHashCalculator).pipe(store);
decoder.once('error', err => {
// pass error forward
store.emit('error', err);
Expand Down
38 changes: 38 additions & 0 deletions lib/filehash-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const Transform = require('stream').Transform;
const crypto = require('crypto');

class FileHashCalculatorStream extends Transform {
constructor(options) {
super(options);
this.bodyHash = crypto.createHash('sha256');
this.hash = null;
}

updateHash(chunk) {
this.bodyHash.update(chunk);
}

_transform(chunk, encoding, callback) {
if (!chunk || !chunk.length) {
return callback();
}

if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, encoding);
}

this.updateHash(chunk);
this.push(chunk);

callback();
}

_flush(done) {
this.hash = this.bodyHash.digest('base64');
done();
}
}

module.exports = FileHashCalculatorStream;