paramshadow

package
v0.0.0-...-9914b64 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package paramshadow runs upstream's paramDef validation (internal/api/ paramschema) in front of the API handlers and answers a rejected request with upstream's 400 (#3742).

The middleware never changes what the handler reads: it reads the JSON body (and puts the same bytes back), decodes its own copy and validates that copy. A request it cannot validate goes on to the handler.

名前は、検証だけを走らせて判定の分かれた件数を数えていた頃(#3677)のまま にしてある。数える経路は#4172で消した。

Index

Constants

View Source
const (
	// SkipNotJSON: a POST whose body is not application/json. Upstream's
	// answer to such a body comes from fastify (no body, text/plain, an
	// unsupported type) and requireObjectBody already mirrors it.
	SkipNotJSON = "not_json"
	// SkipMultipart: a requireFile endpoint (drive/files/create). Upstream
	// takes the params from the multipart fields @fastify/multipart parsed
	// before the file part, and a repeated field becomes undefined. Go's
	// parsed form has lost that order, so the input cannot be rebuilt.
	SkipMultipart = "multipart"
	// SkipMethod: a GET to an endpoint without meta.allowGet (upstream
	// answers it before the params), or a method other than GET and POST.
	SkipMethod = "method"
	// SkipQuery: a query url.ParseQuery rejects (a ';' or a bad
	// %-escape). fast-querystring parses it differently.
	SkipQuery = "unparsable_query"
	// SkipTooLarge: the JSON body or the query is longer than the limit
	// (DefaultMaxBody). Decoding a body into Go values allocates many times
	// its size, so a body near the 1MiB API limit is not validated.
	SkipTooLarge = "too_large"
	// SkipTooManyValues: the JSON body has more tokens (keys, values and
	// brackets) than the limit (DefaultMaxValues), or the query has more
	// params. Decoding allocates per value, so a body of many tiny values
	// ({"x":[{},{},…]}) costs the most per byte.
	SkipTooManyValues = "too_many_values"
	// SkipBody: reading or decoding the body failed. JSONBodyParse has
	// validated it, so this is not expected.
	SkipBody = "body_error"
)

Skip reasons: why a request was not validated and went on to the handler.

View Source
const DefaultMaxBody = 16 << 10

DefaultMaxBody is the largest JSON body (and query) validated, in bytes. A larger one goes to the handler unvalidated (SkipTooLarge).

1件の費用の上限を、トークンの数だけでなく大きさでも抑える。64KiBの`\u`の escapeの文字列は、検証ありで1件0.52ms・311KBかかった。16KiBでは約0.14ms・ 83KB(docs/param-shadow.md)。clips/*の正当なbodyは、UTF-8のまま送れば descriptionの2048文字を4バイトの文字で埋めても約8KiB。charts/*と hashtags/*はもっと小さい(項目は最大6つで、長さに上限の無い文字列は charts/*のuserIdとhost、hashtags/*のqueryとtagだけ)。

View Source
const DefaultMaxValues = 256

DefaultMaxValues is the largest number of JSON tokens (each key, scalar value and bracket counts as one) or query params validated. A request over it goes to the handler unvalidated (SkipTooManyValues).

検証は予算や同時に走る数の枠を通さないので、1件の費用をトークンの数で抑える (#3742のレビュー1周目M-1)。正当なbodyは多くて十数トークン(clips/notesの 全項目と`i`で18、charts/*で12、hashtags/usersで16)。本家のparamDefの項目の 数に対して十分に大きく、64KiBいっぱいの`{}`の配列(約4.4万トークン)に 比べてdecodeの確保は2桁小さくなる。 例外はantennas/create・updateで、本家はkeywords・excludeKeywords・usersの 数に上限を持たないので、多く並べたbodyは超えうる。超えたbodyは拒否せず handlerへ渡す(#3748)。flash/create・updateも、scriptが約16KiBを超えると 大きさの上限を超える(#3750)。gallery/posts/create・updateのdescriptionも 本家に上限が無い(#3754)。channels/updateのpinnedNoteIdsも本家に上限が無く、 同梱のfrontendが`i`と一緒に送るbodyでは236件からトークンの上限を超える(#3758)。 pages/create・updateも、本家がcontentの数にも文字列の長さにも上限を持たず、 同梱のエディタのbodyではtextのブロック29個からトークンの上限を超える(#3772)。 users/showのuserIdsとusers/relationのuserIdの配列も本家に数の上限が無く、 `i`と一緒に送るbodyでは250件からトークンの上限を超える(#3837)。 admin/roles/create・updateも、本家がpoliciesの中身を決めず、同梱のロールの エディタは全部のポリシーを埋めて送るので、普段の保存で超える(updateは ポリシー24件から、roleIdの無いcreateは25件から。#3859)。 admin/update-metaも、本家がblockedHostsなどの一覧の数に上限を持たず、同梱の モデレーションの画面が一覧を1つずつ`i`と一緒に送るbodyでは、250行から超える (#3888)。

Variables

This section is empty.

Functions

func Middleware

func Middleware(eps map[string]paramschema.Endpoint, opts ...Option) echo.MiddlewareFunc

Middleware returns the route middleware that validates the params of the requests to the endpoints of eps (by upstream name; the route is /api/<name>) and answers a request the validator rejects with upstream's 400 INVALID_PARAM (paramschema.Error.Response) instead of reaching the handler (#3742). Install it as a route middleware after the route's credential and permission checks, where upstream runs ajv.

A request that cannot be validated (a Skip* reason, the GET cast step failing, a paramDef that does not compile, or the validator panicking) goes on to the handler, whose own checks stay. The cost of one request is bounded by the size and token limits (DefaultMaxBody, DefaultMaxValues).

Types

type Option

type Option func(*shadow)

Option changes a limit of Middleware.

func WithMaxBody

func WithMaxBody(n int) Option

WithMaxBody sets the largest JSON body (and query) validated, in bytes (DefaultMaxBody by default). A longer one goes to the handler unvalidated.

func WithMaxValues

func WithMaxValues(n int) Option

WithMaxValues sets the largest number of JSON tokens or query params validated (DefaultMaxValues by default). A request with more goes to the handler unvalidated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL