Skip to content

Commit 1b463e1

Browse files
committed
Merge pull request #348 from GoogleCloudPlatform:ee8-invalidPathSpec
PiperOrigin-RevId: 733945939 Change-Id: I556668b063c3596fe9bc63d48cd3cfb6e30718d4
2 parents d474278 + 7b03cd6 commit 1b463e1

4 files changed

Lines changed: 132 additions & 2 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<maven.compiler.target>1.8</maven.compiler.target>
6666
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6767
<jetty.version>9.4.57.v20241219</jetty.version>
68-
<jetty12.version>12.0.16</jetty12.version>
68+
<jetty12.version>12.0.17</jetty12.version>
6969
<io.grpc>1.70.0</io.grpc>
7070
<io.netty>4.1.119.Final</io.netty>
7171
<slf4j.version>2.0.17</slf4j.version>

runtime/local_jetty12/src/main/java/com/google/appengine/tools/development/jetty/JettyContainerService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,6 @@ public void doScope(
647647
Semaphore semaphore =
648648
(Semaphore) env.getAttributes().get(LocalEnvironment.API_CALL_SEMAPHORE);
649649
try {
650-
System.err.println("=========== acquire semaphore ===========");
651650
semaphore.acquire(MAX_SIMULTANEOUS_API_CALLS);
652651
} catch (InterruptedException ex) {
653652
Thread.currentThread().interrupt();

runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee8/AppEngineWebAppContext.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.apphosting.utils.servlet.SnapshotServlet;
2929
import com.google.apphosting.utils.servlet.WarmupServlet;
3030
import com.google.common.collect.ImmutableMap;
31+
import com.google.common.flogger.GoogleLogger;
3132
import java.io.File;
3233
import java.io.FileNotFoundException;
3334
import java.io.IOException;
@@ -51,13 +52,15 @@
5152
import org.eclipse.jetty.ee8.nested.ServletConstraint;
5253
import org.eclipse.jetty.ee8.security.ConstraintMapping;
5354
import org.eclipse.jetty.ee8.security.ConstraintSecurityHandler;
55+
import org.eclipse.jetty.ee8.security.SecurityHandler;
5456
import org.eclipse.jetty.ee8.servlet.FilterHolder;
5557
import org.eclipse.jetty.ee8.servlet.FilterMapping;
5658
import org.eclipse.jetty.ee8.servlet.ListenerHolder;
5759
import org.eclipse.jetty.ee8.servlet.ServletHandler;
5860
import org.eclipse.jetty.ee8.servlet.ServletHolder;
5961
import org.eclipse.jetty.ee8.servlet.ServletMapping;
6062
import org.eclipse.jetty.ee8.webapp.WebAppContext;
63+
import org.eclipse.jetty.http.pathmap.PathSpec;
6164
import org.eclipse.jetty.util.resource.Resource;
6265
import org.eclipse.jetty.util.resource.ResourceFactory;
6366

@@ -70,6 +73,7 @@
7073
// will allow to enable Servlet Async capabilities later, controlled programmatically instead of
7174
// declaratively in webdefault.xml.
7275
public class AppEngineWebAppContext extends WebAppContext {
76+
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
7377

7478
// TODO: This should be some sort of Prometheus-wide
7579
// constant. If it's much larger than this we may need to
@@ -151,6 +155,24 @@ public AppEngineWebAppContext(File appDir, String serverInfo, boolean extractWar
151155
ignoreContentLength = isAppIdForNonContentLength();
152156
}
153157

158+
@Override
159+
protected SecurityHandler newSecurityHandler() {
160+
return new ConstraintSecurityHandler() {
161+
@Override
162+
protected PathSpec asPathSpec(ConstraintMapping mapping) {
163+
try {
164+
// As currently written, this allows regex patterns to be used.
165+
// This may not be supported by default in future releases.
166+
return PathSpec.from(mapping.getPathSpec());
167+
} catch (Throwable t) {
168+
logger.atWarning().log(
169+
"Invalid pathSpec '%s', using literal mapping instead", mapping.getPathSpec());
170+
return new LiteralPathSpec(mapping.getPathSpec());
171+
}
172+
}
173+
};
174+
}
175+
154176
@Override
155177
protected ClassLoader configureClassLoader(ClassLoader loader) {
156178
// Avoid wrapping the provided classloader with WebAppClassLoader.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.apphosting.runtime.jetty.ee8;
18+
19+
import org.eclipse.jetty.http.pathmap.AbstractPathSpec;
20+
import org.eclipse.jetty.http.pathmap.MatchedPath;
21+
import org.eclipse.jetty.http.pathmap.PathSpecGroup;
22+
import org.eclipse.jetty.util.StringUtil;
23+
24+
public class LiteralPathSpec extends AbstractPathSpec
25+
{
26+
private final String _pathSpec;
27+
private final int _pathDepth;
28+
29+
public LiteralPathSpec(String pathSpec)
30+
{
31+
if (StringUtil.isEmpty(pathSpec))
32+
throw new IllegalArgumentException();
33+
_pathSpec = pathSpec;
34+
35+
int pathDepth = 0;
36+
for (int i = 0; i < _pathSpec.length(); i++)
37+
{
38+
char c = _pathSpec.charAt(i);
39+
if (c < 128)
40+
{
41+
if (c == '/')
42+
pathDepth++;
43+
}
44+
}
45+
_pathDepth = pathDepth;
46+
}
47+
48+
@Override
49+
public int getSpecLength()
50+
{
51+
return _pathSpec.length();
52+
}
53+
54+
@Override
55+
public PathSpecGroup getGroup()
56+
{
57+
return PathSpecGroup.EXACT;
58+
}
59+
60+
@Override
61+
public int getPathDepth()
62+
{
63+
return _pathDepth;
64+
}
65+
66+
@Override
67+
public String getPathInfo(String path)
68+
{
69+
return _pathSpec.equals(path) ? "" : null;
70+
}
71+
72+
@Override
73+
public String getPathMatch(String path)
74+
{
75+
return _pathSpec.equals(path) ? _pathSpec : null;
76+
}
77+
78+
@Override
79+
public String getDeclaration()
80+
{
81+
return _pathSpec;
82+
}
83+
84+
@Override
85+
public String getPrefix()
86+
{
87+
return null;
88+
}
89+
90+
@Override
91+
public String getSuffix()
92+
{
93+
return null;
94+
}
95+
96+
@Override
97+
public MatchedPath matched(String path)
98+
{
99+
if (_pathSpec.equals(path))
100+
return MatchedPath.from(_pathSpec, null);
101+
return null;
102+
}
103+
104+
@Override
105+
public boolean matches(String path)
106+
{
107+
return _pathSpec.equals(path);
108+
}
109+
}

0 commit comments

Comments
 (0)