Improve handling of secret text

This commit is contained in:
Bruce Hill 2024-03-09 18:47:56 -05:00
parent 77c9669d41
commit 5131fdff62
5 changed files with 17 additions and 6 deletions

View File

@ -25,7 +25,7 @@
public CORD Text__as_text(const void *text, bool colorize, const TypeInfo *info)
{
if (!text) return info->TextInfo.lang;
CORD ret = Text__quoted(*(CORD*)text, colorize);
CORD ret = info->TextInfo.secret ? "(*****)" : Text__quoted(*(CORD*)text, colorize);
if (!streq(info->TextInfo.lang, "Text"))
ret = colorize ? CORD_all("\x1b[1m$", info->TextInfo.lang, "\x1b[m", ret) : CORD_all("$", info->TextInfo.lang, ret);
return ret;

View File

@ -29,6 +29,7 @@ typedef struct TypeInfo {
} PointerInfo;
struct {
const char *lang;
bool secret;
} TextInfo;
struct {
const struct TypeInfo *item;

View File

@ -582,7 +582,12 @@ CORD compile(env_t *env, ast_t *ast)
}
case TextJoin: {
const char *lang = Match(ast, TextJoin)->lang;
type_t *text_t = Type(TextType, .lang=lang);
type_t *text_t = Table_str_get(*env->types, lang ? lang : "Text");
if (!text_t || text_t->tag != TextType)
code_err(ast, "%s is not a valid text language name", lang);
if (Match(text_t, TextType)->secret)
code_err(ast, "%s text is marked secret, so you cannot use %s literals in code. Please load the content from a secure location instead",
lang, lang);
table_t *lang_ns = lang ? Table_str_get(*env->type_namespaces, lang) : NULL;
ast_list_t *chunks = Match(ast, TextJoin)->children;
if (!chunks) {
@ -604,7 +609,8 @@ CORD compile(env_t *env, ast_t *ast)
for (int64_t i = 1; i <= Table_length(*lang_ns); i++) {
struct {const char *name; binding_t *b; } *entry = Table_entry(*lang_ns, i);
if (entry->b->type->tag != FunctionType) continue;
if (strncmp(entry->name, "escape_", strlen("escape_")) != 0) continue;
if (!(streq(entry->name, "escape") || strncmp(entry->name, "escape_", strlen("escape_")) == 0))
continue;
auto fn = Match(entry->b->type, FunctionType);
if (!fn->args || fn->args->next) continue;
if (fn->ret->tag != TextType || !streq(Match(fn->ret, TextType)->lang, lang))
@ -1237,9 +1243,9 @@ CORD compile(env_t *env, ast_t *ast)
auto def = Match(ast, LangDef);
CORD_appendf(&env->code->typedefs, "typedef CORD %s_t;\n", def->name);
CORD_appendf(&env->code->typedefs, "extern const TypeInfo %s;\n", def->name);
CORD_appendf(&env->code->typeinfos, "public const TypeInfo %s = {%zu, %zu, {.tag=TextInfo, .TextInfo={%s}}};\n",
CORD_appendf(&env->code->typeinfos, "public const TypeInfo %s = {%zu, %zu, {.tag=TextInfo, .TextInfo={%s, .secret=%s}}};\n",
def->name, sizeof(CORD), __alignof__(CORD),
Text__quoted(def->name, false), "}}};\n");
Text__quoted(def->name, false), def->secret ? "yes" : "no");
compile_namespace(env, def->name, def->namespace);
return CORD_EMPTY;
}

View File

@ -182,13 +182,16 @@ void bind_statement(env_t *env, ast_t *statement)
case LangDef: {
auto def = Match(statement, LangDef);
type_t *type = Type(TextType, .lang=def->name);
type_t *type = Type(TextType, .lang=def->name, .secret=def->secret);
Table_str_set(env->types, def->name, type);
env_t *ns_env = namespace_env(env, def->name);
set_binding(ns_env, "from_unsafe_text",
new(binding_t, .type=Type(FunctionType, .args=new(arg_t, .name="text", .type=Type(TextType)), .ret=type),
.code=CORD_all("(", def->name, "_t)")));
set_binding(ns_env, "text_content",
new(binding_t, .type=Type(FunctionType, .args=new(arg_t, .name="text", .type=Type(TextType)), .ret=type),
.code="(Text_t)"));
for (ast_list_t *stmt = def->namespace ? Match(def->namespace, Block)->statements : NULL; stmt; stmt = stmt->next)
bind_statement(ns_env, stmt->ast);

View File

@ -65,6 +65,7 @@ struct type_s {
} NumType;
struct {
const char *lang;
bool secret;
} TextType;
struct {
type_t *item_type;