0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 16:22:44 +00:00

Add MD5 hash check

This commit is contained in:
Trevor Buckner
2025-07-07 21:00:03 +00:00
parent 89ce4de354
commit 4c897fdeb5
2 changed files with 6 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ const React = require('react');
const _ = require('lodash');
const createClass = require('create-react-class');
import {makePatches, applyPatches, stringifyPatches, parsePatches} from '@sanity/diff-match-patch';
import { md5 } from 'hash-wasm';
import request from '../../utils/request-middleware.js';
const { Meta } = require('vitreum/headtags');
@@ -263,6 +264,7 @@ const EditPage = createClass({
brew.pageCount = ((brew.renderer=='legacy' ? brew.text.match(/\\page/g) : brew.text.match(/^\\page$/gm)) || []).length + 1;
brew.patches = makePatches(this.savedBrew.text, brew.text);
brew.hash = await md5(this.savedBrew.text);
brew.text = undefined;
brew.textBin = undefined;
console.log('Saving Brew', brew);

View File

@@ -9,6 +9,7 @@ import yaml from 'js-yaml';
import asyncHandler from 'express-async-handler';
import { nanoid } from 'nanoid';
import {makePatches, applyPatches, stringifyPatches, parsePatch} from '@sanity/diff-match-patch';
import { md5 } from 'hash-wasm';
import { splitTextStyleAndMetadata,
brewSnippetsToJSON } from '../shared/helpers.js';
import checkClientVersion from './middleware/check-client-version.js';
@@ -338,11 +339,12 @@ const api = {
// Initialize brew from request and body, destructure query params, and set the initial value for the after-save method
const brewFromClient = api.excludePropsFromUpdate(req.body);
const brewFromServer = req.brew;
const serverHash = md5(brewFromServer.text);
if(brewFromServer.version && brewFromClient.version && brewFromServer.version > brewFromClient.version) {
if((brewFromServer?.version !== brewFromClient?.version) || (serverHash !== brewFromClient.hash)) {
console.log(`Version mismatch on brew ${brewFromClient.editId}`);
res.setHeader('Content-Type', 'application/json');
return res.status(409).send(JSON.stringify({ message: `The brew has been changed on a different device. Please save your changes elsewhere, refresh, and try again.` }));
return res.status(409).send(JSON.stringify({ message: `The server copy is out of sync with the saved brew. Please save your changes elsewhere, refresh, and try again.` }));
}
console.log(`Brewfromserver: ${JSON.stringify(brewFromServer)}`);