-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32b2c25
commit 94c5260
Showing
5 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
83 changes: 83 additions & 0 deletions
83
...nd/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilter.java
This file contains 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,83 @@ | ||
/* | ||
* SonarLint Core - Implementation | ||
* Copyright (C) 2016-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonarsource.sonarlint.core.embedded.server; | ||
|
||
import org.apache.hc.core5.http.ClassicHttpRequest; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.HttpStatus; | ||
import org.apache.hc.core5.http.io.HttpFilterChain; | ||
import org.apache.hc.core5.http.io.HttpFilterHandler; | ||
import org.apache.hc.core5.http.message.BasicClassicHttpResponse; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class RateLimitFilter implements HttpFilterHandler { | ||
|
||
private static final int MAX_REQUESTS_PER_ORIGIN = 10; | ||
private static final long TIME_FRAME_MS = TimeUnit.SECONDS.toMillis(10); | ||
private final ConcurrentHashMap<String, RequestCounter> requestCounters = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void handle(ClassicHttpRequest request, HttpFilterChain.ResponseTrigger responseTrigger, HttpContext context, HttpFilterChain chain) | ||
throws HttpException, IOException { | ||
var originHeader = request.getHeader("Origin"); | ||
var origin = originHeader != null ? originHeader.getValue() : null; | ||
if (origin == null) { | ||
var response = new BasicClassicHttpResponse(HttpStatus.SC_BAD_REQUEST); | ||
responseTrigger.submitResponse(response); | ||
} else { | ||
if (!isRequestAllowed(origin)) { | ||
var response = new BasicClassicHttpResponse(HttpStatus.SC_TOO_MANY_REQUESTS); | ||
responseTrigger.submitResponse(response); | ||
} else { | ||
chain.proceed(request, responseTrigger, context); | ||
} | ||
} | ||
} | ||
|
||
private boolean isRequestAllowed(String origin) { | ||
long currentTime = System.currentTimeMillis(); | ||
var counter = requestCounters.computeIfAbsent(origin, k -> new RequestCounter(currentTime)); | ||
requestCounters.compute(origin, (k, v) -> { | ||
if (currentTime - counter.timestamp > TIME_FRAME_MS) { | ||
counter.timestamp = currentTime; | ||
counter.count = 1; | ||
} else { | ||
counter.count++; | ||
} | ||
return counter; | ||
}); | ||
return counter.count <= MAX_REQUESTS_PER_ORIGIN; | ||
} | ||
|
||
private static class RequestCounter { | ||
long timestamp; | ||
int count; | ||
|
||
RequestCounter(long timestamp) { | ||
this.timestamp = timestamp; | ||
this.count = 0; | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...re/src/test/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilterTests.java
This file contains 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,67 @@ | ||
/* | ||
* SonarLint Core - Implementation | ||
* Copyright (C) 2016-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonarsource.sonarlint.core.embedded.server; | ||
|
||
import org.apache.hc.core5.http.ClassicHttpRequest; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.io.HttpFilterChain; | ||
import org.apache.hc.core5.http.message.BasicHeader; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class RateLimitFilterTests { | ||
|
||
private final ClassicHttpRequest request = mock(ClassicHttpRequest.class); | ||
private final HttpFilterChain.ResponseTrigger responseTrigger = mock(HttpFilterChain.ResponseTrigger.class); | ||
private final HttpContext context = mock(HttpContext.class); | ||
private final HttpFilterChain chain = mock(HttpFilterChain.class); | ||
private RateLimitFilter filter; | ||
|
||
@BeforeEach | ||
void init() { | ||
filter = new RateLimitFilter(); | ||
} | ||
|
||
@Test | ||
void should_not_proceed_with_request_if_origin_is_null() throws HttpException, IOException { | ||
when(request.getHeader("Origin")).thenReturn(null); | ||
|
||
filter.handle(request, responseTrigger, context, chain); | ||
|
||
verify(responseTrigger).submitResponse(any()); | ||
verify(chain, never()).proceed(any(), any(), any()); | ||
} | ||
|
||
@Test | ||
void should_proceed_when_request_is_valid() throws HttpException, IOException { | ||
when(request.getHeader("Origin")).thenReturn(new BasicHeader("Origin", "https://example.com")); | ||
|
||
filter.handle(request, responseTrigger, context, chain); | ||
|
||
verify(responseTrigger, never()).submitResponse(any()); | ||
verify(chain).proceed(any(), any(), any()); | ||
} | ||
|
||
} |
This file contains 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