Repository navigation
Validate response-supplied file data before constructing File objectsFix/response file sinks - #55
Merged
Conversation
|
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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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
force-pushed
the
fix/response-file-sinks
branch
from
October 27, 2026 05:45
7562488 to
22f265d
Compare
dkotter
approved these changes
Oct 27, 2026
dkotter
left a comment
Contributor
There was a problem hiding this comment.
This looks good to me and tests well, thanks for the PR!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Gemini and Imagen responses can carry file data in several places:
inlineData.data,fileData.fileUri, Imagenpredictions[].bytesBase64Encodedandpredictions[].url, plus the generated audio in text-to-speech. The models passed these strings straight to the SDKFileconstructor, 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 aFileis created.Changes
ResponseFileValidatorutility (src/Util/) withisHttpUrl(),isBase64()andisMimeType(). URL validation requiresFILTER_VALIDATE_URLand 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:inlineDatarequires a valid MIME type and base64 body and is wrapped in adata:URI;fileData.fileUrimust be a valid http(s) URL. Failures raiseResponseExceptionnamingcandidates[i].content.parts[j].GoogleImageGenerationModel:mimeTypeis pattern-checked,urlmust be a valid http(s) URL,bytesBase64Encodedmust be base64 and is wrapped in adata:URI.Fileconstruction is wrapped so any failure is reported asResponseExceptionnamingpredictions[i].GoogleTextToSpeechConversionModel: the generated WAV is passed as adata:audio/wav;base64,URI rather than a bare base64 string.GoogleTextAndImageGenerationModeldelegates to the text model and inherits the change.Compatibility
Filefactories 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.1constraint 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.ResponseExceptionthat names the offending part or prediction.mimeTypestill fall back to the expected MIME type, as before.Testing
tests/Util/ResponseFileValidatorTest.phpcovers accepted and rejected inputs for each check, including mixed-case schemes and MIME types, trailing newlines, stream wrappers and empty payloads.composer phpcs,composer phpstanandcomposer testpass: 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
Fileobjects, 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.