Merge pull request #29530 from overleaf/revert-29472-mfb-from-joi-to-zod-real-time

Revert "Mfb from joi to zod real time"

GitOrigin-RevId: 3bb8b01c692083c1b15d8a53066c01f0ec7af8f6
This commit is contained in:
Brian Gough
2025-11-05 11:29:52 +00:00
committed by Copybot
parent da91e15aa0
commit d10cdb96b3
7 changed files with 30 additions and 28 deletions

View File

@@ -7,7 +7,6 @@ libraries/metrics/**
libraries/o-error/**
libraries/redis-wrapper/**
libraries/settings/**
libraries/validation-tools/**
package-lock.json
package.json
patches/**

View File

@@ -19,7 +19,6 @@ COPY libraries/metrics/package.json /overleaf/libraries/metrics/package.json
COPY libraries/o-error/package.json /overleaf/libraries/o-error/package.json
COPY libraries/redis-wrapper/package.json /overleaf/libraries/redis-wrapper/package.json
COPY libraries/settings/package.json /overleaf/libraries/settings/package.json
COPY libraries/validation-tools/package.json /overleaf/libraries/validation-tools/package.json
COPY services/real-time/package.json /overleaf/services/real-time/package.json
COPY patches/ /overleaf/patches/
@@ -30,7 +29,6 @@ COPY libraries/metrics/ /overleaf/libraries/metrics/
COPY libraries/o-error/ /overleaf/libraries/o-error/
COPY libraries/redis-wrapper/ /overleaf/libraries/redis-wrapper/
COPY libraries/settings/ /overleaf/libraries/settings/
COPY libraries/validation-tools/ /overleaf/libraries/validation-tools/
COPY services/real-time/ /overleaf/services/real-time/
FROM app

View File

@@ -20,7 +20,6 @@ IMAGE_CACHE ?= $(IMAGE_REPO):cache-$(shell cat \
$(MONOREPO)/libraries/o-error/package.json \
$(MONOREPO)/libraries/redis-wrapper/package.json \
$(MONOREPO)/libraries/settings/package.json \
$(MONOREPO)/libraries/validation-tools/package.json \
$(MONOREPO)/services/real-time/package.json \
$(MONOREPO)/patches/* \
| sha256sum | cut -d '-' -f1)

View File

@@ -8,13 +8,17 @@ const WebsocketAddressManager = require('./WebsocketAddressManager')
const bodyParser = require('body-parser')
const base64id = require('base64id')
const { UnexpectedArgumentsError } = require('./Errors')
const { z, zz } = require('@overleaf/validation-tools')
const { isZodErrorLike } = require('zod-validation-error')
const Joi = require('joi')
const HOSTNAME = require('node:os').hostname()
const SERVER_PING_INTERVAL = 15000
const SERVER_PING_LATENCY_THRESHOLD = 5000
const JOI_OBJECT_ID = Joi.string()
.required()
.regex(/^[0-9a-f]{24}$/)
.message('invalid id')
let Router
module.exports = Router = {
_handleError(callback, error, client, method, attrs) {
@@ -25,11 +29,11 @@ module.exports = Router = {
attrs.client_id = client.id
attrs.err = error
attrs.method = method
if (isZodErrorLike(error)) {
if (Joi.isError(error)) {
logger.info(attrs, 'validation error')
let message = 'invalid'
try {
message = error.issues[0].message
message = error.details[0].message
} catch (e) {
// ignore unexpected errors
logger.warn({ error, e }, 'unexpected validation error')
@@ -189,7 +193,7 @@ module.exports = Router = {
if (!isDebugging) {
try {
zz.objectId().parse(projectId)
Joi.assert(projectId, JOI_OBJECT_ID)
} catch (error) {
metrics.inc('socket-io.connection', 1, {
status: client.transport,
@@ -405,11 +409,6 @@ module.exports = Router = {
})
})
const joinDocSchema = z.object({
doc_id: zz.objectId(),
fromVersion: z.number().int().optional(),
options: z.object(),
})
// Variadic. The possible arguments:
// doc_id, callback
// doc_id, fromVersion, callback
@@ -443,7 +442,14 @@ module.exports = Router = {
return Router._handleInvalidArguments(client, 'joinDoc', arguments)
}
try {
joinDocSchema.parse({ doc_id: docId, fromVersion, options })
Joi.assert(
{ doc_id: docId, fromVersion, options },
Joi.object({
doc_id: JOI_OBJECT_ID,
fromVersion: Joi.number().integer(),
options: Joi.object().required(),
})
)
} catch (error) {
return Router._handleError(callback, error, client, 'joinDoc', {
disconnect: 1,
@@ -472,7 +478,7 @@ module.exports = Router = {
return Router._handleInvalidArguments(client, 'leaveDoc', arguments)
}
try {
zz.objectId().parse(docId)
Joi.assert(docId, JOI_OBJECT_ID)
} catch (error) {
return Router._handleError(callback, error, client, 'joinDoc', {
disconnect: 1,
@@ -548,10 +554,6 @@ module.exports = Router = {
}
)
const applyOtUpdateSchema = z.object({
doc_id: zz.objectId(),
update: z.object(),
})
client.on('applyOtUpdate', function (docId, update, callback) {
if (typeof callback !== 'function') {
return Router._handleInvalidArguments(
@@ -561,7 +563,13 @@ module.exports = Router = {
)
}
try {
applyOtUpdateSchema.parse({ doc_id: docId, update })
Joi.assert(
{ doc_id: docId, update },
Joi.object({
doc_id: JOI_OBJECT_ID,
update: Joi.object().required(),
})
)
} catch (error) {
return Router._handleError(callback, error, client, 'applyOtUpdate', {
disconnect: 1,

View File

@@ -22,7 +22,6 @@
"@overleaf/o-error": "*",
"@overleaf/redis-wrapper": "*",
"@overleaf/settings": "*",
"@overleaf/validation-tools": "*",
"async": "^3.2.5",
"base64id": "0.1.0",
"body-parser": "^1.20.3",
@@ -31,12 +30,12 @@
"cookie-parser": "^1.4.6",
"express": "^4.21.2",
"express-session": "^1.17.1",
"joi": "^17.12.0",
"lodash": "^4.17.21",
"proxy-addr": "^2.0.7",
"request": "^2.88.2",
"socket.io": "github:overleaf/socket.io#0.9.19-overleaf-12",
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5",
"zod-validation-error": "^4.0.1"
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5"
},
"devDependencies": {
"chai": "^4.3.6",

View File

@@ -328,7 +328,7 @@ describe('joinDoc', function () {
})
it('should return an invalid id error', function () {
this.error.message.should.equal('invalid Mongo ObjectId')
this.error.message.should.equal('invalid id')
})
return it('should not have joined the doc room', function (done) {