mirror of
https://github.com/aykhans/sarin.git
synced 2026-10-13 21:28:52 +00:00
Compare commits
7
Commits
9c7b7c26a0
...
633f548e7b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
633f548e7b
|
||
|
|
68de415645
|
||
|
|
fb8443fd25
|
||
|
|
e0f6d4aeba
|
||
|
|
17dfa16051
|
||
|
|
a327c4978b
|
||
|
|
9d6e4e6bf8
|
No files matched your search
@@ -177,7 +177,7 @@ The value is a whole number of bytes, with or without a unit: `B`, `KB`, `MB`, `
|
||||
|
||||
**Examples:** `1048576`, `10MiB`, `2MB`, `512KiB`
|
||||
|
||||
A server can declare a `Content-Length` far larger than it sends, and the body is allocated from that declared length, so without a limit a single response can exhaust memory. A response above the limit fails with `body size exceeds the given limit` and is counted like any other error.
|
||||
A server can declare a `Content-Length` far larger than it sends, and the body is allocated from that declared length, so without a limit a single response can exhaust memory. A response above the limit fails with `response body exceeds the body limit` and is counted like any other error.
|
||||
|
||||
The limit applies per response in flight, and a declared Content-Length is allocated up to it, so the worst case is a few times the limit times the concurrency. It covers the requests scripts make as well.
|
||||
|
||||
@@ -609,9 +609,12 @@ Unknown options and values of the wrong type raise an error.
|
||||
| `header(name)` | First value of a header, case-insensitive; `nil`/`null` if missing |
|
||||
| `cookie(name)` | Value of a cookie from `Set-Cookie`; `nil`/`null` if missing |
|
||||
| `json()` | JavaScript only: parses the body as JSON |
|
||||
| `bytes()` | JavaScript only: the body as a `Uint8Array` |
|
||||
|
||||
In Lua, call the helpers as `res:header("name")` or `res.header("name")`.
|
||||
|
||||
> **Note:** JavaScript strings hold UTF-16, so `body` replaces bytes that are not valid UTF-8 and `body.length` counts code units rather than bytes. Use `bytes()` for binary responses and for the real byte count. Lua strings keep the bytes as they arrived.
|
||||
|
||||
**Behavior:**
|
||||
|
||||
- **Runs on every request:** the requests are sent each time `transform` runs, so every main request triggers them. Script globals persist across requests within a worker, so a script can cache a value itself (e.g. `if not token then token = login() end`).
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ Sarin supports Go templates in URL paths, methods, bodies, headers, params, cook
|
||||
|
||||
> **Note:** Templating in URL host and scheme is not supported. Only the path portion of the URL can contain templates.
|
||||
|
||||
> **Note:** Template rendering happens before the request is sent. The request timeout (`-T` / `timeout`) only governs the HTTP request itself and starts _after_ templates have finished rendering, so slow template functions (e.g. captcha solvers, remote `file_Read`) cannot cause a request timeout no matter how long they take.
|
||||
> **Note:** Template rendering happens before the request is sent. The request timeout (`-T` / `timeout`) only governs the HTTP request itself and starts _after_ templates have finished rendering, so slow template functions (e.g. captcha solvers, remote `file_Read`) cannot cause a request timeout no matter how long they take. Downloads have their own limits: 30 seconds and 64MiB per file.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
|
||||
@@ -30,20 +30,11 @@ func (size *byteSize) Set(value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalYAML accepts both a YAML integer and a string with a unit.
|
||||
// UnmarshalYAML reads the raw scalar so a size parses the same way here as on the command line.
|
||||
func (size *byteSize) UnmarshalYAML(node *yaml.Node) error {
|
||||
if node.Kind != yaml.ScalarNode {
|
||||
return fmt.Errorf("expected a size, but got %s", node.Tag)
|
||||
}
|
||||
|
||||
if node.Tag == "!!int" {
|
||||
var parsed uint64
|
||||
if err := node.Decode(&parsed); err != nil {
|
||||
return types.ErrByteSizeInvalid
|
||||
}
|
||||
*size = byteSize(parsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
return size.Set(node.Value)
|
||||
}
|
||||
@@ -652,12 +652,19 @@ func ReadAllConfigs() *Config {
|
||||
|
||||
for _, configFile := range append(envConfig.Files, cliConf.Files...) {
|
||||
fileConfig, err := parseConfigFile(configFile, 10)
|
||||
|
||||
// A nested configFile fails under its own name, not the one given on the command line.
|
||||
path := configFile.Path()
|
||||
if configFileErr, ok := errors.AsType[types.ConfigFileError](err); ok {
|
||||
path = configFileErr.Path
|
||||
}
|
||||
|
||||
_ = utilsErr.MustHandle(err,
|
||||
utilsErr.OnType(func(err types.ConfigFileReadError) error {
|
||||
cliParser.PrintHelp()
|
||||
fmt.Fprint(os.Stderr, lipgloss.Sprintln(
|
||||
StyleYellow.Render(
|
||||
fmt.Sprintf("\nFailed to read config file (%s): ", configFile.Path())+err.Error(),
|
||||
fmt.Sprintf("\nFailed to read config file (%s): ", path)+err.Error(),
|
||||
),
|
||||
))
|
||||
os.Exit(1)
|
||||
@@ -666,14 +673,14 @@ func ReadAllConfigs() *Config {
|
||||
utilsErr.OnType(func(err types.UnmarshalError) error {
|
||||
fmt.Fprint(os.Stderr, lipgloss.Sprintln(
|
||||
StyleYellow.Render(
|
||||
fmt.Sprintf("\nFailed to parse config file (%s): ", configFile.Path())+err.Error(),
|
||||
fmt.Sprintf("\nFailed to parse config file (%s): ", path)+err.Error(),
|
||||
),
|
||||
))
|
||||
os.Exit(1)
|
||||
return nil
|
||||
}),
|
||||
utilsErr.OnType(func(err types.FieldParseErrors) error {
|
||||
printParseErrors(fmt.Sprintf("CONFIG FILE '%s'", configFile.Path()), err.Errors...)
|
||||
printParseErrors(fmt.Sprintf("CONFIG FILE '%s'", path), err.Errors...)
|
||||
os.Exit(1)
|
||||
return nil
|
||||
}),
|
||||
@@ -697,7 +704,7 @@ func parseConfigFile(configFile types.ConfigFile, maxDepth int) (*Config, error)
|
||||
configFileParser := NewConfigFileParser(configFile)
|
||||
fileConfig, err := configFileParser.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, types.NewConfigFileError(configFile.Path(), err)
|
||||
}
|
||||
|
||||
if maxDepth <= 0 {
|
||||
|
||||
+50
-3
@@ -1,13 +1,16 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -235,9 +238,19 @@ func (parser ConfigFileParser) ParseYAML(data []byte) (*Config, error) {
|
||||
parsedData = &configYAML{}
|
||||
)
|
||||
|
||||
err := yaml.Unmarshal(data, &parsedData)
|
||||
if err != nil {
|
||||
return nil, types.NewUnmarshalError(err)
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
||||
decoder.KnownFields(true)
|
||||
// The decoder returns a bare io.EOF for an empty file, errors.Is would also match a wrapped one.
|
||||
if err := decoder.Decode(parsedData); err != nil && err != io.EOF { //nolint:errorlint
|
||||
return nil, types.NewUnmarshalError(yamlError(err))
|
||||
}
|
||||
|
||||
// Decode reads one document, so anything after the first "---" would be dropped silently.
|
||||
switch err := decoder.Decode(&yaml.Node{}); {
|
||||
case err == nil:
|
||||
return nil, types.NewUnmarshalError(types.ErrYAMLMultipleDocuments)
|
||||
case err != io.EOF: //nolint:errorlint
|
||||
return nil, types.NewUnmarshalError(yamlError(err))
|
||||
}
|
||||
|
||||
var fieldParseErrors []types.FieldParseError
|
||||
@@ -312,3 +325,37 @@ func (parser ConfigFileParser) ParseYAML(data []byte) (*Config, error) {
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// configYAMLTypeName is the Go type name the yaml package puts in its messages.
|
||||
var configYAMLTypeName = reflect.TypeFor[configYAML]().String()
|
||||
|
||||
// yamlError rewrites yaml decode errors so they name config keys instead of Go types.
|
||||
func yamlError(err error) error {
|
||||
var loadErrors *yaml.LoadErrors
|
||||
if !errors.As(err, &loadErrors) || len(loadErrors.Errors) == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
for i, loadError := range loadErrors.Errors {
|
||||
if i > 0 {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
if loadError.Mark.Line > 0 {
|
||||
fmt.Fprintf(&builder, "line %d: ", loadError.Mark.Line)
|
||||
}
|
||||
builder.WriteString(yamlErrorMessage(loadError.Message))
|
||||
}
|
||||
|
||||
return errors.New(builder.String())
|
||||
}
|
||||
|
||||
// yamlErrorMessage turns "field x not found in type config.configYAML" into "unknown key "x"".
|
||||
func yamlErrorMessage(message string) string {
|
||||
if field, ok := strings.CutPrefix(message, "field "); ok {
|
||||
if name, _, ok := strings.Cut(field, " not found in type "); ok {
|
||||
return fmt.Sprintf("unknown key %q", name)
|
||||
}
|
||||
}
|
||||
return strings.ReplaceAll(message, configYAMLTypeName, "the config file")
|
||||
}
|
||||
@@ -60,6 +60,8 @@ func NewHostClients(
|
||||
WriteTimeout: timeout,
|
||||
ReadTimeout: timeout,
|
||||
MaxResponseBodySize: safeUint64ToInt(maxResponseBody),
|
||||
MaxIdemponentCallAttempts: 1,
|
||||
ReadBufferSize: 64 << 10, // 64 KiB
|
||||
DisableHeaderNamesNormalizing: true,
|
||||
DisablePathNormalizing: true,
|
||||
NoDefaultUserAgentHeader: true,
|
||||
@@ -83,6 +85,8 @@ func NewHostClients(
|
||||
WriteTimeout: timeout,
|
||||
ReadTimeout: timeout,
|
||||
MaxResponseBodySize: safeUint64ToInt(maxResponseBody),
|
||||
MaxIdemponentCallAttempts: 1,
|
||||
ReadBufferSize: 64 << 10, // 64 KiB
|
||||
DisableHeaderNamesNormalizing: true,
|
||||
DisablePathNormalizing: true,
|
||||
NoDefaultUserAgentHeader: true,
|
||||
|
||||
@@ -311,7 +311,7 @@ func NewSarin(
|
||||
logFile: logFile,
|
||||
hostClients: hostClients,
|
||||
scriptHTTPClients: scriptHTTPClients,
|
||||
fileCache: NewFileCache(time.Second * 10),
|
||||
fileCache: NewFileCache(time.Second * 30),
|
||||
scriptChain: scriptChain,
|
||||
}
|
||||
|
||||
|
||||
@@ -56,9 +56,11 @@ func newScriptHTTPClient(
|
||||
// Read and write timeouts stay unset because they would cap the per-call timeout,
|
||||
// so dialWithDeadline is what bounds the TLS handshake.
|
||||
return &fasthttp.Client{
|
||||
DialTimeout: dialWithDeadline(dial),
|
||||
MaxConnsPerHost: safeUintToInt(maxConns),
|
||||
MaxResponseBodySize: safeUint64ToInt(maxResponseBody),
|
||||
DialTimeout: dialWithDeadline(dial),
|
||||
MaxConnsPerHost: safeUintToInt(maxConns),
|
||||
MaxResponseBodySize: safeUint64ToInt(maxResponseBody),
|
||||
MaxIdemponentCallAttempts: 1,
|
||||
ReadBufferSize: 64 << 10, // 64 KiB
|
||||
TLSConfig: &tls.Config{
|
||||
InsecureSkipVerify: insecure, //nolint:gosec
|
||||
},
|
||||
@@ -159,7 +161,7 @@ func (c *scriptHTTPClient) Do(r *script.HTTPRequest) (*script.HTTPResponse, erro
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, fasthttp.ErrBodyTooLarge) {
|
||||
err = types.ErrScriptHTTPBodyTooLarge
|
||||
err = types.ErrResponseBodyTooLarge
|
||||
}
|
||||
return nil, types.NewScriptHTTPRequestError(r.Method, r.URL, err)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ func statusCodeToString(code int) string {
|
||||
|
||||
// responseErrorKey drops the local address, whose port would mint a stats entry per connection.
|
||||
func responseErrorKey(err error) string {
|
||||
if errors.Is(err, fasthttp.ErrBodyTooLarge) {
|
||||
return types.ErrResponseBodyTooLarge.Error()
|
||||
}
|
||||
|
||||
message := err.Error()
|
||||
|
||||
var opErr *net.OpError
|
||||
|
||||
@@ -159,6 +159,16 @@ func (e *JsEngine) httpResponseToObject(resp *HTTPResponse) *goja.Object {
|
||||
}
|
||||
return value
|
||||
})
|
||||
// body is a JavaScript string, so bytes that are not valid UTF-8 reach the script
|
||||
// only through this.
|
||||
_ = obj.Set("bytes", func(goja.FunctionCall) goja.Value {
|
||||
buffer := rt.NewArrayBuffer([]byte(resp.Body))
|
||||
value, err := rt.New(rt.Get("Uint8Array").ToObject(rt), rt.ToValue(buffer))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return value
|
||||
})
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ var (
|
||||
)
|
||||
|
||||
ErrRemoteFileTooLarge = errors.New("remote file exceeds " + FormatByteSize(RemoteFetchLimit))
|
||||
|
||||
ErrResponseBodyTooLarge = errors.New("response body exceeds the body limit")
|
||||
|
||||
ErrYAMLMultipleDocuments = errors.New("expected a single YAML document, but found more than one")
|
||||
)
|
||||
|
||||
type FieldParseError struct {
|
||||
@@ -304,6 +308,27 @@ func (e CLIUnexpectedArgsError) Error() string {
|
||||
|
||||
// ======================================== Config File ========================================
|
||||
|
||||
// ConfigFileError names the file a parse error came from, which nested configFile chains lose.
|
||||
type ConfigFileError struct {
|
||||
Path string
|
||||
error error
|
||||
}
|
||||
|
||||
func NewConfigFileError(path string, err error) ConfigFileError {
|
||||
if err == nil {
|
||||
err = errNoError
|
||||
}
|
||||
return ConfigFileError{path, err}
|
||||
}
|
||||
|
||||
func (e ConfigFileError) Error() string {
|
||||
return e.error.Error()
|
||||
}
|
||||
|
||||
func (e ConfigFileError) Unwrap() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
type ConfigFileReadError struct {
|
||||
error error
|
||||
}
|
||||
@@ -415,7 +440,6 @@ var (
|
||||
ErrScriptHTTPMethodEmpty = errors.New("http method cannot be empty")
|
||||
ErrScriptHTTPURLInvalid = errors.New("URL must be an absolute http or https URL")
|
||||
ErrScriptHTTPUnknownOption = errors.New("unknown option")
|
||||
ErrScriptHTTPBodyTooLarge = errors.New("response body exceeds the body limit")
|
||||
ErrScriptJSONCycle = errors.New("cannot encode a table that contains itself")
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user