-
-
Notifications
You must be signed in to change notification settings - Fork 79
Update vulnerabilities to use new Tool specification #438
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f53b446
add new tool schema
shaikhu a4a5c9b
add deprecated annotation
shaikhu e33c3d9
update vulnerability deserialization
shaikhu ec76f2c
refactor dup code to deserializer
shaikhu 28ae2b1
add empty line at eof
shaikhu 1d1b411
add license headers
shaikhu 21abe5f
Refactor into single if statement
shaikhu fa67e9d
use consistent date format
shaikhu 409aa99
update examples not to use deprecated field
shaikhu 691649c
drop JacksonXmlElementWrapper annotation
shaikhu 28e15c1
Refactor to common json parser
shaikhu 978fcca
merge main and fix failing test
shaikhu 16a7cce
Merge branch 'master' into master
mr-zepol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * This file is part of CycloneDX Core (Java). | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright (c) OWASP Foundation. All Rights Reserved. | ||
| */ | ||
| package org.cyclonedx.util; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public final class TimestampUtils { | ||
| private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); | ||
shaikhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private TimestampUtils() {} | ||
|
|
||
| public static Date parseTimestamp(String text) { | ||
| try { | ||
| return DATE_FORMAT.parse(text); | ||
| } catch (ParseException | NullPointerException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
src/main/java/org/cyclonedx/util/deserializer/AffectDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * This file is part of CycloneDX Core (Java). | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright (c) OWASP Foundation. All Rights Reserved. | ||
| */ | ||
| package org.cyclonedx.util.deserializer; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.ObjectCodec; | ||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.cyclonedx.model.vulnerability.Vulnerability; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class AffectDeserializer | ||
shaikhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extends JsonDeserializer<Vulnerability.Affect> | ||
| { | ||
| private final ObjectMapper mapper = new ObjectMapper(); | ||
|
|
||
| @Override | ||
| public Vulnerability.Affect deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
| ObjectCodec codec = parser.getCodec(); | ||
| JsonNode node = codec.readTree(parser); | ||
|
|
||
| Vulnerability.Affect affect = new Vulnerability.Affect(); | ||
|
|
||
| JsonNode refNode = node.get("ref"); | ||
| if (refNode != null) { | ||
| affect.setRef(refNode.asText()); | ||
| } | ||
|
|
||
| JsonNode versionsNode = node.get("versions"); | ||
| if (versionsNode != null) { | ||
| if (versionsNode.isArray()) { | ||
| List<Vulnerability.Version> versions = mapper.convertValue(node.get("versions"), new TypeReference<List<Vulnerability.Version>>() {}); | ||
| affect.setVersions(versions); | ||
| } else if (versionsNode.has("version")) { | ||
| JsonNode versionNode = versionsNode.get("version"); | ||
| if (versionNode.isArray()) { | ||
| List<Vulnerability.Version> versions = mapper.convertValue(versionNode, new TypeReference<List<Vulnerability.Version>>() {}); | ||
| affect.setVersions(versions); | ||
| } else { | ||
| affect.setVersions(Collections.singletonList( | ||
| mapper.convertValue(versionNode, Vulnerability.Version.class) | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return affect; | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/main/java/org/cyclonedx/util/deserializer/AffectsDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * This file is part of CycloneDX Core (Java). | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright (c) OWASP Foundation. All Rights Reserved. | ||
| */ | ||
| package org.cyclonedx.util.deserializer; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import org.cyclonedx.model.vulnerability.Vulnerability; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class AffectsDeserializer | ||
| extends JsonDeserializer<List<Vulnerability.Affect>> | ||
| { | ||
| private final AffectDeserializer affectDeserializer = new AffectDeserializer(); | ||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| @Override | ||
| public List<Vulnerability.Affect> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
| JsonNode node = p.getCodec().readTree(p); | ||
| return parseAffects(node.has("target") ? node.get("target") : node, p, ctxt); | ||
| } | ||
|
|
||
| private List<Vulnerability.Affect> parseAffects(JsonNode node, JsonParser p, DeserializationContext ctxt) throws IOException { | ||
| List<Vulnerability.Affect> affects = new ArrayList<>(); | ||
| ArrayNode nodes = DeserializerUtils.getArrayNode(node, objectMapper); | ||
|
|
||
| for (JsonNode affectNode : nodes) { | ||
| affects.add(parseAffect(affectNode, p, ctxt)); | ||
| } | ||
|
|
||
| return affects; | ||
| } | ||
|
|
||
| private Vulnerability.Affect parseAffect(JsonNode node, JsonParser p, DeserializationContext ctxt) throws IOException { | ||
| JsonParser affectParser = node.traverse(p.getCodec()); | ||
| affectParser.nextToken(); | ||
| return affectDeserializer.deserialize(affectParser, ctxt); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.