WeKnora/internal/utils
wizardchen 8b00cb71a3 perf(storage): reuse a live resource access grant per resource
Resolving a resource:// handle into a public URL inserted one capability row
every time. Rendering an answer resolves the same image across many chunks and
re-reading a message history resolves it again on every call, so a read endpoint
behaved like a write-heavy one.

The table stores only sha256(token), so an existing grant cannot be reused by
looking it up — the plaintext token is unrecoverable. Derive the token instead:
HMAC-SHA256(SYSTEM_AES_KEY, resource id + time window). The same resource in the
same window derives the same token, so the row can be found by its hash and
reused; only the first request in a window inserts. The window is half the TTL,
so a reused grant always has at least TTL/2 of life left.

This keeps the storage properties intact: the table still holds only hashes and
the plaintext cannot be reconstructed from a dump without SYSTEM_AES_KEY, while
authorization stays entirely on the row, so expiry and revocation still decide
whether a token resolves. Without SYSTEM_AES_KEY, grants stay random and
per-request as before. A stable URL per window also lets client and CDN caches
hit.

Stop deleting revoked grants during cleanup. A revoked row has to survive until
its expiry, otherwise a token derived for the same resource and window would
re-create it and revive the access that was just revoked. grant.revoked_at has
no writer yet, so this changes nothing today, but the derived token makes the
tombstone load-bearing.

Co-authored-by: lyingbug <lyingbug@users.noreply.github.com>
2026-08-05 14:42:51 +08:00
..
crypto.go feat(crypto): AES-GCM at-rest for MCP and DataSource credentials with graceful key-rotation handling 2026-05-17 15:27:52 +08:00
crypto_test.go fix(crypto): fail loudly when encrypted DB fields cannot be decrypted 2026-04-30 15:17:56 +08:00
debug.go feat: 使用增强的任务ID生成器替换UUID 2026-01-14 17:09:19 +08:00
extraheaders.go fix(embedding): support native Gemini embeddings 2026-06-02 21:37:58 +08:00
extraheaders_test.go fix(embedding): support native Gemini embeddings 2026-06-02 21:37:58 +08:00
filesize.go feat(system): consolidate system admin and settings into one Settings panel 2026-05-26 21:13:56 +08:00
fileutil.go fix(security): harden outbound requests and file previews 2026-07-02 19:15:46 +08:00
httputil.go feat: enhance document loading and state management in doc-content component 2026-03-02 21:21:49 +08:00
inject.go fix(agent): exclude disabled FAQ entries 2026-07-29 14:40:54 +08:00
inject_test.go fix(agent): exclude disabled FAQ entries 2026-07-29 14:40:54 +08:00
json.go feat: Add DataSchema tool for retrieving schema information from CSV and Excel files 2025-12-29 20:03:51 +08:00
log_sanitize.go feat: implement image upload and multimodal support 2026-03-12 10:32:26 +08:00
oidc_state.go fix(security): close six open GHSA findings from security triage 2026-07-02 15:03:53 +08:00
oidc_state_test.go fix(security): close six open GHSA findings from security triage 2026-07-02 15:03:53 +08:00
presign.go perf(storage): reuse a live resource access grant per resource 2026-08-05 14:42:51 +08:00
presign_test.go feat(storage): improve storage connectivity error handling and multi-instance support 2026-07-15 14:54:39 +08:00
security.go refactor(http): share SSRF-safe transport across model clients 2026-07-23 12:00:05 +08:00
security_redirect_test.go fix(security): close six open GHSA findings from security triage 2026-07-02 15:03:53 +08:00
security_test.go feat(retriever): add OpenSearch driver skeleton + interface stubs (PR 2a of 3) 2026-05-26 20:54:58 +08:00
security_transport_test.go refactor(http): share SSRF-safe transport across model clients 2026-07-23 12:00:05 +08:00
storage_error.go feat(storage): improve storage connectivity error handling and multi-instance support 2026-07-15 14:54:39 +08:00
taskid.go fix(security): enforce KB access on wiki reads and related IDOR paths 2026-07-02 12:06:22 +08:00
taskid_test.go fix(security): enforce KB access on wiki reads and related IDOR paths 2026-07-02 12:06:22 +08:00