Skip to content

bug: Improper server side max batch size handling #5825

Closed as not planned
Closed as not planned

Description

@kishore03109

Provide environment information

System:
OS: macOS 12.6
CPU: (8) arm64 Apple M1 Pro
Memory: 86.84 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.20.3 - ~/.nvm/versions/node/v18.20.3/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v18.20.3/bin/npm
Browsers:
Chrome: 126.0.6478.63
Edge: 126.0.2592.68
Safari: 16.0
npmPackages:
@tanstack/react-query: ^4.36.1 => 4.36.1
@trpc/client: 10.45.0 => 10.45.0
@trpc/next: 10.45.0 => 10.45.0
@trpc/react-query: 10.45.0 => 10.45.0
@trpc/server: 10.45.0 => 10.45.0
next: ^14.2.3 => 14.2.3
react: ^18.2.0 => 18.2.0
typescript: ^5.2.2 => 5.2.2

Describe the bug

Batching uses , to separate different TRPC calls, and the size of the batch is limited to the header size. A payload could be crafted to just pass in a string of commas ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, which could be in the
This can potentially lead to multiple 404s being returned for a single call. I would expect to have some sort of batch limiting that is enforced on the server side, but currently, the documentation limits it to the client side.

Link to reproduction

any trpc with batch enabled

To reproduce

eg, for the request

curl --request GET \
  --url '<url>/api/trpc/example.procedureName,,,,,,,,,,,,,,,,,,,,,,,?batch=1&input=<input that works for the first procedure>'

we get a return response

[
	{
		"result": {
			"data": {
                <valid response for the first procedure call>
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	},
	{
		"error": {
			"json": {
				"message": "No \"query\"-procedure on path \"\"",
				"code": -32004,
				"data": {
					"code": "NOT_FOUND",
					"httpStatus": 404,
					"path": "",
					"zodError": null
				}
			}
		}
	}
]

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. changed the title [-]bug: [/-] [+]bug: Improper server side max batch size handling[/+] on Jun 24, 2024
  2. KATT commented on Jun 24, 2024

    @KATT
    Member

    Your createContext receives an info-object with the batch calls, you can implement logic about that there

  3. KATT commented on Jun 24, 2024

    @KATT
    Member
      if (opts.info.calls.length > 10) {
        throw new TRPCError({
          code: 'BAD_REQUEST',
          message: `You are making ${opts.info.calls.length} requests in a single call, which is not allowed`,
        });
      }
  4. added
    👻 invalid
    on Jun 24, 2024
  5. KATT commented on Jun 24, 2024

    @KATT
    Member

    Feel free to make a PR to the documentation.

  6. KATT commented on Jun 26, 2024

    @KATT
    Member

    Closing this. Feel free to open a PR to the documentation or a more specific feature request.

    There's no bug AFAICT, this should be handled in createContext()

  7. kishore03109 commented on Jun 27, 2024

    @kishore03109
    Author

    Hey @KATT, I am not sure if I misunderstood this.

    Following the docs, one can create a context as such

    export const createContext = async (opts: CreateNextContextOptions) => {
     ...
    }
    

    This opts it typed to a CreateNextContextOptions, which only has req and res. When inspecting the object at runtime, I also could not find the key info on the opts object.

    I understand there is no documentation to refer to regarding limiting the batch size at the server side. Where can I refer to get more info of getting the info from the opt object?

  8. reopened this on Jun 27, 2024
  9. KATT commented on Jun 27, 2024

    @KATT
    Member

    Upgrade to v11 where we have that info property, we won't do any non-critical security issues on the v10 branch

    export type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
    req: TRequest;
    res: TResponse;
    info: TRPCRequestInfo;
    };

  10. KATT commented on Jun 27, 2024

    @KATT
    Member

    The main thing you need to do to upgrade

  11. kishore03109 commented on Jun 28, 2024

    @kishore03109
    Author

    Hi @KATT, thanks for the timely response!

    I do differ in opinion on this being a non-critical security issue. When this is run in a dockerised environment, using just one api call but spamming the requests within just one batch, I was able to observe some level of DOS within the service.

    The below seems to fix this in v10.

    export const createContext = async (opts: CreateNextContextOptions) => {
      const trpcReq = opts.req.query.trpc
      const req = Array.isArray(trpcReq) ? _.flatten(trpcReq) : trpcReq
      const count = req?.toString().split(',').length || 0
      if (count > 10) {
        throw new TRPCError({ code: 'TOO_MANY_REQUESTS' })
      }
    
    

    Would this be something that you would consider fixing in v10?

    [Outside the scope of this bug]
    Noticed that the v11 branch is still in beta, am wondering if there are plans to release the non-beta branch soon. It seems like some of the libraries that we are using are expecting the stable branch to be in order to release support for this.

  12. github-actions commented on Jun 29, 2024

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  13. locked as resolved and limited conversation to collaborators on Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions