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

@uppy/companion: do not use unsafe call to JSON.stringify #5422

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/@uppy/companion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"express-interceptor": "1.2.0",
"express-prom-bundle": "7.0.0",
"express-session": "1.18.0",
"fast-safe-stringify": "^2.1.1",
"got": "^13.0.0",
"grant": "5.4.22",
"helmet": "^7.1.0",
Expand Down
11 changes: 10 additions & 1 deletion packages/@uppy/companion/src/server/emitter/redis-emitter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { EventEmitter } = require('node:events')
const { default: safeStringify } = require('fast-safe-stringify')

const logger = require('../logger')

function replacer(key, value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

better to put replacer inside the function call so typescript can automatically infer the types for key and value, but we can also do that later when refactoring to typescript

// Remove the circular structure and internal ones
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Remove the circular structure and internal ones
// Remove the circular references and internal properties
// so that they don't get exposed to the browser (uppy client)

return key[0] === '_' || value === '[Circular]' ? undefined : value
}

/**
* This module simulates the builtin events.EventEmitter but with the use of redis.
* This is useful for when companion is running on multiple instances and events need
Expand Down Expand Up @@ -141,7 +147,10 @@ module.exports = (redisClient, redisPubSubScope) => {
* @param {string} eventName name of the event
*/
function emit (eventName, ...args) {
runWhenConnected(() => publisher.publish(getPrefixedEventName(eventName), JSON.stringify(args)))
runWhenConnected(
() => publisher.publish(getPrefixedEventName(eventName),
safeStringify(args, replacer)),
)
}

/**
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8627,6 +8627,7 @@ __metadata:
express-interceptor: "npm:1.2.0"
express-prom-bundle: "npm:7.0.0"
express-session: "npm:1.18.0"
fast-safe-stringify: "npm:^2.1.1"
got: "npm:^13.0.0"
grant: "npm:5.4.22"
helmet: "npm:^7.1.0"
Expand Down
Loading