-
Notifications
You must be signed in to change notification settings - Fork 713
fix: validate path parameters and prevent traversal/injection in REST transcoder - tests relocated #9166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danieljbruce
merged 9 commits into
main
from
revert-9151-transcoding-path-traversal-fix-reintroduction
Aug 18, 2026
+260
−9
Merged
fix: validate path parameters and prevent traversal/injection in REST transcoder - tests relocated #9166
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5928d4
Revert "fix: validate path parameters and prevent traversal/injection…
danieljbruce 3c595c3
reintroduce the transcoding changes for vulnerability
danieljbruce 5789b39
Merge branch 'main' of https://github.com/googleapis/google-cloud-nod…
danieljbruce d17aa36
reintroduce changes verbatim that AI missed
danieljbruce 192fa6f
Undo the buildQueryStringComponents changes
danieljbruce 60586c7
re-add the transcoding test
danieljbruce 7dcf954
Apply suggestions from code review
danieljbruce 79ce8d5
obscure the tests like we did for transcoding test
danieljbruce 21f8fe7
Merge branch 'revert-9151-transcoding-path-traversal-fix-reintroducti…
danieljbruce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| import * as assert from 'assert'; | ||
| import { describe, it } from 'mocha'; | ||
| const { v3 } = require('../../../../../../packages/google-cloud-dialogflow-cx'); | ||
|
|
||
| const sinon = require('sinon'); | ||
|
|
||
| describe('Dialogflow CX Fallback Transcoding and Path Traversal Prevention', () => { | ||
| let client: any; | ||
| let fetchStub: any; | ||
|
|
||
| beforeEach(() => { | ||
| client = new v3.SessionsClient({ | ||
| fallback: true, | ||
| credentials: { client_email: 'bogus@example.com', private_key: 'bogus' }, | ||
| projectId: 'bogus', | ||
| }); | ||
| fetchStub = sinon.stub().resolves({ | ||
| ok: true, | ||
| status: 200, | ||
| arrayBuffer: () => Promise.resolve(Buffer.from('{}')), | ||
| }); | ||
| client.auth.fetch = fetchStub; | ||
| }); | ||
|
|
||
| // Test 1: Single Asterisk Dot Validation on client call | ||
| it.skip('1. should throw an error for single-asterisk segment traversal using exactly "." as session ID', async () => { | ||
| // TODO: Re-enable this test when the gax version with the new encoding is released. | ||
| await client.initialize(); | ||
| await assert.rejects( | ||
| client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/.', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }), | ||
| /Invalid value \. for session/ | ||
| ); | ||
| }); | ||
|
|
||
| // Test 2: Single Asterisk Dot-Dot Validation on client call | ||
| it.skip('2. should throw an error for single-asterisk segment traversal using exactly ".." as session ID', async () => { | ||
| // TODO: Re-enable this test when the gax version with the new encoding is released. | ||
| await client.initialize(); | ||
| await assert.rejects( | ||
| client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/..', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }), | ||
| /Invalid value \.\. for session/ | ||
| ); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| // Test 5: Standard Valid Path fallback REST call | ||
| it('5. should pass transcoding validation with a valid session path and construct the correct REST URL', async () => { | ||
| await client.initialize(); | ||
| await client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/valid-session-id', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }); | ||
| assert.strictEqual(fetchStub.callCount, 1); | ||
| const requestUrl = fetchStub.firstCall.args[0]; | ||
| assert.ok(requestUrl.includes('/v3/projects/p/locations/l/agents/a/sessions/valid-session-id:detectIntent')); | ||
| }); | ||
|
|
||
| // Test 6: Query Parameter Injection Prevention via percent-encoding | ||
| it('6. should protect against query parameter injection by percent-encoding "?" and "$" in the session ID', async () => { | ||
| await client.initialize(); | ||
| await client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/my-session?$foo=BAR#', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }); | ||
| assert.strictEqual(fetchStub.callCount, 1); | ||
| const requestUrl = fetchStub.firstCall.args[0]; | ||
| // "?" -> %3F, "$" -> %24, "=" -> %3D, "#" -> %23 | ||
| assert.ok(requestUrl.includes('my-session%3F%24foo%3DBAR%23')); | ||
| }); | ||
|
|
||
| // Test 7: Combined Path Traversal and Query Parameter Injection | ||
| it('7. should protect against path traversal and query injection by percent-encoding combined patterns', async () => { | ||
| // This request is permitted because the template uses * instead of ** | ||
| // * is supposed to match against exactly . or .. | ||
| // This is okay because we still percent encode the ? parameter. | ||
| // example: POST https://<location>-dialogflow.googleapis.com/v3/{session=projects/*/locations/*/agents/*/sessions/*}:detectIntent | ||
| await client.initialize(); | ||
| await client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/..?$foo=BAR#', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }); | ||
| assert.strictEqual(fetchStub.callCount, 1); | ||
| const requestUrl = fetchStub.firstCall.args[0]; | ||
| assert.ok(requestUrl.includes('/v3/projects/p/locations/l/agents/a/sessions/..%3F%24foo%3DBAR%23:detectIntent')); | ||
| }); | ||
|
|
||
| // Test 8: Combined Path Traversal (.) and Query Parameter Injection | ||
| it('8. should protect against path traversal and query injection by percent-encoding combined patterns using dot', async () => { | ||
| await client.initialize(); | ||
| await client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/.?$foo=BAR#', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }); | ||
| assert.strictEqual(fetchStub.callCount, 1); | ||
| const requestUrl = fetchStub.firstCall.args[0]; | ||
| assert.ok(requestUrl.includes('/v3/projects/p/locations/l/agents/a/sessions/.%3F%24foo%3DBAR%23:detectIntent')); | ||
| }); | ||
|
|
||
| // Test 9: Percent-encoding all other characters | ||
| it.skip('9. should percent-encode all other characters except unreserved ones', async () => { | ||
| // TODO: Re-enable this test when the gax version with the new encoding is released. | ||
| await client.initialize(); | ||
| await client.detectIntent({ | ||
| session: 'projects/p/locations/l/agents/a/sessions/ !@$&\'()*+,;=:%', | ||
| queryInput: { text: { text: 'hello' }, languageCode: 'en' }, | ||
| }); | ||
| assert.strictEqual(fetchStub.callCount, 1); | ||
| const requestUrl = fetchStub.firstCall.args[0]; | ||
| assert.ok(requestUrl.includes('/v3/projects/p/locations/l/agents/a/sessions/%20%21%40%24%26%27%28%29%2A%2B%2C%3B%3D%3A%25:detectIntent')); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems brittle to me; is there a place that has visibility to both packages? If not, we might want to introduce one. Can you file a ticket to track this (moving this to another directory where the dependencies can be a bit more natural -- maybe reuse your 'de-skip' issue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://b.corp.google.com/issues/548652610