Coder Social home page Coder Social logo

Comments (8)

lrhn avatar lrhn commented on June 24, 2024 3

Not sure this is something I'd want to solve at the language level.

You have something which is not just a string, it's a password.
As @jakemac53 suggests, wrapping it in a class would avoid accidentally treating it as just any other string.

The problem here is that you both want to convert it to JSON, but also don't want to convert it to a string, but JSON is a text format.
Maybe you can choose to have the toJson function return something still containing to Password object, have Password.toString() return "<Redacted>" and pass a toEncodable function to jsonEncode that converts Password to Password.text (the real text). Then anyone accidentally printing the password, or the JSON-like map structure, will not see the password, but actually converting using jsonEncode (perhaps even directly to UTF-8) will contain the password.

from language.

jakemac53 avatar jakemac53 commented on June 24, 2024 2

I would suggest just using a wrapper class here:

class Private<T> {
  final T privateValueWhichShouldNotEscape;
  
  Private(this.privateValueWhichShouldNotEscape);
}

That should send the right signal to anybody using the value, that it shouldn't be printed, assigned to other variables, etc.

from language.

jakemac53 avatar jakemac53 commented on June 24, 2024 2

You could possibly even do this as an extension type to make it zero cost.

from language.

jakemac53 avatar jakemac53 commented on June 24, 2024 1

The default behavior for classes would print something like Instance of Credentials. Anything else is coming from some code generator or other similar feature, which is giving you a toString based on the fields.

This is probably an issue for whatever package it is that is giving you that customized toString method, and could be solved however that package accepts configuration (probably an annotation).

from language.

hanskokx avatar hanskokx commented on June 24, 2024

My apologies, I erroneously omitted the .toJson and .toString methods that I had included from my previous issue. I have updated the example code to include those.

from language.

hanskokx avatar hanskokx commented on June 24, 2024

I wouldn't get too hung up on the fact that my example is a password or uses .toJson - these are just examples. It could very well be something like a social security number, or some other private item. The idea here is that it would function as normal, but using any print, debugPrint, or log statement would print out some sort of "redacted" message. Notably, this would not prevent you from, say, splitting the value into a list of glyphs and printing those out. My idea is that it's a simple safety check to prevent ham-fisted and careless spilling of secrets by using debug logging.

from language.

jakemac53 avatar jakemac53 commented on June 24, 2024

I wouldn't get too hung up on the fact that my example is a password or uses .toJson

The advice above isn't specific to passwords, which is why I suggested a generic class.

The methods you describe all just call toString() on anything they are given, so a simple wrapper type which has a custom toString() which prints nothing is a very simple and reasonable way to prevent this information from being accidentally surfaced by things.

By naming the actual member to access the real value something scary, it should make it easy in code reviews to know when the real, unredacted version is being accessed so that such access can be carefully assessed.

from language.

hanskokx avatar hanskokx commented on June 24, 2024

I wouldn't get too hung up on the fact that my example is a password or uses .toJson

The advice above isn't specific to passwords, which is why I suggested a generic class.

The methods you describe all just call toString() on anything they are given, so a simple wrapper type which has a custom toString() which prints nothing is a very simple and reasonable way to prevent this information from being accidentally surfaced by things.

By naming the actual member to access the real value something scary, it should make it easy in code reviews to know when the real, unredacted version is being accessed so that such access can be carefully assessed.

That's a valid and fair point.

from language.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.