Skip to content
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

fixed: fix Log Injection CWE-117 #604

Merged
merged 8 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/********************************************************************************
* Copyright (c) 2022, 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022, 2023 ZF Friedrichshafen AG
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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
********************************************************************************/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/********************************************************************************
* Copyright (c) 2022, 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022, 2023 ZF Friedrichshafen AG
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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
********************************************************************************/

Please use this as a copyright header.


package org.eclipse.tractusx.traceability.common.model;


public class EDC {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDC means Eclipse Dataspace Connector. This class should have a security related name.

Suggested change
public class EDC {
public class SecurityUtils {


public static String sanitizer(String unsanitizedInput) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static String sanitizer(String unsanitizedInput) {
public static String sanitize(String unsanitizedInput) {

return unsanitizedInput.replaceAll("\r\n|\r|\n", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,28 @@
package org.eclipse.tractusx.traceability.qualitynotification.infrastructure.edc.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.eclipse.tractusx.traceability.common.model.EDC;

import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record EDCNotificationContent(
String information,
List<String> listOfAffectedItems) {

@Override
public String toString() {
String cleanInformation = EDC.sanitizer(information);
List<String> cleanListOfAffectedItems = new ArrayList<>();
for (String AffectedItems : listOfAffectedItems) {
String cleanAffectedItem = EDC.sanitizer(AffectedItems);
cleanListOfAffectedItems.add(cleanAffectedItem);
}
return "EDCNotificationContent{" +
"information='" + cleanInformation + '\'' +
", listOfAffectedItems=" + cleanListOfAffectedItems +
ds-lcapellino marked this conversation as resolved.
Show resolved Hide resolved
'}';
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String toString() {
String cleanInformation = EDC.sanitizer(information);
List<String> cleanListOfAffectedItems = new ArrayList<>();
for (String AffectedItems : listOfAffectedItems) {
String cleanAffectedItem = EDC.sanitizer(AffectedItems);
cleanListOfAffectedItems.add(cleanAffectedItem);
}
return "EDCNotificationContent{" +
"information='" + cleanInformation + '\'' +
", listOfAffectedItems=" + cleanListOfAffectedItems +
'}';
}
public String toString() {
List<String> cleanListOfAffectedItems = new ArrayList<>();
for (String affectedItem : listOfAffectedItems) {
String cleanAffectedItem = EDC.sanitizer(AffectedItems);
cleanListOfAffectedItems.add(cleanAffectedItem);
}
return "EDCNotificationContent{" +
"information='" + EDC.sanitizer(information) + '\'' +
", listOfAffectedItems=" + cleanListOfAffectedItems +
'}';
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,30 @@
package org.eclipse.tractusx.traceability.qualitynotification.infrastructure.edc.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.eclipse.tractusx.traceability.common.model.EDC;


@JsonInclude(JsonInclude.Include.NON_NULL)
public record EDCNotificationHeader(String notificationId, String senderBPN, String senderAddress, String recipientBPN,
String classification, String severity, String relatedNotificationId,
String status, String targetDate, String messageId) {
String classification, String severity, String relatedNotificationId,
String status, String targetDate, String messageId) {


@Override
public String toString() {
return "EDCNotificationHeader{" +
"notificationId='" + EDC.sanitizer(notificationId) + '\'' +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the class as static import so the class name is not visible in each line

", senderBPN='" + EDC.sanitizer(senderBPN) + '\'' +
", senderAddress='" + EDC.sanitizer(senderAddress) + '\'' +
", recipientBPN='" + EDC.sanitizer(recipientBPN) + '\'' +
", classification='" + EDC.sanitizer(classification) + '\'' +
", severity='" + EDC.sanitizer(severity) + '\'' +
", relatedNotificationId='" + EDC.sanitizer(relatedNotificationId) + '\'' +
", status='" + EDC.sanitizer(status) + '\'' +
", targetDate='" + EDC.sanitizer(targetDate) + '\'' +
", messageId='" + EDC.sanitizer(messageId) + '\'' +
'}';
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/********************************************************************************
* Copyright (c) 2022, 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022, 2023 ZF Friedrichshafen AG
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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
********************************************************************************/
package org.eclipse.tractusx.traceability.infrastructure.edc.model;

import org.eclipse.tractusx.traceability.qualitynotification.infrastructure.edc.model.EDCNotificationContent;
import org.eclipse.tractusx.traceability.qualitynotification.infrastructure.edc.model.EDCNotificationHeader;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class EdcNotificationModelTest {

@Test
public void testToStringWithRegex() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the regex in this method?

EDCNotificationHeader header = new EDCNotificationHeader(
ds-lcapellino marked this conversation as resolved.
Show resolved Hide resolved
"12345", "SenderBPN", "Sender\nAddress", "RecipientBPN",
"Classification", "Severity", "Related\nNotificationId",
"Status", "2023-09-22", "MessageId"
);

String expected = "EDCNotificationHeader{" +
"notificationId='12345', " +
"senderBPN='SenderBPN', " +
"senderAddress='Sender Address', " +
"recipientBPN='RecipientBPN', " +
"classification='Classification', " +
"severity='Severity', " +
"relatedNotificationId='Related NotificationId', " +
"status='Status', " +
"targetDate='2023-09-22', " +
"messageId='MessageId'}";

String actual = header.toString();
ds-lcapellino marked this conversation as resolved.
Show resolved Hide resolved

assertEquals(expected, actual);
ds-lcapellino marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testToString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please insert same //GIVEN //WHEN //THEN pattern as above.

List<String> listOfAffectedItems = new ArrayList<>(Arrays.asList("Item1\nItem2", "Item3", "Item4\r\nItem5"));

EDCNotificationContent content = new EDCNotificationContent(
"Information\nwith\nline\nbreaks", listOfAffectedItems
);

String expected = "EDCNotificationContent{" +
"information='Information with line breaks', " +
"listOfAffectedItems=[Item1 Item2, Item3, Item4 Item5]" +
"}";

String actual = content.toString();

assertEquals(expected, actual);
}
}