@@ -27,15 +27,13 @@ type repoAccessCacheEntry struct {
2727 isPrivate bool
2828 knownUsers map [string ]bool // normalized login -> has push access
2929 viewerLogin string
30- viewerType string
3130}
3231
3332// RepoAccessInfo captures repository metadata needed for lockdown decisions.
3433type RepoAccessInfo struct {
3534 IsPrivate bool
3635 HasPushAccess bool
3736 ViewerLogin string
38- ViewerType string
3937}
4038
4139const (
@@ -89,10 +87,7 @@ func GetInstance(client *githubv4.Client, opts ...RepoAccessOption) *RepoAccessC
8987 cache : cache2go .Cache (defaultRepoAccessCacheKey ),
9088 ttl : defaultRepoAccessTTL ,
9189 trustedBotLogins : map [string ]struct {}{
92- "dependabot[bot]" : {},
93- "dependabot-preview[bot]" : {},
94- "github-actions[bot]" : {},
95- "github-copilot[bot]" : {},
90+ "copilot" : {},
9691 },
9792 }
9893 for _ , opt := range opts {
@@ -121,11 +116,13 @@ type CacheStats struct {
121116func (c * RepoAccessCache ) IsSafeContent (ctx context.Context , username , owner , repo string ) (bool , error ) {
122117 repoInfo , err := c .getRepoAccessInfo (ctx , username , owner , repo )
123118 if err != nil {
124- c .logDebug ("error checking repo access info for content filtering" , "owner" , owner , "repo" , repo , "user" , username , "error" , err )
125119 return false , err
126120 }
127121
128- if c .isTrustedBot (username , repoInfo .ViewerType ) || repoInfo .IsPrivate || repoInfo .ViewerLogin == strings .ToLower (username ) {
122+ c .logInfo (ctx , fmt .Sprintf ("evaluated repo access fur user %s to %s/%s for content filtering, result: hasPushAccess=%t, isPrivate=%t" ,
123+ username , owner , repo , repoInfo .HasPushAccess , repoInfo .IsPrivate ))
124+
125+ if c .isTrustedBot (username ) || repoInfo .IsPrivate || repoInfo .ViewerLogin == strings .ToLower (username ) {
129126 return true , nil
130127 }
131128 return repoInfo .HasPushAccess , nil
@@ -146,32 +143,34 @@ func (c *RepoAccessCache) getRepoAccessInfo(ctx context.Context, username, owner
146143 if err == nil {
147144 entry := cacheItem .Data ().(* repoAccessCacheEntry )
148145 if cachedHasPush , known := entry .knownUsers [userKey ]; known {
149- c .logDebug ("repo access cache hit" , "owner" , owner , "repo" , repo , "user" , username )
146+ c .logDebug (ctx , "repo access cache hit" )
150147 return RepoAccessInfo {
151148 IsPrivate : entry .isPrivate ,
152149 HasPushAccess : cachedHasPush ,
153150 ViewerLogin : entry .viewerLogin ,
154151 }, nil
155152 }
156- c .logDebug ("known users cache miss" , "owner" , owner , "repo" , repo , "user" , username )
153+
154+ c .logDebug (ctx , "known users cache miss" )
155+
157156 info , queryErr := c .queryRepoAccessInfo (ctx , username , owner , repo )
158157 if queryErr != nil {
159158 return RepoAccessInfo {}, queryErr
160159 }
160+
161161 entry .knownUsers [userKey ] = info .HasPushAccess
162162 entry .viewerLogin = info .ViewerLogin
163- entry .viewerType = info .ViewerType
164163 entry .isPrivate = info .IsPrivate
165164 c .cache .Add (key , c .ttl , entry )
165+
166166 return RepoAccessInfo {
167167 IsPrivate : entry .isPrivate ,
168168 HasPushAccess : entry .knownUsers [userKey ],
169169 ViewerLogin : entry .viewerLogin ,
170- ViewerType : entry .viewerType ,
171170 }, nil
172171 }
173172
174- c .logDebug ("repo access cache miss" , "owner" , owner , "repo" , repo , "user" , username )
173+ c .logDebug (ctx , "repo access cache miss" )
175174
176175 info , queryErr := c .queryRepoAccessInfo (ctx , username , owner , repo )
177176 if queryErr != nil {
@@ -183,15 +182,13 @@ func (c *RepoAccessCache) getRepoAccessInfo(ctx context.Context, username, owner
183182 knownUsers : map [string ]bool {userKey : info .HasPushAccess },
184183 isPrivate : info .IsPrivate ,
185184 viewerLogin : info .ViewerLogin ,
186- viewerType : info .ViewerType ,
187185 }
188186 c .cache .Add (key , c .ttl , entry )
189187
190188 return RepoAccessInfo {
191189 IsPrivate : entry .isPrivate ,
192190 HasPushAccess : entry .knownUsers [userKey ],
193191 ViewerLogin : entry .viewerLogin ,
194- ViewerType : entry .viewerType ,
195192 }, nil
196193}
197194
@@ -202,8 +199,7 @@ func (c *RepoAccessCache) queryRepoAccessInfo(ctx context.Context, username, own
202199
203200 var query struct {
204201 Viewer struct {
205- Typename string `graphql:"__typename"`
206- Login githubv4.String
202+ Login githubv4.String
207203 }
208204 Repository struct {
209205 IsPrivate githubv4.Boolean
@@ -242,20 +238,28 @@ func (c *RepoAccessCache) queryRepoAccessInfo(ctx context.Context, username, own
242238 IsPrivate : bool (query .Repository .IsPrivate ),
243239 HasPushAccess : hasPush ,
244240 ViewerLogin : string (query .Viewer .Login ),
245- ViewerType : query .Viewer .Typename ,
246241 }, nil
247242}
248243
249- func (c * RepoAccessCache ) logDebug (msg string , args ... any ) {
250- if c != nil && c .logger != nil {
251- c .logger .Debug (msg , args ... )
244+ func (c * RepoAccessCache ) log (ctx context.Context , level slog.Level , msg string , attrs ... slog.Attr ) {
245+ if c == nil || c .logger == nil {
246+ return
247+ }
248+ if ! c .logger .Enabled (ctx , level ) {
249+ return
252250 }
251+ c .logger .LogAttrs (ctx , level , msg , attrs ... )
253252}
254253
255- func (c * RepoAccessCache ) isTrustedBot (username string , viewerType string ) bool {
256- if viewerType != "Bot" {
257- return false
258- }
254+ func (c * RepoAccessCache ) logDebug (ctx context.Context , msg string , attrs ... slog.Attr ) {
255+ c .log (ctx , slog .LevelDebug , msg , attrs ... )
256+ }
257+
258+ func (c * RepoAccessCache ) logInfo (ctx context.Context , msg string , attrs ... slog.Attr ) {
259+ c .log (ctx , slog .LevelInfo , msg , attrs ... )
260+ }
261+
262+ func (c * RepoAccessCache ) isTrustedBot (username string ) bool {
259263 _ , ok := c .trustedBotLogins [strings .ToLower (username )]
260264 return ok
261265}
0 commit comments