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

lib.generators.toPlist: escape XML syntax in strings & keys #356502

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

linnnus
Copy link
Contributor

@linnnus linnnus commented Nov 16, 2024

Before this patch, code like this would break generate invalid XML.

lib.generators.toPlist {} "ab<cd"

That's obviously bad, since the call to toPlist often happens through indirection, as is the case in e.g. the nix-darwin project.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: lib The Nixpkgs function library label Nov 16, 2024
@nix-owners nix-owners bot requested review from infinisil and Profpatsch November 16, 2024 15:51
@linnnus
Copy link
Contributor Author

linnnus commented Nov 16, 2024

Conceivably, someone could be relying on the current behavior to do some truly unholy stuff, so I guess this is technically a breaking change. In that case, what changelog should I update?

@emilazy
Copy link
Member

emilazy commented Nov 16, 2024

This is clearly an improvement, and would be nice for nix-darwin. However I’m a bit worried about the Hyrum’s law implications here, since it is likely that people are already doing manual escaping to work around this in the wild. It may have to be an option, or else perhaps renaming the function and gradually deprecating the old one.

@NixOSInfra NixOSInfra added the 12. first-time contribution This PR is the author's first one; please be gentle! label Nov 16, 2024
@linnnus
Copy link
Contributor Author

linnnus commented Nov 16, 2024

Nice catch! I hadn't thought about that. An initial glance at public uses of generators.toPlist does show at least one repo does manual escaping, though most don't.

If it were up to me it would just be a breaking change in the next version, but to be honest I'm not used to working on projects this large, so I guess that's up to you!

@emilazy
Copy link
Member

emilazy commented Nov 16, 2024

In an ideal world we’d do that, but lib specifically is rather conservative :) My suggestion is to change the first argument to { escape ? false }, and then we can use that in nix-darwin and Home Manager, and we can add a warning when !escape saying that it will be removed. Then after a release cycle we could toggle it on by default, and remove the flag entirely after branch‐off, say. But that’s a long period of work so I can understand not wanting to take it up :( Still, adding the option with a TODO at least means that someone else can complete the transition if they find it.

@linnnus
Copy link
Contributor Author

linnnus commented Nov 16, 2024

I have made the changes you suggested. Escaping is now disabled by default :^)

lib/generators.nix Outdated Show resolved Hide resolved
@linnnus linnnus requested a review from ambroisie November 21, 2024 08:35
@ambroisie
Copy link
Contributor

Could you squash your PR please?

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one reputable person label Nov 22, 2024
@linnnus
Copy link
Contributor Author

linnnus commented Nov 22, 2024

Should be done know?

@ambroisie
Copy link
Contributor

Thanks.

Will let our library owners chime in now.

@wegank wegank removed the 12.approvals: 1 This PR was reviewed and approved by one reputable person label Nov 23, 2024
@linnnus
Copy link
Contributor Author

linnnus commented Dec 22, 2024

Sooo... can we merge this now, or which "library owners" should I reach out to? :^)

@ambroisie
Copy link
Contributor

@infinisil @Profpatsch friendly ping.

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

This is looking almost perfect, just a minor improvement to automate the deprecation a bit more :)

lib/generators.nix Outdated Show resolved Hide resolved
Before this patch, code like this would break generate invalid XML:

    lib.generators.toPlist {} "ab<cd"

That's obviously bad, since the call to toPlist often happens through
indirection, as is the case in e.g. the nix-darwin project. A user might
not realize that they have to escape the strings.

This patch adds the argument 'escape' to lib.generators.plist and emits
a warning if it is not set to true. In a future release, this behavior
should become the default.

I have also added a note for future maintainers, in case I forget to
actually remove the deprecated functionality in a future release.
Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

Thanks, looking good!

@infinisil infinisil merged commit d510f60 into NixOS:master Dec 23, 2024
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 1-10 10.rebuild-linux: 1-10 12. first-time contribution This PR is the author's first one; please be gentle!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants