Skip to content

Commit f767409

Browse files
authored
better usage of AllowOrigin setting (#17670)
1 parent 9211297 commit f767409

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

ydb/core/driver_lib/run/run.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ void TKikimrRunner::InitializeMonitoring(const TKikimrRunConfig& runConfig, bool
477477
if (securityConfig.MonitoringAllowedSIDsSize() > 0) {
478478
monConfig.AllowedSIDs.assign(securityConfig.GetMonitoringAllowedSIDs().begin(), securityConfig.GetMonitoringAllowedSIDs().end());
479479
}
480+
monConfig.AllowOrigin = appConfig.GetMonitoringConfig().GetAllowOrigin();
480481

481482
if (ModuleFactories && ModuleFactories->MonitoringFactory) {
482483
Monitoring = ModuleFactories->MonitoringFactory(std::move(monConfig), appConfig);

ydb/core/mon/mon.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <library/cpp/lwtrace/mon/mon_lwtrace.h>
1717
#include <ydb/library/actors/core/probes.h>
1818
#include <ydb/core/base/monitoring_provider.h>
19+
#include <ydb/core/util/wildcard.h>
1920

2021
#include <library/cpp/monlib/service/pages/version_mon_page.h>
2122
#include <library/cpp/monlib/service/pages/mon_page.h>
@@ -403,7 +404,19 @@ class THttpMonLegacyActorRequest : public TActorBootstrapped<THttpMonLegacyActor
403404
type = "application/json";
404405
}
405406
NHttp::THeaders headers(request->Headers);
406-
TString origin = TString(headers["Origin"]);
407+
TString allowOrigin = AppData()->Mon->GetConfig().AllowOrigin;
408+
TString requestOrigin = TString(headers["Origin"]);
409+
TString origin;
410+
if (allowOrigin) {
411+
if (IsMatchesWildcards(requestOrigin, allowOrigin)) {
412+
origin = requestOrigin;
413+
} else {
414+
Send(Event->Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(request->CreateResponseBadRequest("Invalid CORS origin")));
415+
return PassAway();
416+
}
417+
} else if (requestOrigin) {
418+
origin = requestOrigin;
419+
}
407420
if (origin.empty()) {
408421
origin = "*";
409422
}

ydb/core/mon/mon.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TMon {
4040
TString Certificate;
4141
ui32 MaxRequestsPerSecond = 0;
4242
TDuration InactivityTimeout = TDuration::Minutes(2);
43+
TString AllowOrigin;
4344
};
4445

4546
TMon(TConfig config);
@@ -86,6 +87,10 @@ class TMon {
8687
});
8788
}
8889

90+
const TConfig& GetConfig() const {
91+
return Config;
92+
}
93+
8994
protected:
9095
TConfig Config;
9196
TIntrusivePtr<NMonitoring::TIndexMonPage> IndexMonPage;

ydb/core/viewer/viewer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,16 @@ IActor* CreateViewer(const TKikimrRunConfig& kikimrRunConfig) {
729729
}
730730

731731
void TViewer::FillCORS(TStringBuilder& stream, const TRequestState& request) {
732+
TString requestOrigin = request && request.HasHeader("Origin") ? request.GetHeader("Origin") : TString();
732733
TString origin;
733734
if (AllowOrigin) {
734-
origin = AllowOrigin;
735-
} else if (request && request.HasHeader("Origin")) {
736-
origin = request.GetHeader("Origin");
735+
if (IsMatchesWildcards(requestOrigin, AllowOrigin)) {
736+
origin = requestOrigin;
737+
} else {
738+
return; // no CORS headers - no access
739+
}
740+
} else if (requestOrigin) {
741+
origin = requestOrigin;
737742
}
738743
if (origin.empty()) {
739744
origin = "*";

0 commit comments

Comments
 (0)