From f7d9c75cfb969de4a709ffb554d3c6099066931a Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Mon, 18 Jul 2022 23:03:56 +0300 Subject: [PATCH] Use buffers for BIMI docs --- lib/bimi-handler.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/bimi-handler.js b/lib/bimi-handler.js index dd261c84..e3fb1a11 100644 --- a/lib/bimi-handler.js +++ b/lib/bimi-handler.js @@ -5,6 +5,7 @@ const https = require('https'); const { validateSvg } = require('mailauth/lib/bimi/validate-svg'); const { vmc } = require('@postalsys/vmc'); const { formatDomain, getAlignment } = require('mailauth/lib/tools'); +const crypto = require('crypto'); class BimiHandler { static create(options = {}) { @@ -115,6 +116,10 @@ class BimiHandler { throw error; } + if (bimiDocument?.content?.buffer) { + bimiDocument.content = bimiDocument.content.buffer; + } + return bimiDocument; } @@ -283,6 +288,12 @@ class BimiHandler { { upsert: true, returnDocument: 'after' } ); + bimiDocument = r?.value; + + if (bimiDocument?.content?.buffer) { + bimiDocument.content = bimiDocument.content.buffer; + } + return r && r.value; } @@ -337,6 +348,28 @@ class BimiHandler { throw error; } + if (locationStatus === 'fulfilled' && locationValue?.content && authorityValue.vmc?.hashAlgo && authorityValue.vmc?.validHash) { + console.log(locationValue.content); + let hash = crypto + .createHash(authorityValue.vmc.hashAlgo) + //sss + .update(locationValue.content) + .digest('hex'); + if (hash === authorityValue.vmc.hashValue) { + // logo files match, so location URL is safe to use + authorityValue.locationUrl = bimiData.location; + } else { + let error = new Error('Logo files from l= and a= do not match'); + error.details = { + locationHash: hash, + authorityHash: authorityValue.vmc.hashValue, + hashAlgo: authorityValue.vmc.hashAlgo + }; + error.code = 'LOGO_HASH_MISMATCH'; + throw error; + } + } + return authorityValue; } @@ -348,7 +381,6 @@ module.exports = BimiHandler; /* const db = require('./db'); - db.connect(() => { let bimi = BimiHandler.create({ database: db.database