Skip to content
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

Using MemoryStream on JsonConfigurationExtensions.AddJsonStream throws StreamNotReadable due to casting #1979

Closed
sport-monkey opened this issue Jul 8, 2019 · 2 comments
Assignees

Comments

@sport-monkey
Copy link

If you believe you have an issue that affects the security of the platform please do NOT create an issue and instead email your issue details to secure@microsoft.com. Your report may be eligible for our bug bounty but ONLY if it is reported through email.

Describe the bug

When attempting to use a MemoryStream of a JSON file for config a System.ArgumentException: Stream was not readable is received. This is because the AddJsonStream extension https://github.com/aspnet/Extensions/blob/release/3.0-preview7/src/Configuration/Config.Json/src/JsonConfigurationExtensions.cs.AddJsonStream uses the following https://github.com/aspnet/Extensions/blob/release/3.0-preview7/src/Configuration/Config.Json/src/JsonConfigurationSource.cs that implements FileConfigurationProvider.

To Reproduce

Steps to reproduce the behavior:

  1. Using version 'dotnet/packs/Microsoft.AspNetCore.App.Ref/3.0.0-preview6.19307.2/ref/netcoreapp3.0/Microsoft.Extensions.Configuration.Json.dll'
  2. Run this code
            var builder = new WebHostBuilder()
                .UseContentRoot("../../../../WorkingCode")
                .UseEnvironment("Development")
                .UseUrls("http://localhost:0")
                .ConfigureAppConfiguration((_, config) =>
                { config.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes("{ \"SomeKey\": \"SomeValue\" }"))); Configuration = config.Build(); }).UseStartup<Startup>();
  1. With these arguments '....'
  2. See error "System.ArgumentException: Stream was not readable"

Expected behavior

Either I would expect:
i. the configuration to be read for
or
ii. AddJsonStream to only take a FileStream

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.
Include the output of dotnet --info

@HaoK HaoK self-assigned this Jul 9, 2019
@HaoK
Copy link
Member

HaoK commented Jul 9, 2019

I'm unable to reproduce this using a clean app, created a new dotnet core web app and added the same code as above, and it was able to run fine and I was able to interact with the Config and lookup the SomeValue

            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                    .ConfigureAppConfiguration((_, config) =>
                    {
                        config.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes("{ \"SomeKey\": \"SomeValue\" }")));
                    })
                    .UseStartup<Startup>();
                });

@HaoK HaoK closed this as completed Jul 10, 2019
@sport-monkey
Copy link
Author

sport-monkey commented Jul 24, 2019

Here's a full example, it's caused by building the configuration for reuse inside tests and is a facet of using a StreamReader that forcibly disposes your Stream.

namespace JsonConfigurationFailure
{
    public class Examples
    {
        [Fact]
        public void Failure()
        {
            const string configString = "{\"PointlessValue\": \"hello\"}";

            IConfigurationRoot configuration = null;
            var webFolder = Path.GetFullPath("../../../../JsonConfigurationExtensionsTest");
            
            var builder = new WebHostBuilder()
                .UseContentRoot(webFolder)
                .UseEnvironment("Development")
                .UseUrls("http://localhost:0")
                .ConfigureAppConfiguration((_, config) =>
                {
                    config.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(configString)));
                    configuration = config.Build();
                }).UseStartup<Startup>();

            Assert.Throws<ArgumentException>(() => new TestServer(builder));
        }
        
        [Fact]
        public async Task Success()
        {
            const string configString = "{\"PointlessValue\": \"hello\"}";

            var webFolder = Path.GetFullPath("../../../../JsonConfigurationExtensionsTest");
            
            var builder = new WebHostBuilder()
                .UseContentRoot(webFolder)
                .UseEnvironment("Development")
                .UseUrls("http://localhost:0")
                .ConfigureAppConfiguration((_, config) =>
                {
                    config.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(configString)));
                }).UseStartup<Startup>();

            var testServer = new TestServer(builder);
            
            var client = testServer.CreateClient();
            var response = await client.GetAsync("/api/values/1");
            var content = await response.Content.ReadAsStringAsync();
            
            Assert.Equal("hello", content);
        }
    }
}

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants