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

FileSessionHandler exception: BinaryFormatter serialization and deserialization are disabled #544

Open
8 of 24 tasks
gianlucastella1 opened this issue Jul 7, 2022 · 1 comment

Comments

@gianlucastella1
Copy link

I've:

Issue category

  • Bug
  • Feature Request
  • Missing Feature
  • Question
  • Not sure

Language

  • C#
  • VB.NET

Usage

  • Windows Form/Console app
  • WPF app
  • Asp .NET MVC
  • Asp .NET Core
  • Universal Windows Platform [UWP]
  • Xamarin or Xamarin Forms
  • Mono
  • Other

Operating System

  • Windows
  • Windows Server
  • Linux
  • Mac OS
  • Other

Debug logs

            _api = InstaApiBuilder.CreateBuilder()
                .UseLogger(new DebugLogger(LogLevel.All))
                .SetSessionHandler(new FileSessionHandler() { FilePath = "./ig_session" })
                .Build();

            _api.SessionHandler.Load()

throws following exception

BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) 
at InstagramApiSharp.Helpers.SerializationHelper.SerializeToStream(Object o) 
at InstagramApiSharp.API.InstaApi.GetStateDataAsStream() 
at InstagramApiSharp.Classes.SessionHandlers.FileSessionHandler.Save() 
at [CUT]

Describe your issue

FileSessionHandler uses BinaryFormatter to laod and save session. As stated in https://aka.ms/binaryformatter

The BinaryFormatter type is dangerous and is not recommended for data processing. Applications should stop using BinaryFormatter as soon as possible, even if they believe the data they're processing to be trustworthy. BinaryFormatter is insecure and can't be made secure.
This affects:

  • .NET Framework all versions
  • .NET Core 2.1 - 3.1
  • .NET 5 and later
@gianlucastella1
Copy link
Author

As a workaround, a custom ISessionHandler can be implemented using BinaryReader/BinaryWriter:

    public class CustomFileSessionHandler : ISessionHandler
    {
        public IInstaApi InstaApi { get; set; }
        public string FilePath { get; set; }

        private Encoding _encoding = Encoding.UTF8;

        public void Load()
        {
            if (System.IO.File.Exists(FilePath))
            {
                using (var stream = File.Open(FilePath, FileMode.Open))
                {
                    using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                    {
                        var stateAsBase64String = reader.ReadString();
                        var stateAsString = _encoding.GetString(Convert.FromBase64String(stateAsBase64String));

                        InstaApi.LoadStateDataFromString(stateAsString);
                    }
                }
            }
        }

        public void Save()
        {
            using (var stream = File.Open(FilePath, FileMode.Create))
            {
                var stateAsString = InstaApi.GetStateDataAsString();
                var stateAsBase64String = Convert.ToBase64String(_encoding.GetBytes(stateAsString));

                using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
                {
                    writer.Write(stateAsBase64String);
                }
            }
        }
    }

ramtinak added a commit that referenced this issue Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant