feature: rework guardrails

This commit is contained in:
Dobin Rutishauser
2025-06-09 22:09:32 +02:00
parent f55596f4a8
commit ca1f497074
5 changed files with 84 additions and 33 deletions
+37 -20
View File
@@ -1,29 +1,46 @@
char my_tolower(char c) {
if (c >= 'A' && c <= 'Z') {
return c + ('a' - 'A'); // or return c + 32;
}
return c;
}
int mystrcmp(wchar_t* str1, wchar_t* str2) {
int i = 0;
while (str1[i] != L'\0' && str2[i] != L'\0') {
if (str1[i] != str2[i]) {
return 1;
}
i++;
}
return 0;
// Returns 1 if 'needle' is found in 'haystack' (case-insensitive), 0 otherwise
int contains_case_insensitive(const char* haystack, const char* needle) {
if (!haystack || !needle)
return 0;
for (; *haystack; haystack++) {
const char* h = haystack;
const char* n = needle;
while (*h && *n && my_tolower((unsigned char)*h) == my_tolower((unsigned char)*n)) {
h++;
n++;
}
if (*n == '\0') {
return 1; // Match found
}
}
return 0; // No match
}
int executionguardrail() {
// Execution Guardrail: Env Check
wchar_t envVarName[] = L"USERPROFILE";
wchar_t tocheck[] = L"{{guardrail_data}}";
WCHAR buffer[1024]; // NOTE: Do not make it bigger, or we have a __chkstack() dependency!
DWORD result = GetEnvironmentVariableW(envVarName, buffer, 1024);
if (result == 0) {
return 6;
}
if (mystrcmp(buffer, tocheck) != 0) {
return 6;
}
// Execution Guardrail: Env Check
LPCSTR envVarName = "{{guardrail_data_key}}";
LPCSTR tocheck = "{{guardrail_data_value}}";
char buffer[1024]; // NOTE: Do not make it bigger, or we have a __chkstack() dependency!
DWORD result = GetEnvironmentVariableA(envVarName, buffer, 1024);
if (result == 0) {
return 6;
}
if (! contains_case_insensitive(buffer, tocheck)) {
return 6;
}
return 0;
}