hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
356 stars 73 forks source link

Error: Unexpected data: 0x44 #157

Closed iamgabrielsoft closed 2 years ago

iamgabrielsoft commented 2 years ago

Why getting this error while `

decodeFirst`

Please somebody respond to this....

hildjj commented 2 years ago

Can you give me more information, please? I think your report got its formatting confused a little.

iamgabrielsoft commented 2 years ago

yeah sure...

I was using the cbor package to decode a message but ran into this error ----> Error: Unexpected data: 0x44

iamgabrielsoft commented 2 years ago

return cbor.decodeFirst(payload) .then((obj) => { let type = options.defaultType ? options.defaultType : this.Signtag; if (obj instanceof Tagged) { if (obj.tag !== this.Signtag && obj.tag !== this.Sign1Tag) { throw new Error('Unexpected cbor tag, \'' + obj.tag + '\''); } type = obj.tag; obj = obj.value; }

});``

hildjj commented 2 years ago

Your payload includes multiple cbor data items.

const result = await cbor.decodeFirst('014401020304')

where the first byte (01) is a complete CBOR data item, and decodeFirst by default throws an error in that situation. Try this:

const extendedResult = await cbor.decodeFirst('0144', {extendedResults: true})
// { value: 1, bytes: <Buffer 01>, length: 1, unused: <Buffer 44> }
const result = extendedResult.value

Alternately, it is more efficient to treat the decoder as a stream:

const d = new cbor.Decoder()
d.on('data', value => console.log(value))
d.write(payload)
hildjj commented 2 years ago

cbor.Decoder is a class. cbor.decodeFirst is a function, which is also available as cbor.Decoder.decodeFirst.

iamgabrielsoft commented 2 years ago

Error: Unexpected data: 0x44. Sorry but till getting the error

hildjj commented 2 years ago

Can you please post the code that you're currently running?

iamgabrielsoft commented 2 years ago
  return cbor.decodeFirst(payload)
    .then((obj) => {
      let type = options.defaultType ? options.defaultType : this.Signtag;
      if (obj instanceof Tagged) {
        if (obj.tag !== this.Signtag && obj.tag !== this.Sign1Tag) {
          throw new Error('Unexpected cbor tag, \'' + obj.tag + '\'');
        }
        type = obj.tag;
        obj = obj.value;
      }

      if (!Array.isArray(obj)) {
        throw new Error('Expecting Array');
      }

      if (obj.length !== 4) {
        throw new Error('Expecting Array of lenght 4');
      }

      let [p, u, plaintext, signers] = obj;

      if (type === SignTag && !Array.isArray(signers)) {
        throw new Error('Expecting signature Array');
      }

      p = (!p.length) ? this.EMPTY_BUFFER : cbor.decodeFirstSync(p);
      u = (!u.size) ? this.EMPTY_BUFFER : u;

      let signer = (type === SignTag ? this.getSigner(signers, verifier) : signers);

      if (!signer) {
        throw new Error('Failed to find signer with kid' + verifier.key.kid);
      }

      if (type === SignTag) {
        const externalAAD = verifier.externalAAD || this.EMPTY_BUFFER;
        let [signerP, , sig] = signer;
        signerP = (!signerP.length) ? this.EMPTY_BUFFER : signerP;
        p = (!p.size) ? this.EMPTY_BUFFER : cbor.encode(p);
        const signerPMap = cbor.decode(signerP);
        const alg = signerPMap.get(Common.HeaderParameters.alg);
        const SigStructure = [
          'Signature',
          p,
          signerP,
          externalAAD,
          plaintext
        ];
        return this.doVerify(SigStructure, verifier, alg, sig)
          .then(() => {
            return plaintext;
          });
      } else {
        const externalAAD = verifier.externalAAD || this.EMPTY_BUFFER;

        const alg = this.getCommonParameter(p, u, Common.HeaderParameters.alg);
        p = (!p.size) ? this.EMPTY_BUFFER : cbor.encode(p);
        const SigStructure = [
          'Signature1',
          p,
          externalAAD,
          plaintext
        ];

        return this.doVerify(SigStructure, verifier, alg, signer)
          .then(() => {
            return plaintext;
          });
      }
    });
iamgabrielsoft commented 2 years ago

The paramters am passing are in Buffer

hildjj commented 2 years ago

change the first two lines to:

return cbor.decodeFirst(payload, {extendedResults: true})
    .then(({value: obj}) => {
iamgabrielsoft commented 2 years ago

return cbor.decodeFirst(payload, {extendedResults: true}) .then(({value: obj}) => { console.log("value", obj) })

Till the same

hildjj commented 2 years ago

what is the output of:

return cbor.decodeFirst(payload, {extendedResults: true})
.catch(e => console.log(payload.toString('hex'))
iamgabrielsoft commented 2 years ago

Error: Unexpected data: 0x44 at Decoder._parse (C:\Users\Softbook\Documents\Main Programs\Open Source\cose-js\node_modules\cbor\lib\decoder.js:357:15) at _parse.next () at Decoder.BinaryParseStream._transform (C:\Users\Softbook\Documents\Main Programs\Open Source\cose-js\node_modules\cbor\vendor\binary-parse-stream\index.js:35:31) at Decoder.Transform._read (internal/streams/transform.js:205:10) at Decoder.Transform._write (internal/streams/transform.js:193:12) at writeOrBuffer (internal/streams/writable.js:358:12) at Decoder.Writable.write (internal/streams/writable.js:303:10) at Decoder.Writable.end (internal/streams/writable.js:567:10) at Object.decodeFirst (C:\Users\Softbook\Documents\Main Programs\Open Source\cose-js\node_modules\cbor\lib\decoder.js:282:7) at Sign.verify (C:\Users\Softbook\Documents\Main Programs\Open Source\cose-js\lib\sign.js:177:32)``

hildjj commented 2 years ago

Do you have another place where you're calling decodeFirst than the one you changed?

iamgabrielsoft commented 2 years ago

yeah i do but passing a payload parameter

iamgabrielsoft commented 2 years ago

in another file

hildjj commented 2 years ago

Is your full project available on GitHub or somewhere else where I can see it?

hildjj commented 2 years ago

Or mail me a zip file to the email address in the package.json file for cbor.

iamgabrielsoft commented 2 years ago

https://github.com/iamgabrielsoft/cose-js

iamgabrielsoft commented 2 years ago

sorry this --> https://github.com/iamgabrielsoft/cose-js/blob/master/lib/sign.js#L186

hildjj commented 2 years ago

First off, you're using an ancient version of the cbor package. Please update to the latest.

iamgabrielsoft commented 2 years ago

hahaha😜

iamgabrielsoft commented 2 years ago

to what version??

iamgabrielsoft commented 2 years ago

i installed version + cbor@3.0.3

Till the same error

hildjj commented 2 years ago

Try installing cbor@8.0.0

hildjj commented 2 years ago

I use https://github.com/raineorshine/npm-check-updates to ensure all of my dependencies are up-to-date.

iamgabrielsoft commented 2 years ago

yeah siure...

now getting err UnexpectedDataError: Unexpected data: 0x44

hildjj commented 2 years ago

How are you getting this error to occur? I updated my local copy of your repo to cbor@8, pulled in the git submodules, and all of the tests pass.

iamgabrielsoft commented 2 years ago

wooow....

iamgabrielsoft commented 2 years ago

Probably am runing the example file sepately...

hildjj commented 2 years ago

Also, it looks like you changed one instance of decodeFirst (in encrypt.js), but there are several more. In particular, ensure that you've changed the one on line 188 of sign.js.

hildjj commented 2 years ago

This is likely an issue in how you are calling @erdtman's code. Perhaps you could get his help.