Add immutable content submissions

This commit is contained in:
nolash 2021-10-18 21:58:00 +02:00
parent 8f1afa094d
commit fe2a88a4e1
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 128 additions and 24 deletions

View File

@ -7,13 +7,13 @@ WORKDIR /root
RUN apk add --no-cache postgresql bash RUN apk add --no-cache postgresql bash
# copy the dependencies # copy the dependencies
COPY package.json package-lock.json . COPY package.json package-lock.json ./
RUN --mount=type=cache,mode=0755,target=/root/.npm \ RUN --mount=type=cache,mode=0755,target=/root/.npm \
npm set cache /root/.npm && \ npm set cache /root/.npm && \
npm ci npm ci
COPY webpack.config.js . COPY webpack.config.js ./
COPY tsconfig.json . COPY tsconfig.json ./
## required to build the cic-client-meta module ## required to build the cic-client-meta module
COPY . . COPY . .
COPY tests/*.asc /root/pgp/ COPY tests/*.asc /root/pgp/

View File

@ -1,8 +1,9 @@
create table if not exists store ( create table if not exists store (
id serial primary key not null, id serial primary key not null,
owner_fingerprint text not null, owner_fingerprint text default null,
hash char(64) not null unique, hash char(64) not null unique,
content text not null content text not null,
mime_type text
); );
create index if not exists idx_fp on store ((lower(owner_fingerprint))); create index if not exists idx_fp on store ((lower(owner_fingerprint)));

View File

@ -1,9 +1,10 @@
create table if not exists store ( create table if not exists store (
/*id serial primary key not null,*/ /*id serial primary key not null,*/
id integer primary key autoincrement, id integer primary key autoincrement,
owner_fingerprint text not null, owner_fingerprint text default null,
hash char(64) not null unique, hash char(64) not null unique,
content text not null content text not null,
mime_type text
); );
create index if not exists idx_fp on store ((lower(owner_fingerprint))); create index if not exists idx_fp on store ((lower(owner_fingerprint)));

View File

@ -1,12 +1,13 @@
import * as Automerge from 'automerge'; import * as Automerge from 'automerge';
import * as pgp from 'openpgp'; import * as pgp from 'openpgp';
import * as crypto from 'crypto';
import { Envelope, Syncable } from '@cicnet/crdt-meta'; import { Envelope, Syncable, bytesToHex } from '@cicnet/crdt-meta';
function handleNoMergeGet(db, digest, keystore) { function handleNoMergeGet(db, digest, keystore) {
const sql = "SELECT content FROM store WHERE hash = '" + digest + "'"; const sql = "SELECT owner_fingerprint, content, mime_type FROM store WHERE hash = '" + digest + "'";
return new Promise<string|boolean>((whohoo, doh) => { return new Promise<any>((whohoo, doh) => {
db.query(sql, (e, rs) => { db.query(sql, (e, rs) => {
if (e !== null && e !== undefined) { if (e !== null && e !== undefined) {
doh(e); doh(e);
@ -16,16 +17,37 @@ function handleNoMergeGet(db, digest, keystore) {
return; return;
} }
const immutable = rs.rows[0]['owner_fingerprint'] == undefined;
let mimeType;
if (immutable) {
if (rs.rows[0]['mime_type'] === undefined) {
mimeType = 'application/octet-stream';
} else {
mimeType = rs.rows[0]['mime_type'];
}
} else {
mimeType = 'application/json';
}
const cipherText = rs.rows[0]['content']; const cipherText = rs.rows[0]['content'];
pgp.message.readArmored(cipherText).then((m) => { pgp.message.readArmored(cipherText).then((m) => {
const opts = { const opts = {
message: m, message: m,
privateKeys: [keystore.getPrivateKey()], privateKeys: [keystore.getPrivateKey()],
format: 'binary',
}; };
pgp.decrypt(opts).then((plainText) => { pgp.decrypt(opts).then((plainText) => {
console.debug('immutable ', rs.rows[0]['owner_fingerprint']);
let r;
if (immutable) {
r = plainText.data;
console.debug('data ', r, r.length);
} else {
mimeType = 'application/json';
const o = Syncable.fromJSON(plainText.data); const o = Syncable.fromJSON(plainText.data);
const r = JSON.stringify(o.m['data']); r = JSON.stringify(o.m['data']);
whohoo(r); }
whohoo([r, mimeType]);
}).catch((e) => { }).catch((e) => {
console.error('decrypt', e); console.error('decrypt', e);
doh(e); doh(e);
@ -201,10 +223,58 @@ function handleClientMergePut(data, db, digest, keystore, signer) {
}); });
} }
function handleImmutablePost(data, db, digest, keystore, contentType) {
return new Promise<boolean>((whohoo, doh) => {
handleNoMergeGet(db, digest, keystore).then((haveDigest) => {
if (haveDigest !== false) {
whohoo(false);
return;
}
let data_binary = data;
let message;
if (typeof(data) == 'string') {
data_binary = new TextEncoder().encode(data);
message = pgp.message.fromText(data);
} else {
message = pgp.message.fromBinary(data);
}
const h = crypto.createHash('sha256');
h.update(data_binary);
const z = h.digest();
const r = bytesToHex(z);
if (r != digest) {
doh('hash mismatch: ' + r + ' != ' + digest);
return;
}
const opts = {
message: message,
publicKeys: keystore.getEncryptKeys(),
};
pgp.encrypt(opts).then((cipherText) => {
const sql = "INSERT INTO store (hash, content, mime_type) VALUES ('" + digest + "', '" + cipherText.data + "', '" + contentType + "') ON CONFLICT (hash) DO UPDATE SET content = EXCLUDED.content;";
db.query(sql, (e, rs) => {
if (e !== null && e !== undefined) {
doh(e);
return;
}
whohoo(true);
});
}).catch((e) => {
doh(e);
});
}).catch((e) => {
doh(e);
});
});
}
export { export {
handleClientMergePut, handleClientMergePut,
handleClientMergeGet, handleClientMergeGet,
handleServerMergePost, handleServerMergePost,
handleServerMergePut, handleServerMergePut,
handleNoMergeGet, handleNoMergeGet,
handleImmutablePost,
}; };

View File

@ -127,8 +127,10 @@ async function processRequest(req, res) {
return; return;
} }
const mergeHeader = req.headers['x-cic-automerge'];
let mod = req.method.toLowerCase() + ":automerge:"; let mod = req.method.toLowerCase() + ":automerge:";
const mergeHeader = req.headers['x-cic-automerge'];
switch (mergeHeader) { switch (mergeHeader) {
case "client": case "client":
mod += "client"; // client handles merges mod += "client"; // client handles merges
@ -136,19 +138,33 @@ async function processRequest(req, res) {
case "server": case "server":
mod += "server"; // server handles merges mod += "server"; // server handles merges
break; break;
case "immutable":
mod += "immutable"; // server handles merges
break;
default: default:
mod += "none"; // merged object only (get only) mod += "none"; // merged object only (get only)
} }
let data = ''; // handle bigger chunks of data
let data;
req.on('data', (d) => { req.on('data', (d) => {
if (data === undefined) {
data = d;
} else {
data += d; data += d;
}
}); });
req.on('end', async () => { req.on('end', async (d) => {
console.debug('mode', mod); console.debug('hedaers ', req.headers);
let content = ''; let inputContentType = req.headers['content-type'];
let debugString = 'executing mode ' + mod ;
if (data !== undefined) {
debugString += ' for content type ' + inputContentType + ' length ' + data.length;
}
console.debug(debugString);
let content;
let contentType = 'application/json'; let contentType = 'application/json';
console.debug('handling data', data); let statusCode = 200;
let r:any = undefined; let r:any = undefined;
try { try {
switch (mod) { switch (mod) {
@ -183,12 +199,24 @@ async function processRequest(req, res) {
case 'get:automerge:none': case 'get:automerge:none':
r = await handlers.handleNoMergeGet(db, digest, keystore); r = await handlers.handleNoMergeGet(db, digest, keystore);
if (r == false) { if (r === false) {
res.writeHead(404, {"Content-Type": "text/plain"}); res.writeHead(404, {"Content-Type": "text/plain"});
res.end(); res.end();
return; return;
} }
content = r; content = r[0];
contentType = r[1];
break;
case 'post:automerge:immutable':
if (inputContentType === undefined) {
inputContentType = 'application/octet-stream';
}
r = await handlers.handleImmutablePost(data, db, digest, keystore, inputContentType);
if (r) {
statusCode = 201;
}
content = '';
break; break;
default: default:
@ -210,8 +238,12 @@ async function processRequest(req, res) {
return; return;
} }
const responseContentLength = (new TextEncoder().encode(content)).length; //let responseContentLength;
res.writeHead(200, { //if (typeof(content) == 'string') {
// (new TextEncoder().encode(content)).length;
//}
const responseContentLength = content.length;
res.writeHead(statusCode, {
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Content-Type": contentType, "Content-Type": contentType,
"Content-Length": responseContentLength, "Content-Length": responseContentLength,