Skip to content

Redact DoNotLog annotated values in toString and introduce dangerousToString#2917

Open
mpritham wants to merge 1 commit into
developfrom
pm/toString-doNotLog
Open

Redact DoNotLog annotated values in toString and introduce dangerousToString#2917
mpritham wants to merge 1 commit into
developfrom
pm/toString-doNotLog

Conversation

@mpritham
Copy link
Copy Markdown
Contributor

@mpritham mpritham commented Jun 1, 2026

Before this PR

Third party libraries can call toString on objects when constructing exceptions, leading to DoNotLog annotated fields appearing in logs. Having toString not contain any DoNotLog data will prevent this class of error.

Conjure generates toString methods on objects containing DoNotLog field values.

This PR adds the redactToStringDoNotLog flag (default off), that when enabled:

  • Objects render DoNotLog fields as <name>: REDACTED in toString and contain a new method dangerousToString (annotated DoNotLog) that returns the full representation for the rare cases that need it.
  • DoNotLog aliases return the bare literal "REDACTED" from toString.
    • Aliases do not need the dangerousToString method because they expose an equivalent method get
  • Union variants whose member is DoNotLog redact the value in the wrapper's toString (both sealed and non-sealed paths)
    • Unions do not need the dangerousToString method because they expose an equivalent method getValue

There are certain cases where developers are using the toString value of Conjure objects with DoNotLog fields, so the flag is disabled by default, and will be rolled out repo by repo.

After this PR

==COMMIT_MSG==
Redact DoNotLog annotated values in toString and introduce dangerousToString
==COMMIT_MSG==

Possible downsides?

This introduces a new method for every generated object. We can't naively generate the dangerousToString only for objects with DoNotLog fields: a DoNotLog field could be removed from an object definition while a client calls the dangerousToString method which would lead to an ABI break. Solutions that try to take the previous state of a Conjure definition when generated code seem complicated and would be a divergence from Conjure's simple IR based code gen.

@changelog-app
Copy link
Copy Markdown

changelog-app Bot commented Jun 1, 2026

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Redact DoNotLog annotated values in toString and introduce dangerousToString

Check the box to generate changelog(s)

  • Generate changelog entry

@mpritham mpritham force-pushed the pm/toString-doNotLog branch from f448c7c to efa625a Compare June 1, 2026 15:16
@mpritham mpritham force-pushed the pm/toString-doNotLog branch from efa625a to d461018 Compare June 1, 2026 15:36
Comment on lines +73 to +77
@Override
@DoNotLog
public String toString() {
return "ObjectAllDoNotLog{secret: REDACTED" + ", token: REDACTED" + '}';
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not in fact DoNotLog anymore

default boolean redactToStringDoNotLog() {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are certain cases where developers are using the toString value of Conjure objects with DoNotLog fields, so the flag is disabled by default, and will be rolled out repo by repo.

Since this will be generated on the producer, but may cause issues on the consumers, I don't think it's safe to simply turn on the flag? We may instead need to start always introducing the dangerousToString methods, so consumers can migrate across the board (and we can then write automation to migrate everything), then get the flag turned on to also redact the main toString?

Comment on lines +161 to +164
@Override
public String toString() {
return "UnionWithDoNotLogVariant{value: SecretValueWrapper{value: REDACTED" + "}}";
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should still provide a dangerousToString method for unions, because value() is no quite the same here, and I wonder if this is going to make migration more painful. We can also just not do anything for now and wait until we see migration issues, if any

Comment on lines +234 to +237
@Override
public String toString() {
return "SafeValueWrapper{value: " + value + '}';
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the replacement for this/the union's toString method? getValue() is private access, so we can't use it, and the visitor seems overkill 🤔

Comment on lines +235 to +238
CodeBlock.Builder body = CodeBlock.builder().add("return $S\n", prefix + (redactValue ? "REDACTED" : ""));
if (!redactValue) {
body.add(" + $N", VALUE_FIELD_NAME);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this redact the entire value if it's do-not-log, when we now actually have safe toString for conjure objects, which we could leverage? Probably good enough for a start, but seems like we may want to revisit that approach?

This actually makes me wonder whether the overall object itself should even be marked DoNotLog anymore (iirc, this mostly indicates the stringified log safety?)

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

Successfully merging this pull request may close these issues.

2 participants