Here is the combined list of vulnerabilities, formatted as markdown:
This report combines identified vulnerabilities from multiple sources into a single, de-duplicated list for the cast
project.
-
Vulnerability Name: Incorrect Decimal Point Stripping in Integer Conversions
-
Description:
- An attacker provides an input string that represents a valid integer but ends with a decimal point (e.g., "10.").
- This input is passed to one of the
ToUint*E
orToInt*E
functions in thecast
library. - Inside these functions, the
trimZeroDecimal
function is called to remove trailing zeros and the decimal point. - The
trimZeroDecimal
function incorrectly handles strings ending with a decimal point and fails to remove it. - The modified string (e.g., "10.") is then passed to
strconv.ParseInt
for conversion to an integer. strconv.ParseInt
fails to parse the string because of the trailing decimal point, resulting in an error.- The
To*E
function returns an error, or the zero value for the target type if the non-error version is used.
-
Impact:
- Inconsistent behavior: Applications using the
cast
library might behave inconsistently when handling numerical string inputs that may or may not have trailing decimal points. - Potential application errors: If the application does not properly handle the error returned by the
To*E
functions, it could lead to unexpected application behavior or even crashes depending on how the error is used. - Type conversion failures: Legitimate numerical string inputs with a trailing decimal point will fail to be converted to integers, potentially disrupting application logic.
- Inconsistent behavior: Applications using the
-
Vulnerability Rank: High
-
Currently implemented mitigations: No specific mitigation for this vulnerability in the
trimZeroDecimal
function or in theToInt*E
/ToUint*E
functions. TheTo*E
functions do return errors when conversion fails, which is a general error handling mechanism, but not a specific mitigation for this input. -
Missing mitigations:
- The
trimZeroDecimal
function should be corrected to properly remove the trailing decimal point when it is the last character in the string. - Unit tests should be added to specifically test the
trimZeroDecimal
function with strings ending in decimal points and to test theToInt*E
andToUint*E
functions with such inputs to ensure they are handled correctly.
- The
-
Preconditions:
- An application uses the
cast
library to convert string inputs to integer types. - The application receives string inputs that are intended to be integers but might have a trailing decimal point (e.g., from user input, configuration files, external data sources).
- An application uses the
-
Source code analysis:
- File:
/code/caste.go
- Function:
trimZeroDecimal(s string) string
Thefunc trimZeroDecimal(s string) string { var foundZero bool for i := len(s); i > 0; i-- { switch s[i-1] { case '.': if foundZero { return s[:i-1] } case '0': foundZero = true default: return s } } return s }
trimZeroDecimal
function iterates backwards to remove trailing zeros after a decimal. However, if the input string ends with a decimal point (e.g., "10."), the function fails to remove it because the conditionif foundZero
is not met when the decimal point is encountered last. This is becausefoundZero
is only set totrue
when a '0' is encountered before the decimal. As a result, strings like "10." are not correctly processed, leading to parsing errors in subsequent integer conversion steps usingstrconv.ParseInt
.
- File:
-
Security test case: Vulnerability: Incorrect Decimal Point Stripping in Integer Conversions Test steps:
- Set up a Go testing environment with
quicktest
library. - Navigate to the directory containing the
cast
library code (e.g.,/code/
). - Create a new test file or modify an existing test file (e.g.,
cast_test.go
). - Add the following test function to the test file using
quicktest
framework:package cast import ( "testing" qt "github.com/frankban/quicktest" ) func TestToIntTrailingDecimalPointE(t *testing.T) { c := qt.New(t) inputString := "123." _, err := ToIntE(inputString) c.Assert(err, qt.IsNotNil) valueNonError := ToInt(inputString) c.Assert(valueNonError, qt.Equals, 0) }
- Run the test using
go test ./...
from the/code/
directory. - Observe that the test
TestToIntTrailingDecimalPointE
passes, confirming the vulnerability is present as theToIntE
function returns an error (not nil), andToInt
returns the zero value (0) for the invalid input "123.".
- Set up a Go testing environment with
- Vulnerability Name: Detailed Conversion Error Information Disclosure
- Description:
The cast library’s conversion functions (for example,
ToStringE
,ToIntE
,ToUint64E
, etc.) return error messages that include the full representation of the original input and its type (using the formatting specifiers%#v
and%T
). An external attacker who is able to supply crafted or unexpected input (for example via meta data in YAML/JSON or user‐supplied parameters in a web application that uses this library) can force a conversion failure. If the application then propagates these raw error messages to end users or logs them without proper sanitization, sensitive information (including internal data structure details and even portions of confidential input data) may be disclosed to the attacker. This information disclosure could aid the attacker in mapping internal types and conversion logic and facilitate further targeted attacks. - Impact:
- Information Disclosure: The error messages reveal internal details about the value (its exact representation and type) that was passed to the conversion function.
- Reconnaissance Aid: By knowing exact type names and representations, an attacker might learn about the internal structure and behavior of the application, which could be leveraged in subsequent attacks.
- Potential Collateral Exposure: In cases where sensitive data is fed into a conversion function before a failure occurs, portions of that data might be exposed unintentionally in error messages.
- Vulnerability Rank: High
- Currently implemented mitigations:
- There is no active mitigation in the cast library code. Every conversion function that cannot handle the input simply returns an error (for example, see the default case in
ToStringE
in/code/caste.go
where it does:).return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
- The library assumes that the calling application will handle errors appropriately. However, if these detailed error strings are exposed (for example, directly returned in HTTP responses or logged without filtering), no mitigation exists at the library level to prevent sensitive details from leaking.
- There is no active mitigation in the cast library code. Every conversion function that cannot handle the input simply returns an error (for example, see the default case in
- Missing mitigations:
- Error Message Sanitization: The library should avoid embedding the full, raw value and type (using
%#v
and%T
) in error messages when such messages might eventually be exposed outside of a trusted context. - Configurable Detail Level: An option for downstream applications to control the verbosity or sanitization of error messages could be introduced.
- Separation of Internal and External Error Reporting: Instead of directly using the unfiltered result of
%#v
and%T
, the library could log detailed messages internally while returning a generic error message (e.g., “conversion failed”) to the caller.
- Error Message Sanitization: The library should avoid embedding the full, raw value and type (using
- Preconditions:
- The application using the cast library must accept external input that is then passed to one or more of the conversion functions.
- The application (or its error‐handling/logging path) must expose conversion error messages to an external attacker (for example, as part of an API response or in an unfiltered log that an attacker can view).
- Source code analysis:
- In the
ToStringE
function (file:/code/caste.go
):- The value is first processed by the helper function
indirectToStringerOrError
. - Then, a type switch is used to select a conversion path. If none of the cases match, the default clause is executed.
- The default clause returns an error using
which directly includes the unsanitized input (using
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
%#v
) and its type (using%T
).
- The value is first processed by the helper function
- A similar pattern is present in other conversion functions (for example, in
ToInt64E
,ToUint64E
, etc.), meaning that any conversion failure in these functions yields an error message that includes detailed information about the input. - This design decision—while perhaps acceptable for debugging in a trusted environment—can become a vulnerability if the detailed error output is inadvertently exposed in a publicly available application.
- In the
- Security test case:
- Setup:
- Deploy a sample application that uses the cast library (for example, as part of processing YAML/JSON meta data) and that exposes its conversion error messages in its HTTP API responses.
- Attack Step 1:
- Send a request (via an HTTP client) that submits carefully crafted input designed to fail conversion. For example, supply an object (or a string representing an unsupported type) that does not match any of the expected cases in the conversion function.
- Attack Step 2:
- Observe the HTTP response. Verify that the error message from the conversion function is returned and examine it.
- Expected Result (Vulnerable Behavior):
- The error message contains the full printed representation of the original input and its type (e.g., messages similar to
"unable to cast <detailed input> of type <detailed type> to string"
). - Sensitive internal information (such as type names or any internal state from the input) is disclosed.
- The error message contains the full printed representation of the original input and its type (e.g., messages similar to
- Mitigation Verification:
- After implementing an error sanitization mitigation (for example, by wrapping the conversion functions to filter out detailed formatting in error strings), repeat the test.
- Verify that the error message now uses generic language (e.g., “conversion failed”) without disclosing the original input or its type.
- Setup: