Skip to content

Commit

Permalink
Use regex to match SECRET_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Jul 30, 2024
1 parent 8267b84 commit f8ea103
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
47 changes: 19 additions & 28 deletions common/seaf-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)
{
char *conf_path = g_build_filename(conf_dir, "seahub_settings.py", NULL);
char *data = NULL;
GRegex *regex = NULL;
GRegex *secret_key_regex = NULL;
GRegex *site_root_regex = NULL;
GError *error = NULL;

FILE *file = fopen(conf_path, "r");
Expand All @@ -397,42 +398,31 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)
goto out;
}

regex = g_regex_new ("'(.+)'", 0, 0, &error);
secret_key_regex = g_regex_new ("SECRET_KEY\\s*=\\s*'(.+)'", 0, 0, &error);
if (error) {
g_clear_error (&error);
seaf_warning ("Failed to create regex: %s\n", error->message);
seaf_warning ("Failed to create secret key regex: %s\n", error->message);
goto out;
}

site_root_regex = g_regex_new ("SITE_ROOT\\s*=\\s*'(.+)'", 0, 0, &error);
if (error) {
g_clear_error (&error);
seaf_warning ("Failed to create site root regex: %s\n", error->message);
goto out;
}

char line[256];
char *key, *value, *saveptr;
char *site_root = NULL;
while (fgets(line, sizeof(line), file)) {
char *p;
key = strtok_r(line, "=", &saveptr);
value = strtok_r(NULL, "\n", &saveptr);

// Trim space of the start
for(p = key;p && *p != '\0';p++) {
if (isspace(*p)) {
key++;
} else {
break;
}
GMatchInfo *match_info;
if (g_regex_match (secret_key_regex, line, 0, &match_info)) {
char *sk = g_match_info_fetch (match_info, 1);
session->seahub_pk = sk;
}

if (key && value && strncmp(key, "SECRET_KEY", 10) == 0) {
GMatchInfo *match_info;
if (g_regex_match (regex, value, 0, &match_info)) {
char *sk = g_match_info_fetch (match_info, 1);
session->seahub_pk = sk;
}
}
if (key && value && strncmp(key, "SITE_ROOT", 9) == 0) {
GMatchInfo *match_info;
if (g_regex_match (regex, value, 0, &match_info)) {
site_root = g_match_info_fetch (match_info, 1);
}
if (g_regex_match (site_root_regex, line, 0, &match_info)) {
site_root = g_match_info_fetch (match_info, 1);
}
}

Expand All @@ -446,7 +436,8 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)
}

out:
g_regex_unref (regex);
g_regex_unref (secret_key_regex);
g_regex_unref (site_root_regex);
g_free (conf_path);
g_free (data);
}
Expand Down
32 changes: 19 additions & 13 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
"syscall"
Expand Down Expand Up @@ -283,22 +284,27 @@ func loadSeahubPK() {

scanner := bufio.NewScanner(file)

pkRe, err := regexp.Compile("SECRET_KEY\\s*=\\s*'([^']*)'")
if err != nil {
log.Warnf("Failed to compile regex: %v", err)
return
}
siteRootRe, err := regexp.Compile("SITE_ROOT\\s*=\\s*'([^']*)'")
if err != nil {
log.Warnf("Failed to compile regex: %v", err)
return
}

siteRoot := ""
for scanner.Scan() {
line := scanner.Text()
strs := strings.SplitN(line, "=", 2)
if len(strs) != 2 {
continue
}
key := strs[0]
value := strs[1]
if strings.Index(key, "SECRET_KEY") >= 0 {
value = strings.Trim(value, " ")
seahubPK = strings.Trim(value, "'")
}
if strings.Index(key, "SITE_ROOT") >= 0 {
value = strings.Trim(value, " ")
siteRoot = strings.Trim(value, "'")
matches := pkRe.FindStringSubmatch(line)
if matches != nil {
seahubPK = matches[1]
}
matches = siteRootRe.FindStringSubmatch(line)
if matches != nil {
siteRoot = matches[1]
}
}
if siteRoot != "" {
Expand Down
1 change: 1 addition & 0 deletions server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ http_tx_manager_get_nickname (const char *modifier)
out:
g_free (jwt_token);
g_free (req_content);
g_free (rsp_content);
connection_pool_return_connection (seaf->seahub_conn_pool, conn);

return nickname;
Expand Down

0 comments on commit f8ea103

Please sign in to comment.