Skip to content

Commit ac21910

Browse files
Tomsonscopybara-github
authored andcommitted
fix(bzlmod): throw on json parse exception
[Fixes](bazelbuild#14437) Get rid of new lines in `bazel_registry.json` and return an `Optional.empty()` aka registry descriptor without mirrors and throw an IOException for malformed JSON to stick to official json.org specs Closes bazelbuild#15109. PiperOrigin-RevId: 436992309
1 parent 45b225d commit ac21910

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/IndexRegistry.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.gson.FieldNamingPolicy;
2626
import com.google.gson.Gson;
2727
import com.google.gson.GsonBuilder;
28+
import com.google.gson.JsonParseException;
2829
import java.io.FileNotFoundException;
2930
import java.io.IOException;
3031
import java.net.MalformedURLException;
@@ -115,7 +116,14 @@ private <T> Optional<T> grabJson(String url, Class<T> klass, ExtendedEventHandle
115116
return Optional.empty();
116117
}
117118
String jsonString = new String(bytes.get(), UTF_8);
118-
return Optional.of(gson.fromJson(jsonString, klass));
119+
if (jsonString.strip().isEmpty()) {
120+
return Optional.empty();
121+
}
122+
try {
123+
return Optional.of(gson.fromJson(jsonString, klass));
124+
} catch (JsonParseException e) {
125+
throw new IOException(String.format("Unable to parse json at url %s", url), e);
126+
}
119127
}
120128

121129
@Override

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/IndexRegistryTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import static com.google.common.truth.Truth8.assertThat;
2020
import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey;
2121
import static java.nio.charset.StandardCharsets.UTF_8;
22+
import static org.junit.Assert.assertThrows;
2223

2324
import com.google.common.base.Suppliers;
2425
import com.google.common.collect.ImmutableList;
2526
import com.google.common.collect.ImmutableMap;
2627
import com.google.devtools.build.lib.bazel.repository.downloader.HttpDownloader;
2728
import com.google.devtools.build.lib.testutil.FoundationTestCase;
2829
import java.io.File;
30+
import java.io.IOException;
2931
import java.io.Writer;
3032
import java.nio.file.Files;
3133
import org.junit.Before;
@@ -140,4 +142,54 @@ public void testGetRepoSpec() throws Exception {
140142
.setRemotePatchStrip(3)
141143
.build());
142144
}
145+
146+
@Test
147+
public void testGetRepoInvalidRegistryJsonSpec() throws Exception {
148+
server.serve("/bazel_registry.json", "", "", "", "");
149+
server.start();
150+
server.serve(
151+
"/modules/foo/1.0/source.json",
152+
"{",
153+
" \"url\": \"http://mysite.com/thing.zip\",",
154+
" \"integrity\": \"sha256-blah\",",
155+
" \"strip_prefix\": \"pref\"",
156+
"}");
157+
158+
Registry registry = registryFactory.getRegistryWithUrl(server.getUrl());
159+
assertThat(registry.getRepoSpec(createModuleKey("foo", "1.0"), "foorepo", reporter))
160+
.isEqualTo(
161+
new ArchiveRepoSpecBuilder()
162+
.setRepoName("foorepo")
163+
.setUrls(ImmutableList.of("http://mysite.com/thing.zip"))
164+
.setIntegrity("sha256-blah")
165+
.setStripPrefix("pref")
166+
.setRemotePatches(ImmutableMap.of())
167+
.setRemotePatchStrip(0)
168+
.build());
169+
}
170+
171+
@Test
172+
public void testGetRepoInvalidModuleJsonSpec() throws Exception {
173+
server.serve(
174+
"/bazel_registry.json",
175+
"{",
176+
" \"mirrors\": [",
177+
" \"https://mirror.bazel.build/\",",
178+
" \"file:///home/bazel/mymirror/\"",
179+
" ]",
180+
"}");
181+
server.serve(
182+
"/modules/foo/1.0/source.json",
183+
"{",
184+
" \"url\": \"http://mysite.com/thing.zip\",",
185+
" \"integrity\": \"sha256-blah\",",
186+
" \"strip_prefix\": \"pref\",",
187+
"}");
188+
server.start();
189+
190+
Registry registry = registryFactory.getRegistryWithUrl(server.getUrl());
191+
assertThrows(
192+
IOException.class,
193+
() -> registry.getRepoSpec(createModuleKey("foo", "1.0"), "foorepo", reporter));
194+
}
143195
}

0 commit comments

Comments
 (0)