<commit_before>/**
 * This file is part of the CernVM File System.
 */
#include "scitoken_helper_check.h"
#include "x509_helper_log.h"

#include <scitokens/scitokens.h>

#include <sys/types.h>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
#include <map>
#include <memory>
#include <unistd.h>

using namespace std;  // NOLINT


__attribute__ ((visibility ("default")))
StatusSciTokenValidation CheckSciToken(const string &membership, FILE *fp_token) {

  LogAuthz(kLogAuthzDebug | kLogAuthzSyslog | kLogAuthzSyslogErr, "Checking scitoken");

  SciToken scitoken;

  // Read in the entire scitoken into memory
  string token;
  const unsigned int N=1024;
  while (true) {
    vector<char> buf(N);
    size_t read = fread((void *)&buf[0], 1, N, fp_token);
    if (read) { token.append(buf.begin(), buf.end()); }
    if (read < N) { break; }
    // If the token is larger than 1MB, then stop reading in the token
    // Possible malicious user
    if ( token.size() > (1024 * 1024) ) {
      LogAuthz(kLogAuthzDebug | kLogAuthzSyslog | kLogAuthzSyslogErr, "SciToken larger than 1 MB");
      return kCheckTokenInvalid;
    }
  }

  // Loop through the membership, looking for "https://" issuers
  map<string, string> issuers;
  vector<string> issuers_vec;<commit_msg>Adding error checking for reading the token<commit_after>/**
 * This file is part of the CernVM File System.
 */
#include "scitoken_helper_check.h"
#include "x509_helper_log.h"

#include <scitokens/scitokens.h>

#include <sys/types.h>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
#include <map>
#include <memory>
#include <unistd.h>

using namespace std;  // NOLINT


__attribute__ ((visibility ("default")))
StatusSciTokenValidation CheckSciToken(const string &membership, FILE *fp_token) {

  LogAuthz(kLogAuthzDebug | kLogAuthzSyslog | kLogAuthzSyslogErr, "Checking scitoken");

  SciToken scitoken;

  // Read in the entire scitoken into memory
  string token;
  const unsigned int N=1024;
  while (true) {
    vector<char> buf(N);
    size_t read = fread((void *)&buf[0], 1, N, fp_token);
    if (ferror(fp_token)) {
      LogAuthz(kLogAuthzDebug | kLogAuthzSyslog | kLogAuthzSyslogErr, "Error reading token file");
    }
    if (read) { token.append(buf.begin(), buf.end()); }
    if (read < N) { break; }
    // If the token is larger than 1MB, then stop reading in the token
    // Possible malicious user
    if ( token.size() > (1024 * 1024) ) {
      LogAuthz(kLogAuthzDebug | kLogAuthzSyslog | kLogAuthzSyslogErr, "SciToken larger than 1 MB");
      return kCheckTokenInvalid;
    }
  }

  // Loop through the membership, looking for "https://" issuers
  map<string, string> issuers;
  vector<string> issuers_vec;
{'fraction_non_alphanumeric': 0.12557644554806668, 'fraction_numerical': 0.013479957431713374, 'mean_word_length': 4.127272727272727, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6512588', 'n_tokens_mistral': 1026, 'n_tokens_neox': 977, 'n_words': 276}