Skip to content

Commit e51a15f

Browse files
uhthomascopybara-github
authored andcommitted
Add 'integrity' attribute to http_file and http_jar
Additionally, update the documentation for the 'sha256' attribute on 'http_jar' to be consistent. Fixes #16130 Closes #16131. PiperOrigin-RevId: 468682337 Change-Id: I79938b61bfe90838a975d58253d33e38da1b98e0
1 parent 6a66b0b commit e51a15f

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

tools/build_defs/repo/http.bzl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def _get_auth(ctx, urls):
114114
netrc = read_user_netrc(ctx)
115115
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
116116

117+
def _update_sha256_attr(ctx, attrs, download_info):
118+
# We don't need to override the sha256 attribute if integrity is already specified.
119+
sha256_override = {} if ctx.attr.integrity else {"sha256": download_info.sha256}
120+
return update_attrs(ctx.attr, attrs.keys(), sha256_override)
121+
117122
def _http_archive_impl(ctx):
118123
"""Implementation of the http_archive rule."""
119124
if ctx.attr.build_file and ctx.attr.build_file_content:
@@ -135,9 +140,7 @@ def _http_archive_impl(ctx):
135140
workspace_and_buildfile(ctx)
136141
patch(ctx, auth = auth)
137142

138-
# We don't need to override the sha256 attribute if integrity is already specified.
139-
sha256_override = {} if ctx.attr.integrity else {"sha256": download_info.sha256}
140-
return update_attrs(ctx.attr, _http_archive_attrs.keys(), sha256_override)
143+
return _update_sha256_attr(ctx, _http_archive_attrs, download_info)
141144

142145
_HTTP_FILE_BUILD = """\
143146
package(default_visibility = ["//visibility:public"])
@@ -172,11 +175,12 @@ def _http_file_impl(ctx):
172175
ctx.attr.executable,
173176
canonical_id = ctx.attr.canonical_id,
174177
auth = auth,
178+
integrity = ctx.attr.integrity,
175179
)
176180
ctx.file("WORKSPACE", "workspace(name = \"{name}\")".format(name = ctx.name))
177181
ctx.file("file/BUILD", _HTTP_FILE_BUILD.format(downloaded_file_path))
178182

179-
return update_attrs(ctx.attr, _http_file_attrs.keys(), {"sha256": download_info.sha256})
183+
return _update_sha256_attr(ctx, _http_file_attrs, download_info)
180184

181185
_HTTP_JAR_BUILD = """\
182186
load("@rules_java//java:defs.bzl", "java_import")
@@ -208,10 +212,12 @@ def _http_jar_impl(ctx):
208212
ctx.attr.sha256,
209213
canonical_id = ctx.attr.canonical_id,
210214
auth = auth,
215+
integrity = ctx.attr.integrity,
211216
)
212217
ctx.file("WORKSPACE", "workspace(name = \"{name}\")".format(name = ctx.name))
213218
ctx.file("jar/BUILD", _HTTP_JAR_BUILD.format(file_name = downloaded_file_name))
214-
return update_attrs(ctx.attr, _http_jar_attrs.keys(), {"sha256": download_info.sha256})
219+
220+
return _update_sha256_attr(ctx, _http_jar_attrs, download_info)
215221

216222
_http_archive_attrs = {
217223
"url": attr.string(doc = _URL_DOC),
@@ -420,6 +426,14 @@ This must match the SHA-256 of the file downloaded. _It is a security risk
420426
to omit the SHA-256 as remote files can change._ At best omitting this
421427
field will make your build non-hermetic. It is optional to make development
422428
easier but should be set before shipping.""",
429+
),
430+
"integrity": attr.string(
431+
doc = """Expected checksum in Subresource Integrity format of the file downloaded.
432+
433+
This must match the checksum of the file downloaded. _It is a security risk
434+
to omit the checksum as remote files can change._ At best omitting this
435+
field will make your build non-hermetic. It is optional to make development
436+
easier but either this attribute or `sha256` should be set before shipping.""",
423437
),
424438
"canonical_id": attr.string(
425439
doc = """A canonical id of the archive downloaded.
@@ -466,7 +480,20 @@ Examples:
466480

467481
_http_jar_attrs = {
468482
"sha256": attr.string(
469-
doc = "The expected SHA-256 of the file downloaded.",
483+
doc = """The expected SHA-256 of the file downloaded.
484+
485+
This must match the SHA-256 of the file downloaded. _It is a security risk
486+
to omit the SHA-256 as remote files can change._ At best omitting this
487+
field will make your build non-hermetic. It is optional to make development
488+
easier but either this attribute or `integrity` should be set before shipping.""",
489+
),
490+
"integrity": attr.string(
491+
doc = """Expected checksum in Subresource Integrity format of the file downloaded.
492+
493+
This must match the checksum of the file downloaded. _It is a security risk
494+
to omit the checksum as remote files can change._ At best omitting this
495+
field will make your build non-hermetic. It is optional to make development
496+
easier but either this attribute or `sha256` should be set before shipping.""",
470497
),
471498
"canonical_id": attr.string(
472499
doc = """A canonical id of the archive downloaded.

0 commit comments

Comments
 (0)