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

Apply an excessively permissive large string length limit by default #2601

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-2601.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Apply a large StreamReadConstraints maxStringLength to reduce friction
in preparation for jackson 2.15 adoption
links:
- https://github.com/palantir/conjure-java-runtime/pull/2601
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ public static CBORFactory cborFactory() {

/** Configures provided JsonFactory with Conjure default settings. */
private static <F extends JsonFactory, B extends TSFBuilder<F, B>> B withDefaults(B builder) {
return builder
return ReflectiveStreamReadConstraints.withDefaultConstraints(builder
// Interning introduces excessive contention https://github.com/FasterXML/jackson-core/issues/946
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* (c) Copyright 2023 Palantir Technologies Inc. All rights reserved.
*
* 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.
*/

package com.palantir.conjure.java.serialization;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.TSFBuilder;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.lang.reflect.Method;

/**
* This class exists to ensure default values match our expectations in cases where jackson is
* upgraded transitively prior to a CJR release which expects 2.15.0+. After this library upgrades, the
* reflection may be replaced by the following:
*<pre>{@code
* return builder.streamReadConstraints(StreamReadConstraints.builder()
* .maxStringLength(MAX_STRING_LENGTH)
* .build());
* }</pre>
*/
final class ReflectiveStreamReadConstraints {
private static final SafeLogger log = SafeLoggerFactory.get(ReflectiveStreamReadConstraints.class);

// 50mb up from the default 5mb as a more permissive value to begin with, which we can ratchet down over time.
// This allows us to decouple the initial risk of adopting string length limits from the risk introduced by taking
// a dependency upgrade.
private static final int MAX_STRING_LENGTH = 50_000_000;

@SuppressWarnings("unchecked")
static <F extends JsonFactory, B extends TSFBuilder<F, B>> B withDefaultConstraints(B builder) {
try {
// Use the same classloader which loaded the TSFBuilder
ClassLoader classLoader = builder.getClass().getClassLoader();
Class<?> streamReadConstraintsClass =
Class.forName("com.fasterxml.jackson.core.StreamReadConstraints", true, classLoader);
Object constraints = createConjureDefaultStreamReadConstraints(streamReadConstraintsClass);
return (B) builder.getClass()
.getMethod("streamReadConstraints", streamReadConstraintsClass)
.invoke(builder, constraints);
} catch (ClassNotFoundException cnfe) {
// Log at debug in the expected case using jackson 2.14 which does not support limits.
log.debug("StreamReadConstraints class does not exist, nothing to do", cnfe);
} catch (ReflectiveOperationException e) {
log.warn("Failed to update StreamReadConstraints, upstream default values will be used", e);
}
return builder;
}

private static Object createConjureDefaultStreamReadConstraints(Class<?> streamReadConstraintsClass)
throws ReflectiveOperationException {
Method builderMethod = streamReadConstraintsClass.getMethod("builder");
Object streamReadConstraintsBuilder = builderMethod.invoke(null);
Class<?> streamReadConstraintsBuilderClass = streamReadConstraintsBuilder.getClass();
// Default to a max size of 50mb (up from the 5mb default)
streamReadConstraintsBuilderClass
.getMethod("maxStringLength", int.class)
.invoke(streamReadConstraintsBuilder, MAX_STRING_LENGTH);
return streamReadConstraintsBuilderClass.getMethod("build").invoke(streamReadConstraintsBuilder);
}

private ReflectiveStreamReadConstraints() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ private void testMapKeysAreNotInterned(ObjectMapper mapper) throws IOException {
.isEmpty();
}

@Test
public void testExtraordinarilyLargeStrings() throws IOException {
// Value must sit between StreamReadConstraints.DEFAULT_MAX_STRING_LEN and
// ReflectiveStreamReadConstraints.MAX_STRING_LENGTH.
int size = 10_000_000;
String parsed = ObjectMappers.newServerJsonMapper().readValue('"' + "a".repeat(size) + '"', String.class);
assertThat(parsed).hasSize(size);
}

private static String ser(Object object) throws IOException {
return MAPPER.writeValueAsString(object);
}
Expand Down