forked from googleapis/google-cloud-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(storage): Implement path containment to prevent traversal attacks #7
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
Draft
mahendra-google
wants to merge
12
commits into
main
Choose a base branch
from
prevent_traversal_attack
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.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6802fd
fix: implement method in storage clien t to prevent directry traversa…
mahendra-google 75e4a40
fix(storage): add test to prevent directory traversal attack
mahendra-google d007c1e
fix: modify test to prevent directory traversal attack
mahendra-google 232afb9
fix: modify method description
mahendra-google 8880723
fix: modify test
mahendra-google e1d1872
fix(storage): add check to path to prevent direcory traversal
mahendra-google 091e031
fix(storage): modify tests for directory traversal
mahendra-google 71964c7
fix(storage): add file deletion code in tests
mahendra-google c030f7e
fix(storage): add cross-platform extention method to get rid of polym…
mahendra-google a827fd6
fix(storage): remove blank line from storage client implementation
mahendra-google 7c1bed7
fix(storage): add comment in ValidateObjectDownloadPath method
mahendra-google a1f2d59
feat(Storage): Add hashuploadvalidator in upload object
mahendra-google 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
219 changes: 219 additions & 0 deletions
219
apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.Tests/HashValidatingUploaderTest.cs
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,219 @@ | ||
| // Copyright 2016 Google Inc. All Rights Reserved. | ||
| // | ||
| // 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 | ||
| // | ||
| // http://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. | ||
| using Google.Api; | ||
| using Google.Api.Gax; | ||
| using Google.Apis.Download; | ||
| using Google.Apis.Http; | ||
| using Google.Apis.Services; | ||
| using Google.Apis.Upload; | ||
| using Google.Cloud.ClientTesting; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using Xunit; | ||
|
|
||
| namespace Google.Cloud.Storage.V1.Tests | ||
| { | ||
| public class HashValidatingUploaderTest | ||
| { | ||
| private static readonly byte[] s_data = Enumerable.Range(0, 200000).Select(i => (byte) i).ToArray(); | ||
| private const string Md5Value = "md5=PL4+tNeevS58x7PmIPW6YA=="; | ||
| private const string Crc32Value = "crc32c=WfO2Ug=="; | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData(Md5Value)] | ||
| [InlineData(Md5Value + "," + Crc32Value)] | ||
| [InlineData(Crc32Value + "," + Md5Value)] | ||
| [InlineData(Crc32Value)] | ||
| public void Valid_ModeAlways(string headerValue) | ||
| { | ||
| HashValidatingUploader uploader = CreateUploader(request => CreateResponse(request, headerValue), UploadValidationMode.DeleteAndThrow); | ||
| AssertDownloadSucceeds(uploader); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("gzip", "gzip")] | ||
| [InlineData(null, "gzip")] | ||
| [InlineData("plain", null)] | ||
| [InlineData("plain", "gzip")] | ||
| [InlineData("gzip", "plain")] | ||
| [InlineData("gzip", null)] | ||
| [InlineData(null, null)] | ||
| [InlineData("plain", "plain")] | ||
| public void Valid_ModeAutomatic(string storedContentEncoding, string contentEncoding) | ||
| { | ||
| // This test doesn't try different hashes exhaustively; the way it treats the header | ||
| // is the same as with Always. | ||
| string headerValue = Md5Value + "," + Crc32Value; | ||
| HashValidatingUploader uploader = CreateUploader( | ||
| request => CreateResponse(request, headerValue, storedContentEncoding, contentEncoding), | ||
| UploadValidationMode.DeleteAndThrow); | ||
| AssertDownloadSucceeds(uploader); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Invalid_ModeAlways() | ||
| { | ||
| HashValidatingUploader uploader = CreateUploader(request => CreateResponse(request, "crc32c=Bogus1=="), UploadValidationMode.DeleteAndThrow); | ||
| AssertDownloadFails(uploader); | ||
| } | ||
|
|
||
| // - Mode is DownloadValidationMode.Automatic | ||
| // - Data is incorrect | ||
| // - And we're validating (so will throw) | ||
| [Theory] | ||
| [InlineData("gzip", "gzip")] | ||
| [InlineData(null, "gzip")] | ||
| [InlineData("plain", null)] | ||
| [InlineData("plain", "gzip")] | ||
| [InlineData(null, null)] | ||
| [InlineData("plain", "plain")] | ||
| public void InvalidData_ModeAutomatic_Validated(string storedContentEncoding, string contentEncoding) | ||
| { | ||
| HashValidatingUploader uploader = CreateUploader( | ||
| request => CreateResponse(request, "crc32c=Bogus1==", storedContentEncoding, contentEncoding), | ||
| UploadValidationMode.DeleteAndThrow); | ||
| AssertDownloadFails(uploader); | ||
| } | ||
|
|
||
| // - Mode is DownloadValidationMode.Automatic | ||
| // - Data is incorrect | ||
| // - We're not validating because of the stored content encoding / content encoding (so will "succeed") | ||
| [Theory] | ||
| [InlineData("gzip", "plain")] | ||
| [InlineData("gzip", null)] | ||
| public void InvalidData_ModeAutomatic_NotValidated(string storedContentEncoding, string contentEncoding) | ||
| { | ||
| HashValidatingUploader downloader = CreateUploader( | ||
| request => CreateResponse(request, "crc32c=Bogus1==", storedContentEncoding, contentEncoding), | ||
| UploadValidationMode.ThrowOnly); | ||
| AssertDownloadSucceeds(downloader); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Invalid_ModeNever() | ||
| { | ||
| HashValidatingUploader downloader = CreateUploader(request => CreateResponse(request, "crc32c=Bogus1=="), UploadValidationMode.DeleteAndThrow); | ||
| AssertDownloadSucceeds(downloader); | ||
| } | ||
|
|
||
| private static void AssertDownloadSucceeds(HashValidatingUploader uploader) | ||
| { | ||
| for (int chunks = 1; chunks < 5; chunks++) | ||
| { | ||
| // Make sure it definitely fits... | ||
| uploader.ChunkSize = 1 * 1024 * 1024; | ||
| var stream = new MemoryStream(); | ||
| var status = uploader.Upload(); | ||
| if (status.Exception != null) | ||
| { | ||
| throw status.Exception; | ||
| } | ||
| Assert.Equal(UploadStatus.Completed, status.Status); | ||
| Assert.Equal(s_data, stream.ToArray()); | ||
| } | ||
| } | ||
|
|
||
| private static void AssertDownloadFails(HashValidatingUploader uploader) | ||
| { | ||
| for (int chunks = 1; chunks < 5; chunks++) | ||
| { | ||
| // Make sure it definitely fits... | ||
| uploader.ChunkSize = (s_data.Length + 100) / chunks; | ||
| var stream = new MemoryStream(); | ||
| var status = uploader.Upload(); | ||
| Assert.Equal(UploadStatus.Failed, status.Status); | ||
| Assert.Contains("Incorrect hash", status.Exception.Message); | ||
| } | ||
| } | ||
|
|
||
| private static HashValidatingUploader CreateUploader(Func<HttpRequestMessage, HttpResponseMessage> handler, UploadValidationMode mode) | ||
| { | ||
| var service = new MockableService(handler); | ||
| var metadata = new Apis.Storage.v1.Data.Object(); | ||
| var bucket = new Apis.Storage.v1.Data.Bucket(); | ||
| string contentType = "application/json"; | ||
| var stream = new MemoryStream(); | ||
| return new HashValidatingUploader(service, metadata, bucket.Name, stream, contentType, mode); | ||
| } | ||
|
|
||
| internal class MockableService : BaseClientService | ||
| { | ||
| internal MockableService(Func<HttpRequestMessage, HttpResponseMessage> handler) | ||
| : base(GetInitializer(handler)) | ||
| { | ||
| } | ||
|
|
||
| public override string BasePath => ""; | ||
| public override string BaseUri => ""; | ||
| public override IList<string> Features => new List<string>(); | ||
| public override string Name => "Mockable"; | ||
|
|
||
| private static Initializer GetInitializer(Func<HttpRequestMessage, HttpResponseMessage> handler) | ||
| { | ||
| var httpMessageHandler = new MockableMessageHandler(handler); | ||
| var configurableHandler = new ConfigurableMessageHandler(httpMessageHandler); | ||
| var clientFactory = new FakeHttpClientFactory(configurableHandler); | ||
| return new Initializer { HttpClientFactory = clientFactory }; | ||
| } | ||
| } | ||
|
|
||
| private static HttpResponseMessage CreateResponse( | ||
| HttpRequestMessage request, string hashHeaderValue, | ||
| string storedContentEncoding = null, string contentEncoding = null) | ||
| { | ||
| HttpResponseMessage response = new HttpResponseMessage { Content = new ByteArrayContent(s_data) }; | ||
| if (hashHeaderValue != null) | ||
| { | ||
| response.Headers.Add(Crc32c.HashHeaderName, hashHeaderValue); | ||
| } | ||
| if (storedContentEncoding != null) | ||
| { | ||
| response.Headers.Add(HashValidatingUploader.StoredContentEncodingHeaderName, storedContentEncoding); | ||
| } | ||
| if (contentEncoding != null) | ||
| { | ||
| response.Content.Headers.ContentEncoding.Add(contentEncoding); | ||
| } | ||
| MaybeIntercept(request, response); | ||
| return response; | ||
| } | ||
|
|
||
| // This is ugly - it's effectively mimicing the code in the REST support library for response stream | ||
| // interception. We basically want to use Google.Apis.Http.StreamInterceptionHandler, but that's (understandably) | ||
| // internal. Fake it for now. An alternative might be to use reflection, but that's brittle too. | ||
| private static void MaybeIntercept(HttpRequestMessage request, HttpResponseMessage response) | ||
| { | ||
| // HttpRequestMessage.Properties is obsolete, but that's what the support library uses, and | ||
| // HttpRequestMessage.Options isn't even available in .NET Framework. | ||
| #pragma warning disable CS0618 | ||
| var provider = request.Properties[ConfigurableMessageHandler.ResponseStreamInterceptorProviderKey] as Func<HttpResponseMessage, StreamInterceptor>; | ||
| #pragma warning restore CS0618 | ||
| if (provider == null) | ||
| { | ||
| return; | ||
| } | ||
| var interceptor = provider(response); | ||
| if (interceptor == null) | ||
| { | ||
| return; | ||
| } | ||
| interceptor(s_data, 0, s_data.Length); | ||
| return; | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.