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

Database corruption on keys/values with backslash #1155

Closed
jeff-hykin opened this issue Oct 30, 2021 · 6 comments
Closed

Database corruption on keys/values with backslash #1155

jeff-hykin opened this issue Oct 30, 2021 · 6 comments

Comments

@jeff-hykin
Copy link
Contributor

Minimal Reproduce

NodeJS: v14.17.1

const path = require("path")
const http = require("http")
const Gun = require('gun')

const databasePort = 8765
const databasePath = path.join(__dirname, "data", "database")
const gun = Gun({
    web: http.createServer(Gun.serve(path.dirname(databasePath))).listen(databasePort),
    file: path.basename(databasePath),
})

// add some data (corrupts the database)
gun.get("vulns").get("XT\\").put({hi: "testing"},()=>{
        console.log("first one finished")
        // no issues here ^ 
        gun.get("vulns").get("XT\\").put({hi: "testing"},()=>{
            // this one ^ and any/all queries afterwords fail (data corruption)
            console.log("second one finished")
            process.exit()
        })
    })

Issue Description

Having a \ at the end of a key or a string value seems to corrupt the database.

undefined:1
"\u001bXT\\":{"
            ^

SyntaxError: Unexpected token : in JSON at position 12
    at JSON.parse (<anonymous>)
    at parse (/Users/jeffhykin/repos/vulerabilities_visualized/node_modules/.pnpm/gun@0.2020.1235/node_modules/gun/lib/yson.js:42:55)
    at yson.parseAsync (/Users/jeffhykin/repos/vulerabilities_visualized/node_modules/.pnpm/gun@0.2020.1235/node_modules/gun/lib/yson.js:12:2)
    at p.read (/Users/jeffhykin/repos/vulerabilities_visualized/node_modules/.pnpm/gun@0.2020.1235/node_modules/gun/lib/radisk.js:389:6)
    at /Users/jeffhykin/repos/vulerabilities_visualized/node_modules/.pnpm/gun@0.2020.1235/node_modules/gun/lib/rfs.js:40:4
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:73:3)

The error is thrown from yson.js

Its line #33 if its a string value that ends with \

Its line #42 if the key ends with \

Screen Shot 2021-10-30 at 2 21 17 PM

Currently my only fix is to delete the whole database and remove \ from all strings going to the database

@amark
Copy link
Owner

amark commented Oct 31, 2021

@jeff-hykin thank you! Very important catch, I'll be checking into the parsing, thank you.

@Promaethius
Copy link
Contributor

I think this is what I was running into. Commenting out yson.js includes fixed the issue

@amark
Copy link
Owner

amark commented Nov 1, 2021

@Promaethius thanks for confirming. Thankfully its my fault - tho I'm hoping somebody would be willing to do a screencall with me to help get it fixed? I'm pretty overwhelmed, but this is important.

@Promaethius
Copy link
Contributor

@amark I'd be down to help, still trying to divine the purpose of yson.js

@amark
Copy link
Owner

amark commented Nov 1, 2021

@Promaethius sorry only saw this now.

YSON is a yielding JSON parser.

Native JSON parses X size data by blocking the CPU thread.

That trashes performance if the JSON file is 1MB+ size.

YSON instead parses the data in 32KB strides.

Here are some tests: https://github.com/amark/gun/blob/master/test/common.js#L81-L99

Add a new test with your \\ data. Find that it is failing with the crash, try editing YSON until fixed.

If you get stuck anywhere, I'm happy to help or jump on a call. (Twitter DMs notify my phone the fastest)

@Maygician
Copy link

Also can be noticed while working with escape sequences. Was evaluating stuff gotten from the form on another device and newline character becomes \n. (That's two characters). escape()/unescape() did solve the problem in my case. (Workaround rather)

Emad-salah added a commit to Emad-salah/gun that referenced this issue Nov 30, 2021
@Emad-salah Emad-salah mentioned this issue Nov 30, 2021
@amark amark closed this as completed in 77162fc Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants