Phase 2: Implement Execution Environment Abstraction (v0.3.0)
This commit implements Phase 2 of the CHORUS Task Execution Engine development plan, providing a comprehensive execution environment abstraction layer with Docker container sandboxing support. ## New Features ### Core Sandbox Interface - Comprehensive ExecutionSandbox interface with isolated task execution - Support for command execution, file I/O, environment management - Resource usage monitoring and sandbox lifecycle management - Standardized error handling with SandboxError types and categories ### Docker Container Sandbox Implementation - Full Docker API integration with secure container creation - Transparent repository mounting with configurable read/write access - Advanced security policies with capability dropping and privilege controls - Comprehensive resource limits (CPU, memory, disk, processes, file handles) - Support for tmpfs mounts, masked paths, and read-only bind mounts - Container lifecycle management with proper cleanup and health monitoring ### Security & Resource Management - Configurable security policies with SELinux, AppArmor, and Seccomp support - Fine-grained capability management with secure defaults - Network isolation options with configurable DNS and proxy settings - Resource monitoring with real-time CPU, memory, and network usage tracking - Comprehensive ulimits configuration for process and file handle limits ### Repository Integration - Seamless repository mounting from local paths to container workspaces - Git configuration support with user credentials and global settings - File inclusion/exclusion patterns for selective repository access - Configurable permissions and ownership for mounted repositories ### Testing Infrastructure - Comprehensive test suite with 60+ test cases covering all functionality - Docker integration tests with Alpine Linux containers (skipped in short mode) - Mock sandbox implementation for unit testing without Docker dependencies - Security policy validation tests with read-only filesystem enforcement - Resource usage monitoring and cleanup verification tests ## Technical Details ### Dependencies Added - github.com/docker/docker v28.4.0+incompatible - Docker API client - github.com/docker/go-connections v0.6.0 - Docker connection utilities - github.com/docker/go-units v0.5.0 - Docker units and formatting - Associated Docker API dependencies for complete container management ### Architecture - Interface-driven design enabling multiple sandbox implementations - Comprehensive configuration structures for all sandbox aspects - Resource usage tracking with detailed metrics collection - Error handling with retryable error classification - Proper cleanup and resource management throughout sandbox lifecycle ### Compatibility - Maintains backward compatibility with existing CHORUS architecture - Designed for future integration with Phase 3 Core Task Execution Engine - Extensible design supporting additional sandbox implementations (VM, process) This Phase 2 implementation provides the foundation for secure, isolated task execution that will be integrated with the AI model providers from Phase 1 in the upcoming Phase 3 development. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
652
vendor/go.opentelemetry.io/otel/baggage/baggage.go
generated
vendored
652
vendor/go.opentelemetry.io/otel/baggage/baggage.go
generated
vendored
@@ -1,16 +1,5 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package baggage // import "go.opentelemetry.io/otel/baggage"
|
||||
|
||||
@@ -18,8 +7,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"go.opentelemetry.io/otel/internal/baggage"
|
||||
)
|
||||
@@ -32,16 +21,6 @@ const (
|
||||
listDelimiter = ","
|
||||
keyValueDelimiter = "="
|
||||
propertyDelimiter = ";"
|
||||
|
||||
keyDef = `([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)`
|
||||
valueDef = `([\x21\x23-\x2b\x2d-\x3a\x3c-\x5B\x5D-\x7e]*)`
|
||||
keyValueDef = `\s*` + keyDef + `\s*` + keyValueDelimiter + `\s*` + valueDef + `\s*`
|
||||
)
|
||||
|
||||
var (
|
||||
keyRe = regexp.MustCompile(`^` + keyDef + `$`)
|
||||
valueRe = regexp.MustCompile(`^` + valueDef + `$`)
|
||||
propertyRe = regexp.MustCompile(`^(?:\s*` + keyDef + `\s*|` + keyValueDef + `)$`)
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -61,33 +40,62 @@ type Property struct {
|
||||
// hasValue indicates if a zero-value value means the property does not
|
||||
// have a value or if it was the zero-value.
|
||||
hasValue bool
|
||||
|
||||
// hasData indicates whether the created property contains data or not.
|
||||
// Properties that do not contain data are invalid with no other check
|
||||
// required.
|
||||
hasData bool
|
||||
}
|
||||
|
||||
// NewKeyProperty returns a new Property for key.
|
||||
//
|
||||
// The passed key must be valid, non-empty UTF-8 string.
|
||||
// If key is invalid, an error will be returned.
|
||||
// However, the specific Propagators that are used to transmit baggage entries across
|
||||
// component boundaries may impose their own restrictions on Property key.
|
||||
// For example, the W3C Baggage specification restricts the Property keys to strings that
|
||||
// satisfy the token definition from RFC7230, Section 3.2.6.
|
||||
// For maximum compatibility, alphanumeric value are strongly recommended to be used as Property key.
|
||||
func NewKeyProperty(key string) (Property, error) {
|
||||
if !keyRe.MatchString(key) {
|
||||
if !validateBaggageName(key) {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
|
||||
}
|
||||
|
||||
p := Property{key: key, hasData: true}
|
||||
p := Property{key: key}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// NewKeyValueProperty returns a new Property for key with value.
|
||||
//
|
||||
// If key or value are invalid, an error will be returned.
|
||||
// The passed key must be compliant with W3C Baggage specification.
|
||||
// The passed value must be percent-encoded as defined in W3C Baggage specification.
|
||||
//
|
||||
// Notice: Consider using [NewKeyValuePropertyRaw] instead
|
||||
// that does not require percent-encoding of the value.
|
||||
func NewKeyValueProperty(key, value string) (Property, error) {
|
||||
if !keyRe.MatchString(key) {
|
||||
if !validateKey(key) {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
|
||||
}
|
||||
if !valueRe.MatchString(value) {
|
||||
|
||||
if !validateValue(value) {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
decodedValue, err := url.PathUnescape(value)
|
||||
if err != nil {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
return NewKeyValuePropertyRaw(key, decodedValue)
|
||||
}
|
||||
|
||||
// NewKeyValuePropertyRaw returns a new Property for key with value.
|
||||
//
|
||||
// The passed key must be valid, non-empty UTF-8 string.
|
||||
// The passed value must be valid UTF-8 string.
|
||||
// However, the specific Propagators that are used to transmit baggage entries across
|
||||
// component boundaries may impose their own restrictions on Property key.
|
||||
// For example, the W3C Baggage specification restricts the Property keys to strings that
|
||||
// satisfy the token definition from RFC7230, Section 3.2.6.
|
||||
// For maximum compatibility, alphanumeric value are strongly recommended to be used as Property key.
|
||||
func NewKeyValuePropertyRaw(key, value string) (Property, error) {
|
||||
if !validateBaggageName(key) {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
|
||||
}
|
||||
if !validateBaggageValue(value) {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
|
||||
@@ -95,7 +103,6 @@ func NewKeyValueProperty(key, value string) (Property, error) {
|
||||
key: key,
|
||||
value: value,
|
||||
hasValue: true,
|
||||
hasData: true,
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
@@ -112,20 +119,11 @@ func parseProperty(property string) (Property, error) {
|
||||
return newInvalidProperty(), nil
|
||||
}
|
||||
|
||||
match := propertyRe.FindStringSubmatch(property)
|
||||
if len(match) != 4 {
|
||||
p, ok := parsePropertyInternal(property)
|
||||
if !ok {
|
||||
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidProperty, property)
|
||||
}
|
||||
|
||||
p := Property{hasData: true}
|
||||
if match[1] != "" {
|
||||
p.key = match[1]
|
||||
} else {
|
||||
p.key = match[2]
|
||||
p.value = match[3]
|
||||
p.hasValue = true
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@@ -136,19 +134,15 @@ func (p Property) validate() error {
|
||||
return fmt.Errorf("invalid property: %w", err)
|
||||
}
|
||||
|
||||
if !p.hasData {
|
||||
return errFunc(fmt.Errorf("%w: %q", errInvalidProperty, p))
|
||||
}
|
||||
|
||||
if !keyRe.MatchString(p.key) {
|
||||
if !validateBaggageName(p.key) {
|
||||
return errFunc(fmt.Errorf("%w: %q", errInvalidKey, p.key))
|
||||
}
|
||||
if p.hasValue && !valueRe.MatchString(p.value) {
|
||||
return errFunc(fmt.Errorf("%w: %q", errInvalidValue, p.value))
|
||||
}
|
||||
if !p.hasValue && p.value != "" {
|
||||
return errFunc(errors.New("inconsistent value"))
|
||||
}
|
||||
if p.hasValue && !validateBaggageValue(p.value) {
|
||||
return errFunc(fmt.Errorf("%w: %q", errInvalidValue, p.value))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -164,11 +158,19 @@ func (p Property) Value() (string, bool) {
|
||||
return p.value, p.hasValue
|
||||
}
|
||||
|
||||
// String encodes Property into a string compliant with the W3C Baggage
|
||||
// String encodes Property into a header string compliant with the W3C Baggage
|
||||
// specification.
|
||||
// It would return empty string if the key is invalid with the W3C Baggage
|
||||
// specification. This could happen for a UTF-8 key, as it may contain
|
||||
// invalid characters.
|
||||
func (p Property) String() string {
|
||||
// W3C Baggage specification does not allow percent-encoded keys.
|
||||
if !validateKey(p.key) {
|
||||
return ""
|
||||
}
|
||||
|
||||
if p.hasValue {
|
||||
return fmt.Sprintf("%s%s%v", p.key, keyValueDelimiter, p.value)
|
||||
return fmt.Sprintf("%s%s%v", p.key, keyValueDelimiter, valueEscape(p.value))
|
||||
}
|
||||
return p.key
|
||||
}
|
||||
@@ -228,12 +230,17 @@ func (p properties) validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// String encodes properties into a string compliant with the W3C Baggage
|
||||
// String encodes properties into a header string compliant with the W3C Baggage
|
||||
// specification.
|
||||
func (p properties) String() string {
|
||||
props := make([]string, len(p))
|
||||
for i, prop := range p {
|
||||
props[i] = prop.String()
|
||||
props := make([]string, 0, len(p))
|
||||
for _, prop := range p {
|
||||
s := prop.String()
|
||||
|
||||
// Ignored empty properties.
|
||||
if s != "" {
|
||||
props = append(props, s)
|
||||
}
|
||||
}
|
||||
return strings.Join(props, propertyDelimiter)
|
||||
}
|
||||
@@ -250,11 +257,38 @@ type Member struct {
|
||||
hasData bool
|
||||
}
|
||||
|
||||
// NewMember returns a new Member from the passed arguments. The key will be
|
||||
// used directly while the value will be url decoded after validation. An error
|
||||
// is returned if the created Member would be invalid according to the W3C
|
||||
// Baggage specification.
|
||||
// NewMember returns a new Member from the passed arguments.
|
||||
//
|
||||
// The passed key must be compliant with W3C Baggage specification.
|
||||
// The passed value must be percent-encoded as defined in W3C Baggage specification.
|
||||
//
|
||||
// Notice: Consider using [NewMemberRaw] instead
|
||||
// that does not require percent-encoding of the value.
|
||||
func NewMember(key, value string, props ...Property) (Member, error) {
|
||||
if !validateKey(key) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidKey, key)
|
||||
}
|
||||
|
||||
if !validateValue(value) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
decodedValue, err := url.PathUnescape(value)
|
||||
if err != nil {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
return NewMemberRaw(key, decodedValue, props...)
|
||||
}
|
||||
|
||||
// NewMemberRaw returns a new Member from the passed arguments.
|
||||
//
|
||||
// The passed key must be valid, non-empty UTF-8 string.
|
||||
// The passed value must be valid UTF-8 string.
|
||||
// However, the specific Propagators that are used to transmit baggage entries across
|
||||
// component boundaries may impose their own restrictions on baggage key.
|
||||
// For example, the W3C Baggage specification restricts the baggage keys to strings that
|
||||
// satisfy the token definition from RFC7230, Section 3.2.6.
|
||||
// For maximum compatibility, alphanumeric value are strongly recommended to be used as baggage key.
|
||||
func NewMemberRaw(key, value string, props ...Property) (Member, error) {
|
||||
m := Member{
|
||||
key: key,
|
||||
value: value,
|
||||
@@ -264,11 +298,6 @@ func NewMember(key, value string, props ...Property) (Member, error) {
|
||||
if err := m.validate(); err != nil {
|
||||
return newInvalidMember(), err
|
||||
}
|
||||
decodedValue, err := url.QueryUnescape(value)
|
||||
if err != nil {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
}
|
||||
m.value = decodedValue
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -284,11 +313,7 @@ func parseMember(member string) (Member, error) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %d", errMemberBytes, n)
|
||||
}
|
||||
|
||||
var (
|
||||
key, value string
|
||||
props properties
|
||||
)
|
||||
|
||||
var props properties
|
||||
keyValue, properties, found := strings.Cut(member, propertyDelimiter)
|
||||
if found {
|
||||
// Parse the member properties.
|
||||
@@ -309,34 +334,61 @@ func parseMember(member string) (Member, error) {
|
||||
}
|
||||
// "Leading and trailing whitespaces are allowed but MUST be trimmed
|
||||
// when converting the header into a data structure."
|
||||
key = strings.TrimSpace(k)
|
||||
var err error
|
||||
value, err = url.QueryUnescape(strings.TrimSpace(v))
|
||||
if err != nil {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", err, value)
|
||||
}
|
||||
if !keyRe.MatchString(key) {
|
||||
key := strings.TrimSpace(k)
|
||||
if !validateKey(key) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidKey, key)
|
||||
}
|
||||
if !valueRe.MatchString(value) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
|
||||
|
||||
rawVal := strings.TrimSpace(v)
|
||||
if !validateValue(rawVal) {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, v)
|
||||
}
|
||||
|
||||
// Decode a percent-encoded value.
|
||||
unescapeVal, err := url.PathUnescape(rawVal)
|
||||
if err != nil {
|
||||
return newInvalidMember(), fmt.Errorf("%w: %w", errInvalidValue, err)
|
||||
}
|
||||
|
||||
value := replaceInvalidUTF8Sequences(len(rawVal), unescapeVal)
|
||||
return Member{key: key, value: value, properties: props, hasData: true}, nil
|
||||
}
|
||||
|
||||
// replaceInvalidUTF8Sequences replaces invalid UTF-8 sequences with '<27>'.
|
||||
func replaceInvalidUTF8Sequences(c int, unescapeVal string) string {
|
||||
if utf8.ValidString(unescapeVal) {
|
||||
return unescapeVal
|
||||
}
|
||||
// W3C baggage spec:
|
||||
// https://github.com/w3c/baggage/blob/8c215efbeebd3fa4b1aceb937a747e56444f22f3/baggage/HTTP_HEADER_FORMAT.md?plain=1#L69
|
||||
|
||||
var b strings.Builder
|
||||
b.Grow(c)
|
||||
for i := 0; i < len(unescapeVal); {
|
||||
r, size := utf8.DecodeRuneInString(unescapeVal[i:])
|
||||
if r == utf8.RuneError && size == 1 {
|
||||
// Invalid UTF-8 sequence found, replace it with '<27>'
|
||||
_, _ = b.WriteString("<22>")
|
||||
} else {
|
||||
_, _ = b.WriteRune(r)
|
||||
}
|
||||
i += size
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// validate ensures m conforms to the W3C Baggage specification.
|
||||
// A key is just an ASCII string, but a value must be URL encoded UTF-8,
|
||||
// returning an error otherwise.
|
||||
// A key must be an ASCII string, returning an error otherwise.
|
||||
func (m Member) validate() error {
|
||||
if !m.hasData {
|
||||
return fmt.Errorf("%w: %q", errInvalidMember, m)
|
||||
}
|
||||
|
||||
if !keyRe.MatchString(m.key) {
|
||||
if !validateBaggageName(m.key) {
|
||||
return fmt.Errorf("%w: %q", errInvalidKey, m.key)
|
||||
}
|
||||
if !valueRe.MatchString(m.value) {
|
||||
if !validateBaggageValue(m.value) {
|
||||
return fmt.Errorf("%w: %q", errInvalidValue, m.value)
|
||||
}
|
||||
return m.properties.validate()
|
||||
@@ -351,13 +403,20 @@ func (m Member) Value() string { return m.value }
|
||||
// Properties returns a copy of the Member properties.
|
||||
func (m Member) Properties() []Property { return m.properties.Copy() }
|
||||
|
||||
// String encodes Member into a string compliant with the W3C Baggage
|
||||
// String encodes Member into a header string compliant with the W3C Baggage
|
||||
// specification.
|
||||
// It would return empty string if the key is invalid with the W3C Baggage
|
||||
// specification. This could happen for a UTF-8 key, as it may contain
|
||||
// invalid characters.
|
||||
func (m Member) String() string {
|
||||
// A key is just an ASCII string, but a value is URL encoded UTF-8.
|
||||
s := fmt.Sprintf("%s%s%s", m.key, keyValueDelimiter, url.QueryEscape(m.value))
|
||||
// W3C Baggage specification does not allow percent-encoded keys.
|
||||
if !validateKey(m.key) {
|
||||
return ""
|
||||
}
|
||||
|
||||
s := m.key + keyValueDelimiter + valueEscape(m.value)
|
||||
if len(m.properties) > 0 {
|
||||
s = fmt.Sprintf("%s%s%s", s, propertyDelimiter, m.properties.String())
|
||||
s += propertyDelimiter + m.properties.String()
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -468,7 +527,7 @@ func (b Baggage) Member(key string) Member {
|
||||
}
|
||||
|
||||
// Members returns all the baggage list-members.
|
||||
// The order of the returned list-members does not have significance.
|
||||
// The order of the returned list-members is not significant.
|
||||
//
|
||||
// The returned members are not validated, as we assume the validation happened
|
||||
// when they were added to the Baggage.
|
||||
@@ -489,8 +548,8 @@ func (b Baggage) Members() []Member {
|
||||
return members
|
||||
}
|
||||
|
||||
// SetMember returns a copy the Baggage with the member included. If the
|
||||
// baggage contains a Member with the same key the existing Member is
|
||||
// SetMember returns a copy of the Baggage with the member included. If the
|
||||
// baggage contains a Member with the same key, the existing Member is
|
||||
// replaced.
|
||||
//
|
||||
// If member is invalid according to the W3C Baggage specification, an error
|
||||
@@ -546,17 +605,414 @@ func (b Baggage) Len() int {
|
||||
return len(b.list)
|
||||
}
|
||||
|
||||
// String encodes Baggage into a string compliant with the W3C Baggage
|
||||
// specification. The returned string will be invalid if the Baggage contains
|
||||
// any invalid list-members.
|
||||
// String encodes Baggage into a header string compliant with the W3C Baggage
|
||||
// specification.
|
||||
// It would ignore members where the member key is invalid with the W3C Baggage
|
||||
// specification. This could happen for a UTF-8 key, as it may contain
|
||||
// invalid characters.
|
||||
func (b Baggage) String() string {
|
||||
members := make([]string, 0, len(b.list))
|
||||
for k, v := range b.list {
|
||||
members = append(members, Member{
|
||||
s := Member{
|
||||
key: k,
|
||||
value: v.Value,
|
||||
properties: fromInternalProperties(v.Properties),
|
||||
}.String())
|
||||
}.String()
|
||||
|
||||
// Ignored empty members.
|
||||
if s != "" {
|
||||
members = append(members, s)
|
||||
}
|
||||
}
|
||||
return strings.Join(members, listDelimiter)
|
||||
}
|
||||
|
||||
// parsePropertyInternal attempts to decode a Property from the passed string.
|
||||
// It follows the spec at https://www.w3.org/TR/baggage/#definition.
|
||||
func parsePropertyInternal(s string) (p Property, ok bool) {
|
||||
// For the entire function we will use " key = value " as an example.
|
||||
// Attempting to parse the key.
|
||||
// First skip spaces at the beginning "< >key = value " (they could be empty).
|
||||
index := skipSpace(s, 0)
|
||||
|
||||
// Parse the key: " <key> = value ".
|
||||
keyStart := index
|
||||
keyEnd := index
|
||||
for _, c := range s[keyStart:] {
|
||||
if !validateKeyChar(c) {
|
||||
break
|
||||
}
|
||||
keyEnd++
|
||||
}
|
||||
|
||||
// If we couldn't find any valid key character,
|
||||
// it means the key is either empty or invalid.
|
||||
if keyStart == keyEnd {
|
||||
return
|
||||
}
|
||||
|
||||
// Skip spaces after the key: " key< >= value ".
|
||||
index = skipSpace(s, keyEnd)
|
||||
|
||||
if index == len(s) {
|
||||
// A key can have no value, like: " key ".
|
||||
ok = true
|
||||
p.key = s[keyStart:keyEnd]
|
||||
return
|
||||
}
|
||||
|
||||
// If we have not reached the end and we can't find the '=' delimiter,
|
||||
// it means the property is invalid.
|
||||
if s[index] != keyValueDelimiter[0] {
|
||||
return
|
||||
}
|
||||
|
||||
// Attempting to parse the value.
|
||||
// Match: " key =< >value ".
|
||||
index = skipSpace(s, index+1)
|
||||
|
||||
// Match the value string: " key = <value> ".
|
||||
// A valid property can be: " key =".
|
||||
// Therefore, we don't have to check if the value is empty.
|
||||
valueStart := index
|
||||
valueEnd := index
|
||||
for _, c := range s[valueStart:] {
|
||||
if !validateValueChar(c) {
|
||||
break
|
||||
}
|
||||
valueEnd++
|
||||
}
|
||||
|
||||
// Skip all trailing whitespaces: " key = value< >".
|
||||
index = skipSpace(s, valueEnd)
|
||||
|
||||
// If after looking for the value and skipping whitespaces
|
||||
// we have not reached the end, it means the property is
|
||||
// invalid, something like: " key = value value1".
|
||||
if index != len(s) {
|
||||
return
|
||||
}
|
||||
|
||||
// Decode a percent-encoded value.
|
||||
rawVal := s[valueStart:valueEnd]
|
||||
unescapeVal, err := url.PathUnescape(rawVal)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
value := replaceInvalidUTF8Sequences(len(rawVal), unescapeVal)
|
||||
|
||||
ok = true
|
||||
p.key = s[keyStart:keyEnd]
|
||||
p.hasValue = true
|
||||
|
||||
p.value = value
|
||||
return
|
||||
}
|
||||
|
||||
func skipSpace(s string, offset int) int {
|
||||
i := offset
|
||||
for ; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if c != ' ' && c != '\t' {
|
||||
break
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
var safeKeyCharset = [utf8.RuneSelf]bool{
|
||||
// 0x23 to 0x27
|
||||
'#': true,
|
||||
'$': true,
|
||||
'%': true,
|
||||
'&': true,
|
||||
'\'': true,
|
||||
|
||||
// 0x30 to 0x39
|
||||
'0': true,
|
||||
'1': true,
|
||||
'2': true,
|
||||
'3': true,
|
||||
'4': true,
|
||||
'5': true,
|
||||
'6': true,
|
||||
'7': true,
|
||||
'8': true,
|
||||
'9': true,
|
||||
|
||||
// 0x41 to 0x5a
|
||||
'A': true,
|
||||
'B': true,
|
||||
'C': true,
|
||||
'D': true,
|
||||
'E': true,
|
||||
'F': true,
|
||||
'G': true,
|
||||
'H': true,
|
||||
'I': true,
|
||||
'J': true,
|
||||
'K': true,
|
||||
'L': true,
|
||||
'M': true,
|
||||
'N': true,
|
||||
'O': true,
|
||||
'P': true,
|
||||
'Q': true,
|
||||
'R': true,
|
||||
'S': true,
|
||||
'T': true,
|
||||
'U': true,
|
||||
'V': true,
|
||||
'W': true,
|
||||
'X': true,
|
||||
'Y': true,
|
||||
'Z': true,
|
||||
|
||||
// 0x5e to 0x7a
|
||||
'^': true,
|
||||
'_': true,
|
||||
'`': true,
|
||||
'a': true,
|
||||
'b': true,
|
||||
'c': true,
|
||||
'd': true,
|
||||
'e': true,
|
||||
'f': true,
|
||||
'g': true,
|
||||
'h': true,
|
||||
'i': true,
|
||||
'j': true,
|
||||
'k': true,
|
||||
'l': true,
|
||||
'm': true,
|
||||
'n': true,
|
||||
'o': true,
|
||||
'p': true,
|
||||
'q': true,
|
||||
'r': true,
|
||||
's': true,
|
||||
't': true,
|
||||
'u': true,
|
||||
'v': true,
|
||||
'w': true,
|
||||
'x': true,
|
||||
'y': true,
|
||||
'z': true,
|
||||
|
||||
// remainder
|
||||
'!': true,
|
||||
'*': true,
|
||||
'+': true,
|
||||
'-': true,
|
||||
'.': true,
|
||||
'|': true,
|
||||
'~': true,
|
||||
}
|
||||
|
||||
// validateBaggageName checks if the string is a valid OpenTelemetry Baggage name.
|
||||
// Baggage name is a valid, non-empty UTF-8 string.
|
||||
func validateBaggageName(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return utf8.ValidString(s)
|
||||
}
|
||||
|
||||
// validateBaggageValue checks if the string is a valid OpenTelemetry Baggage value.
|
||||
// Baggage value is a valid UTF-8 strings.
|
||||
// Empty string is also a valid UTF-8 string.
|
||||
func validateBaggageValue(s string) bool {
|
||||
return utf8.ValidString(s)
|
||||
}
|
||||
|
||||
// validateKey checks if the string is a valid W3C Baggage key.
|
||||
func validateKey(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, c := range s {
|
||||
if !validateKeyChar(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func validateKeyChar(c int32) bool {
|
||||
return c >= 0 && c < int32(utf8.RuneSelf) && safeKeyCharset[c]
|
||||
}
|
||||
|
||||
// validateValue checks if the string is a valid W3C Baggage value.
|
||||
func validateValue(s string) bool {
|
||||
for _, c := range s {
|
||||
if !validateValueChar(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var safeValueCharset = [utf8.RuneSelf]bool{
|
||||
'!': true, // 0x21
|
||||
|
||||
// 0x23 to 0x2b
|
||||
'#': true,
|
||||
'$': true,
|
||||
'%': true,
|
||||
'&': true,
|
||||
'\'': true,
|
||||
'(': true,
|
||||
')': true,
|
||||
'*': true,
|
||||
'+': true,
|
||||
|
||||
// 0x2d to 0x3a
|
||||
'-': true,
|
||||
'.': true,
|
||||
'/': true,
|
||||
'0': true,
|
||||
'1': true,
|
||||
'2': true,
|
||||
'3': true,
|
||||
'4': true,
|
||||
'5': true,
|
||||
'6': true,
|
||||
'7': true,
|
||||
'8': true,
|
||||
'9': true,
|
||||
':': true,
|
||||
|
||||
// 0x3c to 0x5b
|
||||
'<': true, // 0x3C
|
||||
'=': true, // 0x3D
|
||||
'>': true, // 0x3E
|
||||
'?': true, // 0x3F
|
||||
'@': true, // 0x40
|
||||
'A': true, // 0x41
|
||||
'B': true, // 0x42
|
||||
'C': true, // 0x43
|
||||
'D': true, // 0x44
|
||||
'E': true, // 0x45
|
||||
'F': true, // 0x46
|
||||
'G': true, // 0x47
|
||||
'H': true, // 0x48
|
||||
'I': true, // 0x49
|
||||
'J': true, // 0x4A
|
||||
'K': true, // 0x4B
|
||||
'L': true, // 0x4C
|
||||
'M': true, // 0x4D
|
||||
'N': true, // 0x4E
|
||||
'O': true, // 0x4F
|
||||
'P': true, // 0x50
|
||||
'Q': true, // 0x51
|
||||
'R': true, // 0x52
|
||||
'S': true, // 0x53
|
||||
'T': true, // 0x54
|
||||
'U': true, // 0x55
|
||||
'V': true, // 0x56
|
||||
'W': true, // 0x57
|
||||
'X': true, // 0x58
|
||||
'Y': true, // 0x59
|
||||
'Z': true, // 0x5A
|
||||
'[': true, // 0x5B
|
||||
|
||||
// 0x5d to 0x7e
|
||||
']': true, // 0x5D
|
||||
'^': true, // 0x5E
|
||||
'_': true, // 0x5F
|
||||
'`': true, // 0x60
|
||||
'a': true, // 0x61
|
||||
'b': true, // 0x62
|
||||
'c': true, // 0x63
|
||||
'd': true, // 0x64
|
||||
'e': true, // 0x65
|
||||
'f': true, // 0x66
|
||||
'g': true, // 0x67
|
||||
'h': true, // 0x68
|
||||
'i': true, // 0x69
|
||||
'j': true, // 0x6A
|
||||
'k': true, // 0x6B
|
||||
'l': true, // 0x6C
|
||||
'm': true, // 0x6D
|
||||
'n': true, // 0x6E
|
||||
'o': true, // 0x6F
|
||||
'p': true, // 0x70
|
||||
'q': true, // 0x71
|
||||
'r': true, // 0x72
|
||||
's': true, // 0x73
|
||||
't': true, // 0x74
|
||||
'u': true, // 0x75
|
||||
'v': true, // 0x76
|
||||
'w': true, // 0x77
|
||||
'x': true, // 0x78
|
||||
'y': true, // 0x79
|
||||
'z': true, // 0x7A
|
||||
'{': true, // 0x7B
|
||||
'|': true, // 0x7C
|
||||
'}': true, // 0x7D
|
||||
'~': true, // 0x7E
|
||||
}
|
||||
|
||||
func validateValueChar(c int32) bool {
|
||||
return c >= 0 && c < int32(utf8.RuneSelf) && safeValueCharset[c]
|
||||
}
|
||||
|
||||
// valueEscape escapes the string so it can be safely placed inside a baggage value,
|
||||
// replacing special characters with %XX sequences as needed.
|
||||
//
|
||||
// The implementation is based on:
|
||||
// https://github.com/golang/go/blob/f6509cf5cdbb5787061b784973782933c47f1782/src/net/url/url.go#L285.
|
||||
func valueEscape(s string) string {
|
||||
hexCount := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if shouldEscape(c) {
|
||||
hexCount++
|
||||
}
|
||||
}
|
||||
|
||||
if hexCount == 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
var buf [64]byte
|
||||
var t []byte
|
||||
|
||||
required := len(s) + 2*hexCount
|
||||
if required <= len(buf) {
|
||||
t = buf[:required]
|
||||
} else {
|
||||
t = make([]byte, required)
|
||||
}
|
||||
|
||||
j := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if shouldEscape(s[i]) {
|
||||
const upperhex = "0123456789ABCDEF"
|
||||
t[j] = '%'
|
||||
t[j+1] = upperhex[c>>4]
|
||||
t[j+2] = upperhex[c&15]
|
||||
j += 3
|
||||
} else {
|
||||
t[j] = c
|
||||
j++
|
||||
}
|
||||
}
|
||||
|
||||
return string(t)
|
||||
}
|
||||
|
||||
// shouldEscape returns true if the specified byte should be escaped when
|
||||
// appearing in a baggage value string.
|
||||
func shouldEscape(c byte) bool {
|
||||
if c == '%' {
|
||||
// The percent character must be encoded so that percent-encoding can work.
|
||||
return true
|
||||
}
|
||||
return !validateValueChar(int32(c))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user