rafmacalaba commited on
Commit
0880d65
·
1 Parent(s): b5036c8

fix: block document access for users not in annotator_config

Browse files

Previously getUserAssignedDocs returned null when user wasn't found,
which the caller treated as 'no filter, show all docs'. Now returns
empty {} which correctly results in zero visible documents.

Files changed (1) hide show
  1. app/api/documents/route.js +7 -6
app/api/documents/route.js CHANGED
@@ -3,8 +3,9 @@ import yaml from 'js-yaml';
3
 
4
  /**
5
  * Fetch annotator_config.yaml and return the doc list for a given user.
6
- * Returns null if no config or user not found (show all docs).
7
- * Now returns per-corpus assignments: { wbg: Set([1,2]), unhcr: Set([3,4]) }
 
8
  */
9
  async function getUserAssignedDocs(username) {
10
  if (!username) return null;
@@ -15,13 +16,13 @@ async function getUserAssignedDocs(username) {
15
  headers: { 'Authorization': `Bearer ${process.env.HF_TOKEN}` },
16
  cache: 'no-store'
17
  });
18
- if (!res.ok) return null;
19
 
20
  const text = await res.text();
21
  const config = yaml.load(text);
22
 
23
  const annotator = (config.annotators || []).find(a => a.username === username);
24
- if (!annotator || !annotator.docs) return null;
25
 
26
  // Support both old format (flat array) and new format (per-corpus object)
27
  if (Array.isArray(annotator.docs)) {
@@ -36,10 +37,10 @@ async function getUserAssignedDocs(username) {
36
  result[corpusId] = new Set(docList);
37
  }
38
  }
39
- return Object.keys(result).length > 0 ? result : null;
40
  } catch (e) {
41
  console.warn('Could not load annotator_config.yaml:', e.message);
42
- return null;
43
  }
44
  }
45
 
 
3
 
4
  /**
5
  * Fetch annotator_config.yaml and return the doc list for a given user.
6
+ * Returns null only if no username is provided (unauthenticated).
7
+ * Returns empty {} if user is not in config (sees no docs).
8
+ * Returns per-corpus assignments: { wbg: Set([1,2]), unhcr: Set([3,4]) }
9
  */
10
  async function getUserAssignedDocs(username) {
11
  if (!username) return null;
 
16
  headers: { 'Authorization': `Bearer ${process.env.HF_TOKEN}` },
17
  cache: 'no-store'
18
  });
19
+ if (!res.ok) return {}; // config missing — block access
20
 
21
  const text = await res.text();
22
  const config = yaml.load(text);
23
 
24
  const annotator = (config.annotators || []).find(a => a.username === username);
25
+ if (!annotator || !annotator.docs) return {}; // user not in config — no docs
26
 
27
  // Support both old format (flat array) and new format (per-corpus object)
28
  if (Array.isArray(annotator.docs)) {
 
37
  result[corpusId] = new Set(docList);
38
  }
39
  }
40
+ return result; // may be empty {} if user has no corpus assignments
41
  } catch (e) {
42
  console.warn('Could not load annotator_config.yaml:', e.message);
43
+ return {}; // on error, block access rather than show all
44
  }
45
  }
46