-
Notifications
You must be signed in to change notification settings - Fork 700
fix: validate path parameters and prevent traversal/injection in REST transcoder #9151
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
Open
danieljbruce
wants to merge
34
commits into
main
Choose a base branch
from
transcoding-path-traversal-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−9
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
23d2854
fix: secure URL transcoding path traversal vulnerability in REST fall…
google-labs-jules[bot] 0e1f219
Apply suggestions from code review
danieljbruce ebd4779
Add a comment about changing the applyPattern signature
danieljbruce 9759c50
Merge branch 'jules-transcoding-path-traversal-vuln-fix-6401005213542…
danieljbruce 70c928d
Move the transcoding tests
danieljbruce ae95e73
Move the wildcards definition to later
danieljbruce 08d13b0
Add another test with the exact pattern
danieljbruce 94095ab
Adjust the code to fail when the user tries to traverse the path
danieljbruce d33163b
Revert "Adjust the code to fail when the user tries to traverse the p…
danieljbruce c898db0
Add comments to the last test
danieljbruce ea99463
Revert "Add comments to the last test"
danieljbruce ca4f260
Reapply "Adjust the code to fail when the user tries to traverse the …
danieljbruce af94646
Revert "Reapply "Adjust the code to fail when the user tries to trave…
danieljbruce 89c693f
Reapply "Add comments to the last test"
danieljbruce 3e46c57
Add another comment about percent encoding
danieljbruce fcc25cc
Add two tests for transversal
danieljbruce 94131e5
Add comment for encoding using slashes
danieljbruce e600bb3
revert buildQueryStringComponents changes
danieljbruce db33841
Add strictEncodeURIComponent wrapper method
danieljbruce 2336449
Add method comments
danieljbruce 38506c6
Add more notes about character preservation
danieljbruce d875be0
Add the pnpm lock file
danieljbruce 5c6ad6d
Revert "Add the pnpm lock file"
danieljbruce 23097b9
skip the tests because gax is not released yet.
danieljbruce a967001
fix: validate path parameters and prevent traversal/injection in REST…
danieljbruce fe620b0
Merge branch 'transcoding-path-traversal-fix' of https://github.com/g…
danieljbruce fe2c259
Replace with [...str]
danieljbruce be19de3
Add tests for the spread operator
danieljbruce 1746c32
remove unused import
danieljbruce 0f5cc2b
Rename two transcoding methods
danieljbruce 1ab70ea
Add comments to with and without slashes helpers
danieljbruce d1c34f2
Simplify encodeWithSlashes and encodeWithout
danieljbruce c9f85fd
collapse strictEncodeURIComponent method
danieljbruce 0768534
Use a more obscure request
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
Some comments aren't visible on the classic Files Changed page.
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
132 changes: 132 additions & 0 deletions
132
packages/google-cloud-dialogflow-cx/test/transcoding_validation.ts
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'; | ||
| import { v3 } from '../src'; | ||
|
|
||
| const sinon = require('sinon'); | ||
|
|
||
| describe('Dialogflow CX Fallback Transcoding and Path Traversal Prevention', () => { | ||
| let client: v3.SessionsClient; | ||
| 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?$httpMethod=DELETE#', | ||
| 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%24httpMethod%3DDELETE%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/..?$httpMethod=DELETE#', | ||
| 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%24httpMethod%3DDELETE%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/.?$httpMethod=DELETE#', | ||
| 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%24httpMethod%3DDELETE%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.
can you file an issue for re-enabling the tests?
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.
Yup. Already did here.