Skip to content

fix(functions): return 400 for malformed execution headers - #13963

Merged
HarshMN2345 merged 3 commits into
appwrite:mainfrom
AayushKrGupta:fix/execution-headers-validation
Sep 29, 2026
Merged

HarshMN2345 merged 3 commits into
appwrite:mainfrom
AayushKrGupta:fix/execution-headers-validation

Conversation

@AayushKrGupta

@AayushKrGupta AayushKrGupta commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes two bugs causing POST /v1/functions/:functionId/executions to return HTTP 500 (general_unknown) instead of HTTP 400 when malformed headers are passed:

  1. Headers validator TypeError on non-string values:
    Headers::isValid() was calculating $size += $length + \strlen($val ?? '') without asserting the type of $val. When a caller sends a non-string value (like an array {headers: {X-Test: [bad]}}), PHP throws TypeError: strlen(): Argument #1 ($string) must be of type string, array given.
    Headers::isValid() now returns false if $val is neither a string nor null.

  2. Wrong Exception constructor arguments in Executions/Create:
    When header validation failed, the route threw:

    throw new Exception($validator->getDescription(), 400);

    Appwrite\Extend\Exception takes an error type constant as the first argument and the description as the second. Passing the description string as the type and 400 as the message left the HTTP code unset, so the exception handler fell back to an unhandled 500 error.
    This now throws Exception::GENERAL_ARGUMENT_INVALID with the validator description, matching the datetime check in the same route.

Test Plan

  • tests/unit/Functions/Validator/HeadersTest.php:
    • Added test cases rejecting array, integer, boolean values, and invalid header names.
  • tests/e2e/Services/Functions/FunctionsCustomServerTest.php:
    • Added failure cases in testCreateExecution verifying that invalid header shapes (e.g. array values, bad key names) return HTTP 400 with general_argument_invalid.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (No metadata or spec changes)

@ghost

ghost commented Sep 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The follow-up retains the existing scalar-header behavior while preserving the new rejection of array and object values.

The pull request returns HTTP 400 for malformed function execution headers by rejecting array or object values and using the invalid-argument exception type. It adds unit and end-to-end coverage for invalid values and header names. The latest commit preserves acceptance of scalar header values and revises the validator’s error message.

Latest changes: The newest commit changed header validation to allow scalar values again, updated unit expectations for integers and booleans, and clarified the validation description.

Verdict New comments Fixed Still open
💬 Commented 0 0 0

Note

@AayushKrGupta does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 4
File Change
src/Appwrite/Functions/Validator/Headers.php Rejects array and object header values and describes header constraints while allowing scalars.
src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php Returns the invalid-argument API error when header validation fails.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Covers HTTP 400 responses for invalid header values and names.
tests/unit/Functions/Validator/HeadersTest.php Covers malformed headers and confirms scalar values remain accepted.

Reviewed the commits since ec55235 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Tightens validation rules for function execution headers.

The PR should not merge until accepted scalar headers are normalized or rejected consistently with the public header contract.

Fix All in Claude CodeFindings

  1. P1 Non-string headers pass validation ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Functions/Validator/Headers.php:59
When a caller supplies a number or boolean for an allowed header such as `Content-Type`, this check now accepts it. The execution path stores the value in `requestHeaders` and forwards it to the executor without converting it to a string. The API can therefore return a non-string header value despite its string response contract, while the function receives a header value that has not been normalized.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR makes malformed execution headers return an argument-validation error instead of an unhandled error.

  • Rejects array and object header values, corrects the exception type, and adds unit and API failure cases.
  • The subsequent change also admits scalar header values that are not normalized for the execution response or executor.

Reviews (4) · Last reviewed commit: "fix(functions): keep scalar execution he..."

@AayushKrGupta
AayushKrGupta force-pushed the fix/execution-headers-validation branch from 713175e to b2be519 Compare September 28, 2026 19:29
Fixes appwrite#13962

Reject non-string and non-null values in the Headers validator to prevent a TypeError when strlen() is called on non-string values. In Executions\Create, pass Exception::GENERAL_ARGUMENT_INVALID as the exception type rather than passing the description string and integer 400 as message.
@AayushKrGupta
AayushKrGupta force-pushed the fix/execution-headers-validation branch from b2be519 to ec55235 Compare September 28, 2026 19:30
Only arrays and objects break the header size check, so numeric and
boolean values stay accepted as before. The description now follows
the other validators and mentions the leading/trailing hyphen rule.
@HarshMN2345
HarshMN2345 merged commit 6ec15e7 into appwrite:main Sep 29, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed function execution headers return HTTP 500 instead of HTTP 400

2 participants