From b2944d93a6d95810f0aef09e9ac44b95e2712554 Mon Sep 17 00:00:00 2001 From: Delan Azabani Date: Sat, 22 Jun 2024 23:00:59 +0800 Subject: Reject fully-qualified URLs in 'from' argument of `nix registry add` We previously allowed you to map any flake URL to any other flake URL, including shorthand flakerefs, indirect flake URLs like `flake:nixpkgs`, direct flake URLs like `github:NixOS/nixpkgs`, or local paths. But flake registry entries mapping from direct flake URLs often come from swapping the 'from' and 'to' arguments by accident, and even when created intentionally, they may not actually work correctly. This patch rejects those URLs (and fully-qualified flake: URLs), making it harder to swap the arguments by accident. Fixes #181. Change-Id: I24713643a534166c052719b8770a4edfcfdb8cf3 --- src/nix/registry.cc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/nix/registry.cc') diff --git a/src/nix/registry.cc b/src/nix/registry.cc index f509ccae8..9619b85d4 100644 --- a/src/nix/registry.cc +++ b/src/nix/registry.cc @@ -5,6 +5,7 @@ #include "flake/flake.hh" #include "store-api.hh" #include "fetchers.hh" +#include "url-parts.hh" #include "registry.hh" using namespace nix; @@ -109,7 +110,14 @@ struct CmdRegistryAdd : MixEvalArgs, Command, RegistryCommand void run() override { + std::smatch match; + if (!std::regex_match(fromUrl, match, flakeShorthandRegex)) { + throw UsageError("'from-url' argument must be a shorthand like 'nixpkgs' or 'nixpkgs/nixos-20.03'"); + } auto fromRef = parseFlakeRef(fromUrl); + if (fromRef.input.direct) { + throw UsageError("'from-url' argument must be an indirect flakeref like 'nixpkgs' or 'flake:nixpkgs'"); + } auto toRef = parseFlakeRef(toUrl); auto registry = getRegistry(); fetchers::Attrs extraAttrs; -- cgit v1.2.3