Skip to content

Validate response-supplied file data before constructing File objectsFix/response file sinks - #55

Merged
dkotter merged 3 commits into
WordPress:trunkfrom
saarnilauri:fix/response-file-sinks
Oct 27, 2026
Merged

dkotter merged 3 commits into
WordPress:trunkfrom
saarnilauri:fix/response-file-sinks

Conversation

@saarnilauri

Copy link
Copy Markdown
Contributor

Summary

Gemini and Imagen responses can carry file data in several places: inlineData.data, fileData.fileUri, Imagen predictions[].bytesBase64Encoded and predictions[].url, plus the generated audio in text-to-speech. The models passed these strings straight to the SDK File constructor, which decides by inspection whether a string is a URL, a data URI, a local path or base64 data. Response data should not be left to that detection. This PR validates every such value and makes the intended interpretation explicit before a File is created.

Changes

  • New ResponseFileValidator utility (src/Util/) with isHttpUrl(), isBase64() and isMimeType(). URL validation requires FILTER_VALIDATE_URL and an http(s) scheme, mirroring the SDK's own URL detection. The base64 and MIME type grammars match the SDK's as well, including empty payloads.
  • GoogleTextGenerationModel: inlineData requires a valid MIME type and base64 body and is wrapped in a data: URI; fileData.fileUri must be a valid http(s) URL. Failures raise ResponseException naming candidates[i].content.parts[j].
  • GoogleImageGenerationModel: mimeType is pattern-checked, url must be a valid http(s) URL, bytesBase64Encoded must be base64 and is wrapped in a data: URI. File construction is wrapped so any failure is reported as ResponseException naming predictions[i].
  • GoogleTextToSpeechConversionModel: the generated WAV is passed as a data:audio/wav;base64, URI rather than a bare base64 string.
  • GoogleTextAndImageGenerationModel delegates to the text model and inherits the change.

Compatibility

  • No new SDK API is expected. The explicit File factories proposed in Harden File construction: explicit factories, strict array shapes, deprecate implicit filesystem access php-ai-client#308 are not required here, so the ^1.3.1 constraint is unchanged and the plugin keeps working with the SDK bundled in WordPress 7.0 and 7.1. Once that SDK change is the minimum supported version, the wrapping here can be replaced by those factories.
  • Well-formed responses parse exactly as before. Responses with malformed file references, which previously failed later or with a less specific message, now fail with a ResponseException that names the offending part or prediction.
  • Imagen predictions with a non-string mimeType still fall back to the expected MIME type, as before.

Testing

  • New tests/Util/ResponseFileValidatorTest.php covers accepted and rejected inputs for each check, including mixed-case schemes and MIME types, trailing newlines, stream wrappers and empty payloads.
  • New and extended model tests cover accepted http(s) references, malformed URLs, empty and newline-terminated payloads, the reported index, and that a response value matching an existing file name is never read.
  • composer phpcs, composer phpstan and composer test pass: 87 tests, 190 assertions.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Mythos 5.1 (implementation), GPT-6 Astra (independent review)
Used for: Identifying the response parsing sites that construct File objects, implementing the validation and tests, and an independent review pass whose findings (full URL validation, error wrapping with the prediction index, the text-to-speech sink, empty payloads, test coverage) were then addressed. Final review, tweaking and testing done by me.

@saarnilauri
saarnilauri requested a review from a team October 27, 2026 05:44
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

saarnilauri and others added 3 commits October 27, 2026 07:45
Provider responses are untrusted. The SDK File constructor (through 1.5.0)
treats any string that is an existing local path as a file to read, so
inlineData.data, fileData.fileUri and Imagen prediction url values from a
malicious or compromised endpoint could make the site read local files.

Text model: inlineData now requires a valid mimeType and base64 data and is
wrapped in a data URI before reaching File; fileData.fileUri must be an
http(s) URL. Image model: prediction url must be an http(s) URL,
bytesBase64Encoded must be base64, mimeType must be well-formed.

This works with every SDK version the plugin supports, including the 1.3.1
bundled in WordPress 7.0 and 7.1, because it validates before the SDK sees
the string and uses only the existing constructor.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
â€Ķtruction

- Require FILTER_VALIDATE_URL in addition to the http(s) scheme check for
  Imagen prediction URLs and Gemini fileData.fileUri, so malformed values
  never fall through to the SDK's other File detection branches.
- Wrap Imagen File construction failures in ResponseException naming
  predictions[i], consistent with the Gemini part errors.
- Allow empty base64 payloads, matching the SDK.
- Wrap the generated text-to-speech WAV in a data URI so its base64 form is
  never interpreted as a local file name.
- Add tests for accepted http(s) URLs, mixed-case schemes and MIME types,
  malformed URLs, empty payloads, trailing newlines, local-file collisions
  and the reported index.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
Move the duplicated URL, base64 and MIME type patterns and the isHttpUrl()
helper from GoogleImageGenerationModel and GoogleTextGenerationModel into a
single final utility class, and cover it with its own unit tests.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
@saarnilauri
saarnilauri force-pushed the fix/response-file-sinks branch from 7562488 to 22f265d Compare October 27, 2026 05:45
@jeffpaul jeffpaul added this to the 1.3.0 milestone Oct 27, 2026
@jeffpaul
jeffpaul requested review from gziolo and superdav42 October 27, 2026 13:32

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and tests well, thanks for the PR!

@dkotter
dkotter merged commit 18de1b5 into WordPress:trunk Oct 27, 2026
14 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.

3 participants