|
23 | 23 | import io.apicurio.registry.model.GA;
|
24 | 24 | import io.apicurio.registry.model.GAV;
|
25 | 25 | import io.apicurio.registry.model.VersionId;
|
| 26 | +import io.apicurio.registry.rest.RestConfig; |
26 | 27 | import io.apicurio.registry.rules.compatibility.CompatibilityLevel;
|
27 | 28 | import io.apicurio.registry.rules.integrity.IntegrityLevel;
|
28 | 29 | import io.apicurio.registry.rules.validity.ValidityLevel;
|
@@ -219,6 +220,9 @@ public abstract class AbstractSqlRegistryStorage implements RegistryStorage {
|
219 | 220 | @Inject
|
220 | 221 | SemVerConfigProperties semVerConfigProps;
|
221 | 222 |
|
| 223 | + @Inject |
| 224 | + RestConfig restConfig; |
| 225 | + |
222 | 226 | @Inject
|
223 | 227 |
|
224 | 228 | protected SqlStatements sqlStatements() {
|
@@ -1112,6 +1116,7 @@ public ArtifactSearchResultsDto searchArtifacts(Set<SearchFilter> filters, Order
|
1112 | 1116 |
|
1113 | 1117 | // Execute artifact query
|
1114 | 1118 | List<SearchedArtifactDto> artifacts = artifactsQuery.map(SearchedArtifactMapper.instance).list();
|
| 1119 | + limitReturnedLabelsInArtifacts(artifacts); |
1115 | 1120 | // Execute count query
|
1116 | 1121 | Integer count = countQuery.mapTo(Integer.class).one();
|
1117 | 1122 |
|
@@ -1736,6 +1741,7 @@ public VersionSearchResultsDto searchVersions(Set<SearchFilter> filters, OrderBy
|
1736 | 1741 |
|
1737 | 1742 | // Execute query
|
1738 | 1743 | List<SearchedVersionDto> versions = versionsQuery.map(SearchedVersionMapper.instance).list();
|
| 1744 | + limitReturnedLabelsInVersions(versions); |
1739 | 1745 | // Execute count query
|
1740 | 1746 | Integer count = countQuery.mapTo(Integer.class).one();
|
1741 | 1747 |
|
@@ -2907,6 +2913,8 @@ public GroupSearchResultsDto searchGroups(Set<SearchFilter> filters, OrderBy ord
|
2907 | 2913 |
|
2908 | 2914 | // Execute query
|
2909 | 2915 | List<SearchedGroupDto> groups = groupsQuery.map(SearchedGroupMapper.instance).list();
|
| 2916 | + limitReturnedLabelsInGroups(groups); |
| 2917 | + |
2910 | 2918 | // Execute count query
|
2911 | 2919 | Integer count = countQuery.mapTo(Integer.class).one();
|
2912 | 2920 |
|
@@ -3539,6 +3547,7 @@ public VersionSearchResultsDto getBranchVersions(GA ga, BranchId branchId, int o
|
3539 | 3547 |
|
3540 | 3548 | // Execute query
|
3541 | 3549 | List<SearchedVersionDto> versions = versionsQuery.map(SearchedVersionMapper.instance).list();
|
| 3550 | + limitReturnedLabelsInVersions(versions); |
3542 | 3551 | // Execute count query
|
3543 | 3552 | Integer count = countQuery.mapTo(Integer.class).one();
|
3544 | 3553 |
|
@@ -3788,4 +3797,52 @@ private boolean isMssql() {
|
3788 | 3797 | private boolean isH2() {
|
3789 | 3798 | return sqlStatements.dbType().equals("h2");
|
3790 | 3799 | }
|
| 3800 | + |
| 3801 | + /* |
| 3802 | + * Ensures that only a reasonable number/size of labels for each item in the list are returned. This is to |
| 3803 | + * guard against an unexpectedly enormous response size to a REST API search operation. |
| 3804 | + */ |
| 3805 | + |
| 3806 | + private Map<String, String> limitReturnedLabels(Map<String, String> labels) { |
| 3807 | + int maxBytes = restConfig.getLabelsInSearchResultsMaxSize(); |
| 3808 | + if (labels != null && !labels.isEmpty()) { |
| 3809 | + Map<String, String> cappedLabels = new HashMap<>(); |
| 3810 | + int totalBytes = 0; |
| 3811 | + for (String key : labels.keySet()) { |
| 3812 | + if (totalBytes < maxBytes) { |
| 3813 | + String value = labels.get(key); |
| 3814 | + cappedLabels.put(key, value); |
| 3815 | + totalBytes += key.length() + (value != null ? value.length() : 0); |
| 3816 | + } |
| 3817 | + } |
| 3818 | + return cappedLabels; |
| 3819 | + } |
| 3820 | + |
| 3821 | + return null; |
| 3822 | + } |
| 3823 | + |
| 3824 | + private void limitReturnedLabelsInGroups(List<SearchedGroupDto> groups) { |
| 3825 | + groups.forEach(group -> { |
| 3826 | + Map<String, String> labels = group.getLabels(); |
| 3827 | + Map<String, String> cappedLabels = limitReturnedLabels(labels); |
| 3828 | + group.setLabels(cappedLabels); |
| 3829 | + }); |
| 3830 | + } |
| 3831 | + |
| 3832 | + private void limitReturnedLabelsInArtifacts(List<SearchedArtifactDto> artifacts) { |
| 3833 | + artifacts.forEach(artifact -> { |
| 3834 | + Map<String, String> labels = artifact.getLabels(); |
| 3835 | + Map<String, String> cappedLabels = limitReturnedLabels(labels); |
| 3836 | + artifact.setLabels(cappedLabels); |
| 3837 | + }); |
| 3838 | + } |
| 3839 | + |
| 3840 | + private void limitReturnedLabelsInVersions(List<SearchedVersionDto> versions) { |
| 3841 | + versions.forEach(version -> { |
| 3842 | + Map<String, String> labels = version.getLabels(); |
| 3843 | + Map<String, String> cappedLabels = limitReturnedLabels(labels); |
| 3844 | + version.setLabels(cappedLabels); |
| 3845 | + }); |
| 3846 | + } |
| 3847 | + |
3791 | 3848 | }
|
0 commit comments