cedar-policy / cedar

Implementation of the Cedar Policy Language

Home Page:https://www.cedarpolicy.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Policy formatter drops empty lines in multi-line strings

john-h-kastner-aws opened this issue · comments

Bug Category

Other

Describe the bug

Given a policy

permit (principal, action, resource)
when { "

a
" };

The formatter drops the new-line character and rewrites the policy to

permit (principal, action, resource)
when { "
a
" };

This is probably because the RcDoc::as_string function cannot be used with strings containing line breaks. Possibly related to #265.

Reproduction steps

[jkastner@dev-dsk-jkastner-1a-3309db3b cedar]$ cargo run format <<<'
permit (principal, action, resource)
when { "

a"};'

permit (principal, action, resource)
when { "
a" };

This bug should be captured with the fix in #865.

To fix the formatted policy, I don't see a way to keep the literal newlines while using the current pretty formatter. We instead need to replace them with escapes. This should be as simple as printing the debug_escape() of the string because we use Rust's escaping rules. Although, this may influence other escapes. For example, I think a string containing a tab literals would have it replaced with \t which is not horrible, but ideally it shouldn't do that.