ipld / edelweiss

Decentralized Protocol Compiler
Other
16 stars 6 forks source link

Value in HTTP GET ?q= is percent-encoded CBOR #61

Open lidel opened 2 years ago

lidel commented 2 years ago

Problem

The specification we've cleaned up in https://github.com/ipfs/specs/pull/326 states that the value from ?q=value is percent-encoded DAG-JSON.

Turns out the edelweiss uses percent-encoded CBOR.

Example:

This creates a special kind of hell for implementers, especially JS, where not all binary can go through the binary string js unescape/charCodeAt safely, :crying_cat_face: Original spec was aiming to mitigate it by using plain text DAG-JSON OR multibase-encoded DAG-CBOR, as we know JS libraries for these do the right thing and no data corruption can occur.

Solution

Follow the spec, switch to percent-encoded DAG-JSON, but make it backward-compatible with format sent by Kubo 0.16: If DAG-JSON fails to parse, a fallback to current (invalid) percent-encoded-CBOR-as-string parser should occur.

Test sample

Kubo 0.16 sends:

%A1tFindProvidersRequest%A1cKey%D8%2AX%25%00%01p%12+%D56%5C%E5%8Bc%8D%1Fs%AC%95%1B%F5C%40Q%08%A5%02%B0DV%B3t%5B%B6%14%D5%BB%A5%E9%B6
$ "https://cid.contact/reframe?q=%A1tFindProvidersRequest%A1cKey%D8%2AX%25%00%01p%12+%D56%5C%E5%8Bc%8D%1Fs%AC%95%1B%F5C%40Q%08%A5%02%B0DV%B3t%5B%B6%14%D5%BB%A5%E9%B6" | jq
{"FindProvidersResponse":{"Providers":[{"Node":{"peer":{"ID":{"/":{"bytes":"EiAngCqwSSL46hQ5+DWaJsZ1SPV2RwrqwID/OEuj5Rdgqw"}},"Multiaddresses":[{"/":{"bytes":"NhFlbGFzdGljLmRhZy5ob3VzZQYBu94D"}}]}},"Proto":[{"2304":{}}]}]}}
decodeURIComponent("%A1tFindProvidersRequest%A1cKey%D8%2AX%25%00%01p%12+%D56%5C%E5%8Bc%8D%1Fs%AC%95%1B%F5C%40Q%08%A5%02%B0DV%B3t%5B%B6%14%D5%BB%A5%E9%B6") 

→ URIError: malformed URI sequence