Skip to content

The current design of "use server" and combined with query() is inherintly type unsafe #507

@samualtnorman

Description

@samualtnorman

Describe the bug

The expected usage of query() with SolidStart's "use server" looks something like this:

const getUser = query(async (email: string) => {
	"use server"
	// `email` is typed as a `string` here which makes sense because that's what it's declared as in the parameter
	// ...
}, `get-user`)

In the client code the DX is very good, when you go to use getUser() you will get type errors for passing in the wrong type and the return type of getUser() can be automatically inferred.

The problem with this however is that once you cross the boundary from the client to the server, the passed in data can no longer be trusted. email might not be a string yet that's what it's typed as, this is type unsafe and could potentially lead to people writing security vulnerabilities.

Something I do in the codebase for the company I work at's app is to do something like this:

const getUser = query(async (email: unknown) => {
	"use server"
	// `email` is typed as `unknown` which is good because it will force me to validate it first before using it
	// ...
}, `get-user`)

This comes with its own problems though, the return type is still inferred which is good but the client is allowed to accidentally pass in a number and typescript won't let you know you made a mistake because number is assignable to unknown.

This problem currently does not have any easy workarounds that I'm aware of.

Ideally there should be some middle ground, like maybe query() could let you pass in a type parameter that decides what the argument types are on the outside, but inside the function the parameters are still typed as unknown and the inferred return type stays intact.

Your Example Website or App

see code examples above

Steps to Reproduce the Bug or Issue

see code examples above

Expected behavior

For the default way of writing "use server" query()s to be type safe.

Screenshots or Videos

No response

Platform

@solidjs/meta 0.29.4
@solidjs/router 0.15.2
@solidjs/start 1.0.10
solid-js 1.9.3

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions