Skip to content

Support creating "secret" variables that hide their values in data and log files #4537

Description

@pekkaklarck

Often in automation we need to use passwords or other values that should not be logged. To help with that, various libraries have keywords that avoid logging given values even though their keywords normally do that. For example, SeleniumLibrary has Input Password that doesn't log the password and also temporarily disables Robot's logging to avoid the password being logged by the underlying Selenium on DEBUG level. This works fine otherwise, but if tests are run with --loglevel TRACE, Robot anyway logs all argument values and the password will be visible. Browser's Fill Secret tries to avoid that problem by requiring the value to be passed using a special $var syntax so that Robot won't log the actual variable value. Unfortunately that doesn't work if the value is passed to Fill Secret via a user keyword and only that keyword uses the $var syntax like in the following example:

*** Variables ***
${PASSWORD}        secret

*** Test Cases ***
Example
    Keyword    ${PASSWORD}    # This value is logged on TRACE level

*** Keywords ***
Keyword
    [Arguments]    ${pwd}
    Fill Secret    selector    $pwd

To make it easier to use values that should not be logged, I propose we add new variable syntax *{var} to create "secret variables". The syntax could be used when creating variables in the Variables section, when creating local variables based on keyword return values, and with Set Global/Suite/Test/Local Variable keywords. The syntax would be only used for creating variables, they would be used normally like ${var}:

*** Variables ***
*{PASSWORD}        secret

*** Test Cases ***
Example
    Keyword    ${PASSWORD}
    *{local} =    Another keyword
    Keyword    ${local}

*** Keywords ***
Keyword
    [Arguments]    ${pwd}
    Fill Secret    selector    ${pwd}

Technically the *{var} assignment would create a custom object with this kind of implementation:

class Secret:
    def __init__(self, value, name=None):
        self.value = value
        self.name = name
    def __str__(self):
        return f'<Secret {self.name}>' if self.name else '<Secret>'

These objects would be used when the variable is passed from tests to user keywords and from user keywords to other user keywords. The real value would, however, be passed to library keywords so they would get values normally and wouldn't need to be changed to benefit from this new syntax.

Although this new syntax would work with existing keywords, we should make it possible for library keywords to require arguments to be "secret". That's basically what Fill Secret does but this new syntax would avoid the problem that the value can be logged on higher level. A convenient way to support this would be type hints:

from robot.api.secrets import Secret

def example(secret: Secret):
    ...

This would work so that Robot would validate that the used value is a Secret and fail if it's not. I believe we should pass the real value, not the actual Secret object, to the underlying keyword also in this case. That can confuse type checkers but keywords can use typing.cast if that's a problem. To support normal type conversion, we should also allow parameterizing the type hint like Secret[int].

Keywords could also return secret values simply by returning Secret instances:

from robot.api.secrets import Secret

def example():
    return Secret('value')

UPDATE: The syntax to create secrets turned out to be quite a bit different to what was initially proposed, but the concept is still the same.

Activity

  1. added this to the v6.1 milestone on Nov 16, 2022
  2. pekkaklarck commented on Nov 16, 2022

    @pekkaklarck
    MemberAuthor

    One problem with the proposed implementation is that although Robot itself wouldn't log secret variable values, keywords could accidentally log them. We could easily special case keywords like Log, but because other keywords could leak the value that alone probably doesn't make sense. One possibility would be making it an error to pass a Secret to a library keyword that doesn't explicitly require Secret. An obvious problem of this approach is that secret variables wouldn't work with existing keywords. I got a feeling that it is a too big problem and we shouldn't to this. Possibly this behavior could be configurable, though, but in that case I'd add configuration only later.

    If we'd made using secrets more strict like this, I believe we should anyway allow users to use ${secret.value} syntax to access the actual value. This would make it possible to log the value for debug purposes. We should also allow keywords to accept also other values than secrets like arg: Secret | str.

    Another possible approach would be always just passing Secret objects to library keywords instead of upwrapping them and passing actual values. This wouldn't work with existing keywords any better than requiring keywords to declare they accept Secret objects, though.

  3. pekkaklarck commented on Nov 17, 2022

    @pekkaklarck
    MemberAuthor

    One open question is what to do if a secret variable is added into a @{list} or &{dict} in the Variables section:

    *** Variables ***
    *{SECRET}    value
    @{LIST}      ${SECRET}
    &{DICT}      key=${SECRET}

    I believe we should add the Secret object, not the value, to containers in cases like this. Otherwise if these variables are logged on TRACE, the secret value is logged as part of them. If the variables are passed to keywords like Keyword ${LIST}, I don't think it makes sense for us to go through their contents and convert Secret objects to values like the plan is to do when only a secret value is used. These values can be deeply nested and also recursive making going through them hard. Not unwrapping Secret objects means keywords can encounter these objects, but I expect that to be rare and accessing the actual value is anyway easy.

    If collections are created dynamically in tests or keywords like

    ${list} =    Create List    ${SECRET}
    

    they end up getting the real value, not Secret object, because that's what is planned to be passed to Create List. The comment above discusses other alternatives to these plans, though.

  4. pekkaklarck commented on Nov 17, 2022

    @pekkaklarck
    MemberAuthor

    Another questions is should we allow using secrets as parts of strings like Hello, ${SECRET}!. I believe this should be prohibited because the whole Hello, ${SECRET}! argument may be logged using TRACE after the variable is resolved. Using Hello, ${SECRET.value}! would work, though, unless we hide the actual value somewhere (which is rather complicated with Python).

  5. pekkaklarck commented on Nov 17, 2022

    @pekkaklarck
    MemberAuthor

    Yet another question is deciding what to do if variables are used like *{var}:

    Keyword    *{SECRET}
    

    Options include:

    • Allow using exactly like ${SECRET}. I don't like this too much. Having two alternative ways to do same thing is seldom a good idea.
    • Come up some use case for. It could, for example, mean unwrapping Secret and normal ${SECRET} usage would pass the Secret object, not the value. Or possibly *{SECRET} could mean passing Secret while ${SECRET} would unwrap.
    • Make it an error and possibly come up with some usage for the syntax later.

    If *{SECRET} and ${SECRET} would work differently, one referring to the Secret object and other to the value, then we could allow using them both when creating @{list} and &{dict} in the Variables section. Possibly one of them would also work as part of a string.

  6. modified the milestones: v6.1, on Jan 3, 2023
  7. pekkaklarck commented on Mar 7, 2023

    @pekkaklarck
    MemberAuthor

    Issue #4674 proposes supporting $var syntax as an alternative to ${var}. If we do that, passing secrets to the Browser library by using the $var syntax won't work anymore. That would increase the priority of this issue.

  8. Snooz82 commented on Mar 7, 2023

    @Snooz82
    Member

    I would propose to NOT implement *{secret} because it is pretty implicit and only some super nerds will know that feature.

    i would propose to implement type converters for variables and user keyword arguments first! Then Secret creation is for free.

    *** Variables ***
    ${secret}:Secret    123456
    

    #3278

    discussion about that:
    https://robotframework.slack.com/archives/C04FPJ3R4PN/p1678217116430379?thread_ts=1678198681.294349&channel=C04FPJ3R4PN&message_ts=1678217116.430379

  9. modified the milestones: , v7.0 on Mar 7, 2023
  10. pekkaklarck commented on Mar 7, 2023

    @pekkaklarck
    MemberAuthor

    Using ${pwd}:Secret (or ${pwd:Secret}, depending on how #3278 is implemented) would be a good alternative to *{pwd}. One downside is that variable type conversion is harder to implement than adding *{pwd} syntax, but that feature also has lot of other usages so implementing it makes sense anyway. If we go that route, then this issue obviously is dependent on #3278.

    If we decide to rely on variable conversion instead of adding *{var} syntax, we need to add a custom variable converter for Secret that converts the value to a Secret object as discussed above. Then we need another argument converter for libraries that only validates that the given argument is a Secret.

  11. pekkaklarck commented on Mar 25, 2023

    @pekkaklarck
    MemberAuthor

    Here's some proto code how Secret could possibly look like. It supports also Secret[int] and converts value accordingly. If this is added to Robot, values naturally need to be converted using Robot's converters.

    class Secret:
        type = None
    
        def __init__(self, value, name=None):
            self.__value = value
            self.name = name
    
        @property
        def value(self):
            return self.type(self.__value) if self.type else self.__value
    
        @classmethod
        def __class_getitem__(cls, item):
            return type(f'{cls.__name__}[{item.__name__}]', (cls,), {'type': item})
    
        def __str__(self):
            name = type(self).__name__
            args = 'value=<secret>'
            if self.name:
                args += f', name={self.name!r}'
            return f'{name}({args})'
  12. 45 remaining items

  13. pekkaklarck commented on Sep 12, 2025

    @pekkaklarck
    MemberAuthor

    I tried moving the Secret implementation under robot.api.types, but the cyclic import issues turned out to be too hard to fix. We've many times earlier exposed classes, functions, etc. implemented elsewhere via robot.api, so I guess that's not a major issue in this case either. The implementation is currently in robot.utils._secret module, but I don't think trying to hide it into a private module has benefits, especially because it is anyway public exposed via robot.utils. I'll thus rename the module to robot.utils.secret. The class also needs a note that it should be imported via robot.api.types and that module itself needs docs too.

  14. pekkaklarck commented on Sep 15, 2025

    @pekkaklarck
    MemberAuthor

    The new robot.api.types module now has API docs and those docs link to the Secret class. There were some technical challenges with that due to #5498.

  15. added a commit that references this issue on Sep 16, 2025
  16. pekkaklarck commented on Sep 16, 2025

    @pekkaklarck
    MemberAuthor

    Above commits finalized documentation so this issues is now done! 🎉

  17. added a commit that references this issue on Sep 16, 2025
    2f42ee8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

acknowledgebeta 1effort: largepriority: critical

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions