ipfs / protons

Protocol Buffers for Node.js and the browser without eval
Other
32 stars 23 forks source link

feat: runtime size limits for arrays and maps #128

Closed achingbrain closed 9 months ago

achingbrain commented 9 months ago

It's possible to limit the size of arrays and maps at compile time:

message MyMessage {
  repeated uint32 repeatedField = 1 [(protons.options).limit = 10];
  map<string, string> stringMap = 2 [(protons.options).limit = 10];
}

This PR adds the ability to do it at runtime too:

const message = MyMessage.decode(buf, {
  limits: {
    repeatedField: 10,
    stringMap: 10
  }
})
github-actions[bot] commented 9 months ago

:tada: This PR is included in version 5.3.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] commented 9 months ago

:tada: This PR is included in version 7.4.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

twoeths commented 9 months ago

@achingbrain thanks this is a nice feature to add. How can I specify limits for embed field, specifically something like this in js-libp2p-gossipsub cc @wemeetagain :

export interface RPC {
  subscriptions: RPC.SubOpts[]
  messages: RPC.Message[]
  control?: RPC.ControlMessage
}

export interface ControlMessage {
    ihave: RPC.ControlIHave[]
    iwant: RPC.ControlIWant[]
    graft: RPC.ControlGraft[]
    prune: RPC.ControlPrune[]
  }

const rpc = RPC.decode(rpcBytes, {
              limits: {
                messages: this.decodeRpcLimits.maxMessages,
                subscriptions: this.decodeRpcLimits.maxSubscriptions,
                control: {
                  limits: {
                    ihave: this.decodeRpcLimits.maxIhaveMessageIDs,
                  }
                }
              }
            })

I got this error Type '{ limits: { ihave: number; }; }' is not assignable to type 'number'

achingbrain commented 9 months ago

Ah, yeah - that feature is missing. It's added by https://github.com/ipfs/protons/pull/129

You'll be able to do:

const rpc = RPC.decode(rpcBytes, {
  limits: {
    subscriptions: this.decodeRpcLimits.maxSubscriptions,
    messages: this.decodeRpcLimits.maxMessages,
    control: {
      ihave$: {
        messageIDs: this.decodeRpcLimits.maxIhaveMessageIDs
      },
      iwant$: {
        messageIDs: this.decodeRpcLimits.maxIwantMessageIDs
      },
      prune$: {
        peers: this.decodeRpcLimits.maxPeerInfos
      }
    } 
  }
})

This line:

ihave$: {
  messageIDs: this.decodeRpcLimits.maxIhaveMessageIDs
}

..limits how many messageIDs each ihave entry can have. If you want to do something more complicated like limit the combined total number of messageIDs across all ihave entries we might have to add the ability to pass a custom validation function instead.

github-actions[bot] commented 3 months ago

:tada: This issue has been resolved in version 3.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: