@squidcloud/backend 1.0.492 → 1.0.494

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{createPrivateKey as t,randomBytes as e,sign as r}from"node:crypto";import{spawn as o}from"node:child_process";import{chmod as n,copyFile as i,lstat as s,mkdir as a,mkdtemp as I,rm as c,writeFile as _}from"node:fs/promises";import{isAbsolute as u,join as E,posix as l,relative as T,sep as p}from"node:path";import{tmpdir as S}from"node:os";let d=t=>new Error(t);function A(t,e,...r){t||function(t,...e){const r=function(t){return void 0===t?"":"string"==typeof t?t:t()}(t);if("object"==typeof r)throw r;throw d(r||"Assertion error",...e)}(e,...r)}class N extends Error{constructor(t,e){super(e),this.classification=t,this.name="SourceProviderError"}}const O="https://api.bitbucket.org",R="bitbucket.org";class f{constructor(t,e=fetch){this.fetchFn=e,A(t.email.trim()===t.email&&t.email.length>0,"BITBUCKET_EMAIL_INVALID"),A(t.apiToken.trim()===t.apiToken&&t.apiToken.length>0,"BITBUCKET_API_TOKEN_INVALID"),this.authorization=`Basic ${Buffer.from(`${t.email}:${t.apiToken}`).toString("base64")}`}async listRepositories(t,e){w(e),B(t,"BITBUCKET_WORKSPACE_INVALID");const r=[],o=new Set,n=new Set,i=new Set,s=`/2.0/repositories/${t}`;let a=`${O}${s}?pagelen=100`,I=0;for(;a;){A(I<100,"BITBUCKET_PAGINATION_LIMIT_EXCEEDED"),I+=1,A(!i.has(a),"BITBUCKET_PAGINATION_CYCLE"),i.add(a);const c=await this.request(a,e);w(e),A(Array.isArray(c.values),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");for(const e of c.values){const i=m(e,t),s=i.providerRepositoryId.toLowerCase(),a=i.fullName.toLowerCase();A(!o.has(s)&&!n.has(a),"BITBUCKET_REPOSITORIES_MUST_BE_UNIQUE"),o.add(s),n.add(a),r.push(i)}a=h(c.next,s)}return w(e),r}async getRepository(t,e,r){w(r),B(t,"BITBUCKET_WORKSPACE_INVALID"),B(e,"BITBUCKET_REPOSITORY_SLUG_INVALID");const o=await this.request(`${O}/2.0/repositories/${encodeURIComponent(t)}/${encodeURIComponent(e)}`,r);w(r);const n=m(o,t);return A(n.fullName.toLowerCase()===`${t}/${e}`.toLowerCase(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),n}async request(t,e){let r;try{r=await this.fetchFn(t,{headers:{Accept:"application/json",Authorization:this.authorization},redirect:"manual",signal:e})}catch(t){if(e?.aborted||C(t))throw new N("aborted","BITBUCKET_REQUEST_ABORTED");throw new N("transient","BITBUCKET_PROVIDER_UNAVAILABLE")}if(r.redirected||r.status>=300&&r.status<400)throw new Error("BITBUCKET_REDIRECT_REJECTED");if(!r.ok)throw 401===(o=r.status)?new N("authentication","BITBUCKET_AUTHENTICATION_FAILED"):403===o?new N("authentication","BITBUCKET_PERMISSION_DENIED"):429===o?new N("transient","BITBUCKET_RATE_LIMITED"):o>=500?new N("transient","BITBUCKET_PROVIDER_UNAVAILABLE"):new N("repository-unavailable",404===o?"BITBUCKET_NOT_FOUND":"BITBUCKET_REQUEST_FAILED");var o;try{const t=await r.json();return w(e),A(!!t&&"object"==typeof t&&!Array.isArray(t),"BITBUCKET_RESPONSE_INVALID"),t}catch(t){if(e?.aborted||C(t))throw new N("aborted","BITBUCKET_REQUEST_ABORTED");if(t instanceof Error&&t.message.startsWith("BITBUCKET_"))throw t;throw new Error("BITBUCKET_RESPONSE_INVALID")}}}function m(t,e){A(!!t&&"object"==typeof t&&!Array.isArray(t),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const r=t;A("string"==typeof r.uuid&&r.uuid.trim()===r.uuid&&r.uuid.length>0,"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof r.full_name&&/^[^/\s]+\/[^/\s]+$/.test(r.full_name)&&r.full_name.split("/")[0]?.toLowerCase()===e.toLowerCase(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const o=L(r.links,"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),n=o.clone;A(Array.isArray(n),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const i=n.filter(t=>"https"===function(t){return t&&"object"==typeof t&&!Array.isArray(t)?t:void 0}(t)?.name);A(1===i.length,"BITBUCKET_REPOSITORY_URL_INVALID");const s=U(L(i[0],"BITBUCKET_REPOSITORY_URL_INVALID").href,r.full_name,!0),a=U(L(o.html,"BITBUCKET_REPOSITORY_URL_INVALID").href,r.full_name,!1),I=r.mainbranch,c=null===I?"":function(t){return A("string"==typeof t&&t.trim(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),t}(L(I,"BITBUCKET_REPOSITORY_RESPONSE_INVALID").name);return{cloneUrl:s,defaultBranch:c,fullName:r.full_name,provider:"bitbucket",providerRepositoryId:r.uuid,webUrl:a}}function h(t,e){if(null==t)return;let r;A("string"==typeof t&&t,"BITBUCKET_PAGINATION_URL_INVALID");try{r=new URL(t)}catch{throw new Error("BITBUCKET_PAGINATION_URL_INVALID")}const o=/^https:\/\/(?:[^/@]+@)?api\.bitbucket\.org:\d+(?:\/|$)/i.test(t);return A(!("https:"!==r.protocol||"api.bitbucket.org"!==r.hostname||o||r.username||r.password||r.hash||r.pathname!==e),"BITBUCKET_PAGINATION_URL_INVALID"),r.toString()}function U(t,e,r){if("string"!=typeof t)throw new Error("BITBUCKET_REPOSITORY_URL_INVALID");let o;try{o=new URL(t)}catch{throw new Error("BITBUCKET_REPOSITORY_URL_INVALID")}const n=`/${e}${r?".git":""}`,i=/^https:\/\/(?:[^/@]+@)?bitbucket\.org:\d+(?:\/|$)/i.test(t);return A("https:"===o.protocol&&o.hostname===R&&!i&&(r||!o.username)&&!o.password&&!o.search&&!o.hash&&o.pathname===n,"BITBUCKET_REPOSITORY_URL_INVALID"),`https://${R}${n}`}function L(t,e){return A(!!t&&"object"==typeof t&&!Array.isArray(t),e),t}function B(t,e){A("string"==typeof t&&t.trim()===t&&/^[A-Za-z0-9_-]+$/.test(t),e)}function w(t){if(t?.aborted)throw new N("aborted","BITBUCKET_REQUEST_ABORTED")}function C(t){return t instanceof Error&&"AbortError"===t.name}const y="https://api.github.com",D=3e5,G="2022-11-28";class P{constructor(e,r=fetch){this.fetchFn=r,this.tokenCache=new Map,g(e.appId),g(e.installationId),this.appId=e.appId,this.installationId=e.installationId,this.privateKey=function(e){A("string"==typeof e&&e.trim(),"GITHUB_PRIVATE_KEY_INVALID");const r=e.replace(/\\r\\n/g,"\n").replace(/\\n/g,"\n").replace(/\r\n/g,"\n").trim(),o=r.includes("\n")?r:function(t){const e=/^-----BEGIN (RSA )?PRIVATE KEY-----(.*?)-----END (RSA )?PRIVATE KEY-----$/s.exec(t);if(!e)return t;const r=e[1]?"RSA PRIVATE KEY":"PRIVATE KEY";return`-----BEGIN ${r}-----\n${(e[2].replace(/\s/g,"").match(/.{1,64}/g)??[]).join("\n")}\n-----END ${r}-----`}(r);try{t(o)}catch{throw new Error("GITHUB_PRIVATE_KEY_INVALID")}return o}(e.privateKey)}async createInstallationToken(t,e){K(e);const r=function(t){const e=t.repositoryIds?[...t.repositoryIds].sort((t,e)=>t-e):void 0;A(!e||e.every(t=>Number.isSafeInteger(t)&&t>0),"GITHUB_REPOSITORY_IDS_INVALID");const r=t.permissions?Object.fromEntries(Object.entries(t.permissions).sort(([t],[e])=>t.localeCompare(e)).map(([t,e])=>(A("read"===e||"write"===e,"GITHUB_PERMISSIONS_INVALID"),[t,e]))):void 0;return{...e?{repository_ids:e}:{},...r?{permissions:r}:{}}}(t),o=JSON.stringify(r),n=this.tokenCache.get(o);if(n&&Date.parse(n.expiresAt)-Date.now()>D)return n;const i=await V(this.fetchFn,`${y}/app/installations/${this.installationId}/access_tokens`,{body:JSON.stringify(r),headers:{Accept:"application/vnd.github+json",Authorization:`Bearer ${this.createAppJwt()}`,"Content-Type":"application/json","X-GitHub-Api-Version":G},method:"POST",redirect:"manual",signal:e}),s=await b(i,e);K(e);const a=$(s,"token"),I=$(s,"expires_at");A(Number.isFinite(Date.parse(I)),"GITHUB_TOKEN_RESPONSE_INVALID");const c=function(t){const e=t.permissions;A(null!==e&&"object"==typeof e&&!Array.isArray(e),"GITHUB_TOKEN_RESPONSE_INVALID");const r={};for(const[t,o]of Object.entries(e))A(/^[a-z][a-z0-9_]{0,127}$/.test(t)&&("read"===o||"write"===o),"GITHUB_TOKEN_RESPONSE_INVALID"),r[t]=o;return Object.fromEntries(Object.entries(r).sort(([t],[e])=>t.localeCompare(e)))}(s),_=function(t){const e=t.repository_selection;return A("all"===e||"selected"===e,"GITHUB_TOKEN_RESPONSE_INVALID"),e}(s),u=function(t){const e=t.repositories;if(void 0===e)return[];A(Array.isArray(e),"GITHUB_TOKEN_RESPONSE_INVALID");const r=e.map(t=>(A(null!==t&&"object"==typeof t&&!Array.isArray(t)&&"id"in t&&Number.isSafeInteger(t.id)&&t.id>0,"GITHUB_TOKEN_RESPONSE_INVALID"),t.id));return A(new Set(r).size===r.length,"GITHUB_TOKEN_RESPONSE_INVALID"),r.sort((t,e)=>t-e)}(s),E={expiresAt:I,permissions:c,repositoryIds:u,repositorySelection:_,token:a};return function(t,e){e.permissions&&A(JSON.stringify(t.permissions)===JSON.stringify(e.permissions),"GITHUB_TOKEN_SCOPE_INVALID"),e.repository_ids&&A("selected"===t.repositorySelection&&t.repositoryIds.length===e.repository_ids.length&&t.repositoryIds.every((t,r)=>t===e.repository_ids?.[r]),"GITHUB_TOKEN_SCOPE_INVALID")}(E,r),Date.parse(I)-Date.now()>D&&this.tokenCache.set(o,E),E}async listInstallationRepositories(t){K(t);const{token:e}=await this.createInstallationToken({},t),r=[],o=new Set,n=new Set,i=new Set;let s=`${y}/installation/repositories?per_page=100`,a=0;for(;s;){A(a<100,"GITHUB_PAGINATION_LIMIT_EXCEEDED"),a+=1,A(!i.has(s),"GITHUB_PAGINATION_CYCLE"),i.add(s);const I=await V(this.fetchFn,s,{headers:{Accept:"application/vnd.github+json",Authorization:`Bearer ${e}`,"X-GitHub-Api-Version":G},redirect:"manual",signal:t}),c=await b(I,t);K(t);const _=c.repositories;A(Array.isArray(_),"GITHUB_REPOSITORY_RESPONSE_INVALID");for(const t of _.map(H)){A(!o.has(t.providerRepositoryId),"GITHUB_REPOSITORIES_MUST_BE_UNIQUE"),o.add(t.providerRepositoryId);const e=t.fullName.toLowerCase();A(!n.has(e),"GITHUB_REPOSITORIES_MUST_BE_UNIQUE"),n.add(e),r.push(t)}s=x(I.headers.get("link"))}return K(t),r}createAppJwt(){const t=Math.floor(Date.now()/1e3),e=`${M({alg:"RS256",typ:"JWT"})}.${M({exp:t+600,iat:t-60,iss:this.appId})}`;return`${e}.${r("RSA-SHA256",Buffer.from(e),this.privateKey).toString("base64url")}`}}function g(t){A("string"==typeof t&&/^\d+$/.test(t),"GITHUB_IDENTIFIER_INVALID")}async function b(t,e){if(t.redirected||t.status>=300&&t.status<400)throw new Error("GITHUB_REDIRECT_REJECTED");if(!t.ok)throw function(t){const{status:e}=t;return 401===e?new N("authentication","GITHUB_AUTHENTICATION_FAILED"):403===e?t.headers.has("retry-after")||"0"===t.headers.get("x-ratelimit-remaining")?new N("transient","GITHUB_RATE_LIMITED"):new N("authentication","GITHUB_PERMISSION_DENIED"):429===e?new N("transient","GITHUB_RATE_LIMITED"):e>=500?new N("transient","GITHUB_PROVIDER_UNAVAILABLE"):new N("repository-unavailable","GITHUB_REQUEST_FAILED")}(t);try{const e=await t.json();return A(!!e&&"object"==typeof e&&!Array.isArray(e),"GITHUB_RESPONSE_INVALID"),e}catch(t){if(e?.aborted||v(t))throw new N("aborted","GITHUB_REQUEST_ABORTED");if(t instanceof Error&&t.message.startsWith("GITHUB_"))throw t;throw new Error("GITHUB_RESPONSE_INVALID")}}async function V(t,e,r){try{return await t(e,r)}catch(t){if(r.signal?.aborted||v(t))throw new N("aborted","GITHUB_REQUEST_ABORTED");throw new N("transient","GITHUB_PROVIDER_UNAVAILABLE")}}function K(t){if(t?.aborted)throw new N("aborted","GITHUB_REQUEST_ABORTED")}function v(t){return t instanceof Error&&"AbortError"===t.name}function H(t){A(!!t&&"object"==typeof t&&!Array.isArray(t),"GITHUB_REPOSITORY_RESPONSE_INVALID");const e=t,r=e.id,o=e.full_name,n=e.clone_url,i=e.html_url,s=e.default_branch;return A(Number.isSafeInteger(r)&&r>0,"GITHUB_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof o&&/^[^/\s]+\/[^/\s]+$/.test(o),"GITHUB_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof s&&s.trim(),"GITHUB_REPOSITORY_RESPONSE_INVALID"),F(n,o,!0),F(i,o,!1),{cloneUrl:n,defaultBranch:s,fullName:o,provider:"github",providerRepositoryId:String(r),webUrl:i}}function F(t,e,r){if("string"!=typeof t)throw new Error("GITHUB_REPOSITORY_URL_INVALID");try{const o=new URL(t),n=`/${e}${r?".git":""}`;A(!("https:"!==o.protocol||"github.com"!==o.hostname||o.port||o.username||o.password||o.search||o.hash||o.pathname!==n),"GITHUB_REPOSITORY_URL_INVALID")}catch(t){if(t instanceof Error&&"GITHUB_REPOSITORY_URL_INVALID"===t.message)throw t;throw new Error("GITHUB_REPOSITORY_URL_INVALID")}}function x(t){if(!t)return;const e=t.split(",").map(t=>t.trim()).find(t=>/;\s*rel="?next"?/i.test(t));if(!e)return;const r=/^<([^>]+)>/.exec(e)?.[1];let o;A(r,"GITHUB_PAGINATION_URL_INVALID");try{o=new URL(r)}catch{throw new Error("GITHUB_PAGINATION_URL_INVALID")}return A(!("https:"!==o.protocol||"api.github.com"!==o.hostname||o.username||o.password||o.port||o.hash||"/installation/repositories"!==o.pathname),"GITHUB_PAGINATION_URL_INVALID"),o.toString()}function $(t,e){const r=t[e];return A("string"==typeof r&&r,"GITHUB_TOKEN_RESPONSE_INVALID"),r}function M(t){return Buffer.from(JSON.stringify(t)).toString("base64url")}function k(t){return t.isFile()&&0===t.uid&&0===t.gid&&!!(73&t.mode)&&!(3584&t.mode)&&!(18&t.mode)}const Y=16777216,j="/usr/local/bin/nsjail",Q="/opt/source-agent-rootfs",z="/workspace",W="/usr/local/bin/squid-source-git-askpass",J="/proc",q=["COMSPEC","HOME","PATH","SSL_CERT_DIR","SSL_CERT_FILE","SystemRoot","WINDIR"],X=["GIT_ASKPASS","GIT_SSL_CAINFO","SQUID_GIT_PASSWORD","SQUID_GIT_USERNAME"],Z=new Set(["GIT_ASKPASS","GIT_CONFIG_GLOBAL","GIT_CONFIG_NOSYSTEM","GIT_LFS_SKIP_SMUDGE","GIT_LITERAL_PATHSPECS","GIT_NO_REPLACE_OBJECTS","GIT_OPTIONAL_LOCKS","GIT_SSL_CAINFO","GIT_TERMINAL_PROMPT","HOME","LANG","LC_ALL","PATH","SQUID_GIT_PASSWORD","SQUID_GIT_USERNAME","SSL_CERT_DIR","SSL_CERT_FILE","TMPDIR"]);class tt extends Error{constructor(t){super(`SOURCE_GIT_${t.toUpperCase().replaceAll("-","_")}`),this.kind=t,this.name="SourceGitCommandError"}}class et{constructor(t,e=function(){return"win32"===process.platform?"git.exe":"/usr/bin/git"}(),r=Tt()){this.runtimeRootDirectory=t,this.executable=e,this.fileLimitExecutable=r,A("win32"===process.platform||u(e),"SOURCE_CONFIGURATION_INVALID"),A(void 0===r||u(r),"SOURCE_CONFIGURATION_INVALID")}async run(t,e){if(A(t.length>0&&t.every(lt),"SOURCE_CONFIGURATION_INVALID"),A(Number.isSafeInteger(e.timeoutMs)&&e.timeoutMs>0,"SOURCE_CONFIGURATION_INVALID"),A(void 0===e.maxFileBytes||Number.isSafeInteger(e.maxFileBytes)&&e.maxFileBytes>0,"SOURCE_CONFIGURATION_INVALID"),A(void 0===e.maxOutputBytes||Number.isSafeInteger(e.maxOutputBytes)&&e.maxOutputBytes>0,"SOURCE_CONFIGURATION_INVALID"),e.signal?.aborted)throw new tt("aborted");let r;try{const c="linux"===process.platform?e.sandboxRootDirectory:void 0;c?(It(c),await Promise.all([it(j),it(`${Q}${this.executable}`),it(`${Q}${W}`),...this.fileLimitExecutable?[it(`${Q}${this.fileLimitExecutable}`)]:[]]),r=await I(E(c,".squid-trusted-git-"))):(await a(this.runtimeRootDirectory,{mode:448,recursive:!0}),r=await I(E(this.runtimeRootDirectory,"disabled-git-hooks-"))),A(r,"SOURCE_CONFIGURATION_INVALID");const _=c?E(r,"hooks"):r;if(c&&await a(_,{mode:448}),await n(_,448),e.signal?.aborted)throw new tt("aborted");const S=ct(e.environment);if(c){const a=T(c,r);A(a.length>0&&!a.includes(p)&&".."!==a,"SOURCE_CONFIGURATION_INVALID");const I=l.join(z,a);S.GIT_ASKPASS=W,S.HOME="/home/source-agent",S.PATH="/usr/local/bin:/usr/bin:/bin",S.SSL_CERT_DIR="/etc/ssl/certs",S.SSL_CERT_FILE="/etc/ssl/certs/ca-certificates.crt",S.TMPDIR="/tmp",delete S.COMSPEC,delete S.SystemRoot,delete S.WINDIR,S.GIT_SSL_CAINFO&&(S.GIT_SSL_CAINFO=await async function(t,e,r){A(u(t)&&!t.includes("\0"),"SOURCE_CONFIGURATION_INVALID");const o=await s(t);A(o.isFile()&&o.size>0&&o.size<=16777216,"SOURCE_CONFIGURATION_INVALID");const a="ca.pem",I=E(e,a);return await i(t,I),await n(I,256),l.join(r,a)}(S.GIT_SSL_CAINFO,r,I));const _=t.map(t=>ot(t,c)),d=e.cwd?function(t,e){A(u(t),"SOURCE_CONFIGURATION_INVALID");const r=ot(t,e);return A(nt(r),"SOURCE_CONFIGURATION_INVALID"),r}(e.cwd,c):z,N=rt({environment:S,invocation:_t(this.executable,[...this.fixedArguments(e.mode,l.join(I,"hooks")),..._],e.maxFileBytes,process.platform,this.fileLimitExecutable),maxFileBytes:e.maxFileBytes??10737418240,mode:e.mode,tenantGid:st(process.getgid?.()),tenantUid:st(process.getuid?.()),timeoutMs:e.timeoutMs,workingDirectory:d,workspaceSource:c}),O=o(j,N,{detached:!0,env:S,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await ut(O,e.input,e.maxOutputBytes??Y,e.signal,e.timeoutMs)}const d=_t(this.executable,[...this.fixedArguments(e.mode,_),...t],e.maxFileBytes,process.platform,this.fileLimitExecutable),N=o(d.executable,d.arguments,{cwd:e.cwd,detached:"win32"!==process.platform,env:S,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await ut(N,e.input,e.maxOutputBytes??Y,e.signal,e.timeoutMs)}finally{r&&await c(r,{force:!0,recursive:!0})}}fixedArguments(t,e){return["--no-optional-locks","-c","advice.detachedHead=false","-c","core.longpaths=true","-c",`core.hooksPath=${e}`,"-c","credential.helper=","-c","http.followRedirects=false","-c","core.sshCommand=false","-c","protocol.ext.allow=never","-c","protocol.file.allow="+("local"===t?"always":"never"),"-c","protocol.ssh.allow=never","-c","protocol.git.allow=never","-c","protocol.http.allow=never","-c","protocol.https.allow=always"]}}function rt(t){A(Number.isSafeInteger(t.timeoutMs)&&t.timeoutMs>0,"SOURCE_CONFIGURATION_INVALID"),A(Number.isSafeInteger(t.maxFileBytes)&&t.maxFileBytes>0,"SOURCE_CONFIGURATION_INVALID"),It(t.workspaceSource),at(t.tenantUid),at(t.tenantGid);const e=Math.max(1,Math.ceil(t.timeoutMs/1e3)),r=Math.max(1,Math.ceil(t.maxFileBytes/1048576)),o=t.workingDirectory??z;A(nt(o),"SOURCE_CONFIGURATION_INVALID");const n=["--mode","o",..."remote"===t.mode?["--disable_clone_newnet"]:[],"--chroot",Q,"--cwd",o,"--user",`65532:${t.tenantUid}:1`,"--group",`65532:${t.tenantGid}:1`,"--hostname","squid-trusted-git","--time_limit",String(e),"--rlimit_as","4096","--rlimit_cpu",String(e),"--rlimit_fsize",String(r),"--rlimit_nofile","256","--rlimit_nproc","128","--max_cpus","2","--bindmount",`${t.workspaceSource}:${z}`,"--mount","none:/home/source-agent:tmpfs:size=67108864,nr_inodes=16384,mode=0700,uid=65532,gid=65532","--mount","none:/tmp:tmpfs:size=67108864,nr_inodes=16384,mode=0700,uid=65532,gid=65532","--bindmount","/dev/null:/dev/null","--bindmount_ro","/dev/urandom:/dev/urandom","--disable_proc","--bindmount_ro",`${J}:${J}`];for(const e of Object.keys(t.environment).sort())A(Z.has(e),"SOURCE_CONFIGURATION_INVALID"),n.push("--env",e);return n.push("--",t.invocation.executable,...t.invocation.arguments),n}function ot(t,e){const r=T(e,t);return""===r?z:function(t){return".."!==t&&!t.startsWith(`..${p}`)&&!u(t)}(r)?l.join(z,...r.split(p)):t}function nt(t){return t===l.normalize(t)&&(t===z||t.startsWith(`${z}/`))}async function it(t){A(k(await s(t)),"SOURCE_SANDBOX_UNAVAILABLE")}function st(t){return A(void 0!==t,"SOURCE_SANDBOX_UNAVAILABLE"),at(t),t}function at(t){A(Number.isSafeInteger(t)&&t>=0,"SOURCE_SANDBOX_UNAVAILABLE")}function It(t){A(u(t)&&!t.includes("\0")&&!t.includes(":")&&!/[\r\n]/.test(t),"SOURCE_CONFIGURATION_INVALID")}function ct(t,e=process.env,r=process.platform){const o={GIT_CONFIG_GLOBAL:"win32"===r?"NUL":"/dev/null",GIT_CONFIG_NOSYSTEM:"1",GIT_LFS_SKIP_SMUDGE:"1",GIT_LITERAL_PATHSPECS:"1",GIT_NO_REPLACE_OBJECTS:"1",GIT_OPTIONAL_LOCKS:"0",GIT_TERMINAL_PROMPT:"0",LANG:"C",LC_ALL:"C",PATH:e.PATH};for(const r of q){const n=t?.[r]??e[r];void 0!==n&&(o[r]=n)}for(const e of X){const r=t?.[e];void 0!==r&&(o[e]=r)}return o}function _t(t,e,r,o=process.platform,n=Tt(o)){return"linux"!==o||void 0===r?{arguments:e,executable:t}:(A(Number.isSafeInteger(r)&&r>0&&void 0!==n&&u(n),"SOURCE_CONFIGURATION_INVALID"),{arguments:[`--fsize=${r}`,"--",t,...e],executable:n})}async function ut(t,e,r,o,n){A(Number.isSafeInteger(r)&&r>0,"SOURCE_CONFIGURATION_INVALID");const i=[],s=[];let a=0,I=!1,c=!1,_=!1;const u=(e,o)=>{if(a+=o.length,a>r)return c=!0,void Et(t);e.push(o)};t.stdout?.on("data",t=>u(i,t)),t.stderr?.on("data",t=>u(s,t));const E=()=>{I=!0,Et(t)};o?.addEventListener("abort",E,{once:!0}),o?.aborted&&E();const l=setTimeout(()=>{_=!0,Et(t)},n);l.unref?.(),e?t.stdin?.end(e):t.stdin?.end();try{const{code:e,error:r,signal:o}=await function(t){return new Promise(e=>{let r;t.once("error",t=>{r=t}),t.once("close",(t,o)=>{e({code:t,...r?{error:r}:{},signal:o})})})}(t);if(c)throw new tt("resource-limit");if(I)throw new tt("aborted");if(_)throw new tt("transient");if(r)throw new tt("transient");if(0===e)return{stdout:Buffer.concat(i)};if("SIGXFSZ"===o)throw new tt("resource-limit");if("SIGKILL"===o)throw new tt("transient");throw new tt(function(t){const e=t.toLowerCase();return e.includes("file too large")||e.includes("disk quota exceeded")||e.includes("no space left")||e.includes("resource limit")?"resource-limit":/\b429\b/.test(e)||/\b5\d\d\b/.test(e)||e.includes("could not resolve host")||e.includes("connection reset")||e.includes("rate limit")||e.includes("timed out")||e.includes("timeout")?"transient":/\b(?:401|403)\b/.test(e)||e.includes("authentication failed")||e.includes("access denied")?"authentication":/\b404\b/.test(e)||e.includes("repository not found")||e.includes("does not appear to be a git repository")?"repository-unavailable":"transient"}(Buffer.concat(s).toString("utf8")))}finally{clearTimeout(l),o?.removeEventListener("abort",E)}}function Et(t){if(null===t.exitCode&&null===t.signalCode){if("win32"!==process.platform&&t.pid)try{return void process.kill(-t.pid,"SIGKILL")}catch{}t.kill("SIGKILL")}}function lt(t){return t.length<=32768&&!t.includes("\0")&&!/[\r\n]/.test(t)}function Tt(t=process.platform){return"linux"===t?"/usr/bin/prlimit":void 0}class pt{async withEnvironment(t,r,o){const i=await I(E(S(),`squid-source-git-${e(6).toString("hex")}-`));await n(i,448);const s=E(i,"askpass.sh");try{return await _(s,'#!/bin/sh\ncase "$1" in\n *Username*) printf \'%s\\n\' "$SQUID_GIT_USERNAME" ;;\n *Password*) printf \'%s\\n\' "$SQUID_GIT_PASSWORD" ;;\n *) exit 1 ;;\nesac\n',{mode:448}),await o({GIT_ASKPASS:s,GIT_CONFIG_NOSYSTEM:"1",GIT_TERMINAL_PROMPT:"0",HOME:i,PATH:process.env.PATH,SQUID_GIT_PASSWORD:r,SQUID_GIT_USERNAME:t})}finally{await c(i,{force:!0,maxRetries:5,recursive:!0,retryDelay:100})}}}export{f as BitbucketCloudClient,P as GitHubAppClient,et as SourceGitCommand,tt as SourceGitCommandError,pt as SourceGitCredentialHelper,N as SourceProviderError,ct as buildSourceGitEnvironment,_t as buildSourceGitInvocation,rt as buildSourceGitNsjailArguments,k as isTrustedBoundaryExecutable};
1
+ import{createPrivateKey as t,randomBytes as e,sign as r}from"node:crypto";import{spawn as o}from"node:child_process";import{chmod as n,copyFile as i,lstat as s,mkdir as a,mkdtemp as I,rm as c,writeFile as _}from"node:fs/promises";import{isAbsolute as u,join as E,posix as l,relative as T,sep as p}from"node:path";import{tmpdir as S}from"node:os";let d=t=>new Error(t);function A(t,e,...r){t||function(t,...e){const r=function(t){return void 0===t?"":"string"==typeof t?t:t()}(t);if("object"==typeof r)throw r;throw d(r||"Assertion error",...e)}(e,...r)}class N extends Error{constructor(t,e){super(e),this.classification=t,this.name="SourceProviderError"}}const O="https://api.bitbucket.org",R="bitbucket.org";class f{constructor(t,e=fetch){this.fetchFn=e,A(t.email.trim()===t.email&&t.email.length>0,"BITBUCKET_EMAIL_INVALID"),A(t.apiToken.trim()===t.apiToken&&t.apiToken.length>0,"BITBUCKET_API_TOKEN_INVALID"),this.authorization=`Basic ${Buffer.from(`${t.email}:${t.apiToken}`).toString("base64")}`}async listRepositories(t,e){w(e),B(t,"BITBUCKET_WORKSPACE_INVALID");const r=[],o=new Set,n=new Set,i=new Set,s=`/2.0/repositories/${t}`;let a=`${O}${s}?pagelen=100`,I=0;for(;a;){A(I<100,"BITBUCKET_PAGINATION_LIMIT_EXCEEDED"),I+=1,A(!i.has(a),"BITBUCKET_PAGINATION_CYCLE"),i.add(a);const c=await this.request(a,e);w(e),A(Array.isArray(c.values),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");for(const e of c.values){const i=m(e,t),s=i.providerRepositoryId.toLowerCase(),a=i.fullName.toLowerCase();A(!o.has(s)&&!n.has(a),"BITBUCKET_REPOSITORIES_MUST_BE_UNIQUE"),o.add(s),n.add(a),r.push(i)}a=h(c.next,s)}return w(e),r}async getRepository(t,e,r){w(r),B(t,"BITBUCKET_WORKSPACE_INVALID"),B(e,"BITBUCKET_REPOSITORY_SLUG_INVALID");const o=await this.request(`${O}/2.0/repositories/${encodeURIComponent(t)}/${encodeURIComponent(e)}`,r);w(r);const n=m(o,t);return A(n.fullName.toLowerCase()===`${t}/${e}`.toLowerCase(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),n}async request(t,e){let r;try{r=await this.fetchFn(t,{headers:{Accept:"application/json",Authorization:this.authorization},redirect:"manual",signal:e})}catch(t){if(e?.aborted||C(t))throw new N("aborted","BITBUCKET_REQUEST_ABORTED");throw new N("transient","BITBUCKET_PROVIDER_UNAVAILABLE")}if(r.redirected||r.status>=300&&r.status<400)throw new Error("BITBUCKET_REDIRECT_REJECTED");if(!r.ok)throw 401===(o=r.status)?new N("authentication","BITBUCKET_AUTHENTICATION_FAILED"):403===o?new N("authentication","BITBUCKET_PERMISSION_DENIED"):429===o?new N("transient","BITBUCKET_RATE_LIMITED"):o>=500?new N("transient","BITBUCKET_PROVIDER_UNAVAILABLE"):new N("repository-unavailable",404===o?"BITBUCKET_NOT_FOUND":"BITBUCKET_REQUEST_FAILED");var o;try{const t=await r.json();return w(e),A(!!t&&"object"==typeof t&&!Array.isArray(t),"BITBUCKET_RESPONSE_INVALID"),t}catch(t){if(e?.aborted||C(t))throw new N("aborted","BITBUCKET_REQUEST_ABORTED");if(t instanceof Error&&t.message.startsWith("BITBUCKET_"))throw t;throw new Error("BITBUCKET_RESPONSE_INVALID")}}}function m(t,e){A(!!t&&"object"==typeof t&&!Array.isArray(t),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const r=t;A("string"==typeof r.uuid&&r.uuid.trim()===r.uuid&&r.uuid.length>0,"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof r.full_name&&/^[^/\s]+\/[^/\s]+$/.test(r.full_name)&&r.full_name.split("/")[0]?.toLowerCase()===e.toLowerCase(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const o=L(r.links,"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),n=o.clone;A(Array.isArray(n),"BITBUCKET_REPOSITORY_RESPONSE_INVALID");const i=n.filter(t=>"https"===function(t){return t&&"object"==typeof t&&!Array.isArray(t)?t:void 0}(t)?.name);A(1===i.length,"BITBUCKET_REPOSITORY_URL_INVALID");const s=U(L(i[0],"BITBUCKET_REPOSITORY_URL_INVALID").href,r.full_name,!0),a=U(L(o.html,"BITBUCKET_REPOSITORY_URL_INVALID").href,r.full_name,!1),I=r.mainbranch,c=null===I?"":function(t){return A("string"==typeof t&&t.trim(),"BITBUCKET_REPOSITORY_RESPONSE_INVALID"),t}(L(I,"BITBUCKET_REPOSITORY_RESPONSE_INVALID").name);return{cloneUrl:s,defaultBranch:c,fullName:r.full_name,provider:"bitbucket",providerRepositoryId:r.uuid,webUrl:a}}function h(t,e){if(null==t)return;let r;A("string"==typeof t&&t,"BITBUCKET_PAGINATION_URL_INVALID");try{r=new URL(t)}catch{throw new Error("BITBUCKET_PAGINATION_URL_INVALID")}const o=/^https:\/\/(?:[^/@]+@)?api\.bitbucket\.org:\d+(?:\/|$)/i.test(t);return A(!("https:"!==r.protocol||"api.bitbucket.org"!==r.hostname||o||r.username||r.password||r.hash||r.pathname!==e),"BITBUCKET_PAGINATION_URL_INVALID"),r.toString()}function U(t,e,r){if("string"!=typeof t)throw new Error("BITBUCKET_REPOSITORY_URL_INVALID");let o;try{o=new URL(t)}catch{throw new Error("BITBUCKET_REPOSITORY_URL_INVALID")}const n=`/${e}${r?".git":""}`,i=/^https:\/\/(?:[^/@]+@)?bitbucket\.org:\d+(?:\/|$)/i.test(t);return A("https:"===o.protocol&&o.hostname===R&&!i&&(r||!o.username)&&!o.password&&!o.search&&!o.hash&&o.pathname===n,"BITBUCKET_REPOSITORY_URL_INVALID"),`https://${R}${n}`}function L(t,e){return A(!!t&&"object"==typeof t&&!Array.isArray(t),e),t}function B(t,e){A("string"==typeof t&&t.trim()===t&&/^[A-Za-z0-9_-]+$/.test(t),e)}function w(t){if(t?.aborted)throw new N("aborted","BITBUCKET_REQUEST_ABORTED")}function C(t){return t instanceof Error&&"AbortError"===t.name}const D="https://api.github.com",y=3e5,G="2022-11-28";class P{constructor(e,r=fetch){this.fetchFn=r,this.tokenCache=new Map,g(e.appId),g(e.installationId),this.appId=e.appId,this.installationId=e.installationId,this.privateKey=function(e){A("string"==typeof e&&e.trim(),"GITHUB_PRIVATE_KEY_INVALID");const r=e.replace(/\\r\\n/g,"\n").replace(/\\n/g,"\n").replace(/\r\n/g,"\n").trim(),o=r.includes("\n")?r:function(t){const e=/^-----BEGIN (RSA )?PRIVATE KEY-----(.*?)-----END (RSA )?PRIVATE KEY-----$/s.exec(t);if(!e)return t;const r=e[1]?"RSA PRIVATE KEY":"PRIVATE KEY";return`-----BEGIN ${r}-----\n${(e[2].replace(/\s/g,"").match(/.{1,64}/g)??[]).join("\n")}\n-----END ${r}-----`}(r);try{t(o)}catch{throw new Error("GITHUB_PRIVATE_KEY_INVALID")}return o}(e.privateKey)}async createInstallationToken(t,e){K(e);const r=function(t){const e=t.repositoryIds?[...t.repositoryIds].sort((t,e)=>t-e):void 0;A(!e||e.every(t=>Number.isSafeInteger(t)&&t>0),"GITHUB_REPOSITORY_IDS_INVALID");const r=t.permissions?Object.fromEntries(Object.entries(t.permissions).sort(([t],[e])=>t.localeCompare(e)).map(([t,e])=>(A("read"===e||"write"===e,"GITHUB_PERMISSIONS_INVALID"),[t,e]))):void 0;return{...e?{repository_ids:e}:{},...r?{permissions:r}:{}}}(t),o=JSON.stringify(r),n=this.tokenCache.get(o);if(n&&Date.parse(n.expiresAt)-Date.now()>y)return n;const i=await V(this.fetchFn,`${D}/app/installations/${this.installationId}/access_tokens`,{body:JSON.stringify(r),headers:{Accept:"application/vnd.github+json",Authorization:`Bearer ${this.createAppJwt()}`,"Content-Type":"application/json","X-GitHub-Api-Version":G},method:"POST",redirect:"manual",signal:e}),s=await b(i,e);K(e);const a=$(s,"token"),I=$(s,"expires_at");A(Number.isFinite(Date.parse(I)),"GITHUB_TOKEN_RESPONSE_INVALID");const c=function(t){const e=t.permissions;A(null!==e&&"object"==typeof e&&!Array.isArray(e),"GITHUB_TOKEN_RESPONSE_INVALID");const r={};for(const[t,o]of Object.entries(e))A(/^[a-z][a-z0-9_]{0,127}$/.test(t)&&("read"===o||"write"===o),"GITHUB_TOKEN_RESPONSE_INVALID"),r[t]=o;return Object.fromEntries(Object.entries(r).sort(([t],[e])=>t.localeCompare(e)))}(s),_=function(t){const e=t.repository_selection;return A("all"===e||"selected"===e,"GITHUB_TOKEN_RESPONSE_INVALID"),e}(s),u=function(t){const e=t.repositories;if(void 0===e)return[];A(Array.isArray(e),"GITHUB_TOKEN_RESPONSE_INVALID");const r=e.map(t=>(A(null!==t&&"object"==typeof t&&!Array.isArray(t)&&"id"in t&&Number.isSafeInteger(t.id)&&t.id>0,"GITHUB_TOKEN_RESPONSE_INVALID"),t.id));return A(new Set(r).size===r.length,"GITHUB_TOKEN_RESPONSE_INVALID"),r.sort((t,e)=>t-e)}(s),E={expiresAt:I,permissions:c,repositoryIds:u,repositorySelection:_,token:a};return function(t,e){e.permissions&&A(JSON.stringify(t.permissions)===JSON.stringify(e.permissions),"GITHUB_TOKEN_SCOPE_INVALID"),e.repository_ids&&A("selected"===t.repositorySelection&&t.repositoryIds.length===e.repository_ids.length&&t.repositoryIds.every((t,r)=>t===e.repository_ids?.[r]),"GITHUB_TOKEN_SCOPE_INVALID")}(E,r),Date.parse(I)-Date.now()>y&&this.tokenCache.set(o,E),E}async listInstallationRepositories(t){K(t);const{token:e}=await this.createInstallationToken({},t),r=[],o=new Set,n=new Set,i=new Set;let s=`${D}/installation/repositories?per_page=100`,a=0;for(;s;){A(a<100,"GITHUB_PAGINATION_LIMIT_EXCEEDED"),a+=1,A(!i.has(s),"GITHUB_PAGINATION_CYCLE"),i.add(s);const I=await V(this.fetchFn,s,{headers:{Accept:"application/vnd.github+json",Authorization:`Bearer ${e}`,"X-GitHub-Api-Version":G},redirect:"manual",signal:t}),c=await b(I,t);K(t);const _=c.repositories;A(Array.isArray(_),"GITHUB_REPOSITORY_RESPONSE_INVALID");for(const t of _.map(H)){A(!o.has(t.providerRepositoryId),"GITHUB_REPOSITORIES_MUST_BE_UNIQUE"),o.add(t.providerRepositoryId);const e=t.fullName.toLowerCase();A(!n.has(e),"GITHUB_REPOSITORIES_MUST_BE_UNIQUE"),n.add(e),r.push(t)}s=x(I.headers.get("link"))}return K(t),r}createAppJwt(){const t=Math.floor(Date.now()/1e3),e=`${M({alg:"RS256",typ:"JWT"})}.${M({exp:t+600,iat:t-60,iss:this.appId})}`;return`${e}.${r("RSA-SHA256",Buffer.from(e),this.privateKey).toString("base64url")}`}}function g(t){A("string"==typeof t&&/^\d+$/.test(t),"GITHUB_IDENTIFIER_INVALID")}async function b(t,e){if(t.redirected||t.status>=300&&t.status<400)throw new Error("GITHUB_REDIRECT_REJECTED");if(!t.ok)throw function(t){const{status:e}=t;return 401===e?new N("authentication","GITHUB_AUTHENTICATION_FAILED"):403===e?t.headers.has("retry-after")||"0"===t.headers.get("x-ratelimit-remaining")?new N("transient","GITHUB_RATE_LIMITED"):new N("authentication","GITHUB_PERMISSION_DENIED"):429===e?new N("transient","GITHUB_RATE_LIMITED"):e>=500?new N("transient","GITHUB_PROVIDER_UNAVAILABLE"):new N("repository-unavailable","GITHUB_REQUEST_FAILED")}(t);try{const e=await t.json();return A(!!e&&"object"==typeof e&&!Array.isArray(e),"GITHUB_RESPONSE_INVALID"),e}catch(t){if(e?.aborted||v(t))throw new N("aborted","GITHUB_REQUEST_ABORTED");if(t instanceof Error&&t.message.startsWith("GITHUB_"))throw t;throw new Error("GITHUB_RESPONSE_INVALID")}}async function V(t,e,r){try{return await t(e,r)}catch(t){if(r.signal?.aborted||v(t))throw new N("aborted","GITHUB_REQUEST_ABORTED");throw new N("transient","GITHUB_PROVIDER_UNAVAILABLE")}}function K(t){if(t?.aborted)throw new N("aborted","GITHUB_REQUEST_ABORTED")}function v(t){return t instanceof Error&&"AbortError"===t.name}function H(t){A(!!t&&"object"==typeof t&&!Array.isArray(t),"GITHUB_REPOSITORY_RESPONSE_INVALID");const e=t,r=e.id,o=e.full_name,n=e.clone_url,i=e.html_url,s=e.default_branch;return A(Number.isSafeInteger(r)&&r>0,"GITHUB_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof o&&/^[^/\s]+\/[^/\s]+$/.test(o),"GITHUB_REPOSITORY_RESPONSE_INVALID"),A("string"==typeof s&&s.trim(),"GITHUB_REPOSITORY_RESPONSE_INVALID"),F(n,o,!0),F(i,o,!1),{cloneUrl:n,defaultBranch:s,fullName:o,provider:"github",providerRepositoryId:String(r),webUrl:i}}function F(t,e,r){if("string"!=typeof t)throw new Error("GITHUB_REPOSITORY_URL_INVALID");try{const o=new URL(t),n=`/${e}${r?".git":""}`;A(!("https:"!==o.protocol||"github.com"!==o.hostname||o.port||o.username||o.password||o.search||o.hash||o.pathname!==n),"GITHUB_REPOSITORY_URL_INVALID")}catch(t){if(t instanceof Error&&"GITHUB_REPOSITORY_URL_INVALID"===t.message)throw t;throw new Error("GITHUB_REPOSITORY_URL_INVALID")}}function x(t){if(!t)return;const e=t.split(",").map(t=>t.trim()).find(t=>/;\s*rel="?next"?/i.test(t));if(!e)return;const r=/^<([^>]+)>/.exec(e)?.[1];let o;A(r,"GITHUB_PAGINATION_URL_INVALID");try{o=new URL(r)}catch{throw new Error("GITHUB_PAGINATION_URL_INVALID")}return A(!("https:"!==o.protocol||"api.github.com"!==o.hostname||o.username||o.password||o.port||o.hash||"/installation/repositories"!==o.pathname),"GITHUB_PAGINATION_URL_INVALID"),o.toString()}function $(t,e){const r=t[e];return A("string"==typeof r&&r,"GITHUB_TOKEN_RESPONSE_INVALID"),r}function M(t){return Buffer.from(JSON.stringify(t)).toString("base64url")}function k(t){return t.isFile()&&0===t.uid&&0===t.gid&&!!(73&t.mode)&&!(3584&t.mode)&&!(18&t.mode)}const Y=16777216,j="/usr/local/bin/nsjail",Q="/opt/source-agent-rootfs",z="/workspace",W="/usr/local/bin/squid-source-git-askpass",J="/proc",q=["COMSPEC","HOME","PATH","SSL_CERT_DIR","SSL_CERT_FILE","SystemRoot","WINDIR"],X=["GIT_ASKPASS","GIT_SSL_CAINFO","SQUID_GIT_PASSWORD","SQUID_GIT_USERNAME"],Z=new Set(["GIT_ASKPASS","GIT_CONFIG_GLOBAL","GIT_CONFIG_NOSYSTEM","GIT_LFS_SKIP_SMUDGE","GIT_LITERAL_PATHSPECS","GIT_NO_REPLACE_OBJECTS","GIT_OPTIONAL_LOCKS","GIT_SSL_CAINFO","GIT_TERMINAL_PROMPT","HOME","LANG","LC_ALL","PATH","SQUID_GIT_PASSWORD","SQUID_GIT_USERNAME","SSL_CERT_DIR","SSL_CERT_FILE","TMPDIR"]);class tt extends Error{constructor(t){super(`SOURCE_GIT_${t.toUpperCase().replaceAll("-","_")}`),this.kind=t,this.name="SourceGitCommandError"}}class et{constructor(t,e=function(){return"win32"===process.platform?"git.exe":"/usr/bin/git"}(),r=Tt()){this.runtimeRootDirectory=t,this.executable=e,this.fileLimitExecutable=r,A("win32"===process.platform||u(e),"SOURCE_CONFIGURATION_INVALID"),A(void 0===r||u(r),"SOURCE_CONFIGURATION_INVALID")}async run(t,e){if(A(t.length>0&&t.every(lt),"SOURCE_CONFIGURATION_INVALID"),A(Number.isSafeInteger(e.timeoutMs)&&e.timeoutMs>0,"SOURCE_CONFIGURATION_INVALID"),A(void 0===e.maxFileBytes||Number.isSafeInteger(e.maxFileBytes)&&e.maxFileBytes>0,"SOURCE_CONFIGURATION_INVALID"),A(void 0===e.maxOutputBytes||Number.isSafeInteger(e.maxOutputBytes)&&e.maxOutputBytes>0,"SOURCE_CONFIGURATION_INVALID"),e.signal?.aborted)throw new tt("aborted");let r;try{const c="linux"===process.platform?e.sandboxRootDirectory:void 0;c?(It(c),await Promise.all([it(j),it(`${Q}${this.executable}`),it(`${Q}${W}`),...this.fileLimitExecutable?[it(`${Q}${this.fileLimitExecutable}`)]:[]]),r=await I(E(c,".squid-trusted-git-"))):(await a(this.runtimeRootDirectory,{mode:448,recursive:!0}),r=await I(E(this.runtimeRootDirectory,"disabled-git-hooks-"))),A(r,"SOURCE_CONFIGURATION_INVALID");const _=c?E(r,"hooks"):r;if(c&&await a(_,{mode:448}),await n(_,448),e.signal?.aborted)throw new tt("aborted");const S=ct(e.environment);if(c){const a=T(c,r);A(a.length>0&&!a.includes(p)&&".."!==a,"SOURCE_CONFIGURATION_INVALID");const I=l.join(z,a);S.GIT_ASKPASS=W,S.HOME="/home/source-agent",S.PATH="/usr/local/bin:/usr/bin:/bin",S.SSL_CERT_DIR="/etc/ssl/certs",S.SSL_CERT_FILE="/etc/ssl/certs/ca-certificates.crt",S.TMPDIR="/tmp",delete S.COMSPEC,delete S.SystemRoot,delete S.WINDIR,S.GIT_SSL_CAINFO&&(S.GIT_SSL_CAINFO=await async function(t,e,r){A(u(t)&&!t.includes("\0"),"SOURCE_CONFIGURATION_INVALID");const o=await s(t);A(o.isFile()&&o.size>0&&o.size<=16777216,"SOURCE_CONFIGURATION_INVALID");const a="ca.pem",I=E(e,a);return await i(t,I),await n(I,256),l.join(r,a)}(S.GIT_SSL_CAINFO,r,I));const _=t.map(t=>ot(t,c)),d=e.cwd?function(t,e){A(u(t),"SOURCE_CONFIGURATION_INVALID");const r=ot(t,e);return A(nt(r),"SOURCE_CONFIGURATION_INVALID"),r}(e.cwd,c):z,N=rt({environment:S,invocation:_t(this.executable,[...this.fixedArguments(e.mode,l.join(I,"hooks")),..._],e.maxFileBytes,process.platform,this.fileLimitExecutable),maxFileBytes:e.maxFileBytes??10737418240,mode:e.mode,tenantGid:st(process.getgid?.()),tenantUid:st(process.getuid?.()),timeoutMs:e.timeoutMs,workingDirectory:d,workspaceSource:c}),O=o(j,N,{detached:!0,env:S,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await ut(O,e.input,e.maxOutputBytes??Y,e.signal,e.timeoutMs)}const d=_t(this.executable,[...this.fixedArguments(e.mode,_),...t],e.maxFileBytes,process.platform,this.fileLimitExecutable),N=o(d.executable,d.arguments,{cwd:e.cwd,detached:"win32"!==process.platform,env:S,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await ut(N,e.input,e.maxOutputBytes??Y,e.signal,e.timeoutMs)}finally{r&&await c(r,{force:!0,recursive:!0})}}fixedArguments(t,e){return["--no-optional-locks","-c","advice.detachedHead=false","-c","core.longpaths=true","-c",`core.hooksPath=${e}`,"-c","credential.helper=","-c","gc.autoDetach=false","-c","maintenance.autoDetach=false","-c","http.followRedirects=false","-c","core.sshCommand=false","-c","protocol.ext.allow=never","-c","protocol.file.allow="+("local"===t?"always":"never"),"-c","protocol.ssh.allow=never","-c","protocol.git.allow=never","-c","protocol.http.allow=never","-c","protocol.https.allow=always"]}}function rt(t){A(Number.isSafeInteger(t.timeoutMs)&&t.timeoutMs>0,"SOURCE_CONFIGURATION_INVALID"),A(Number.isSafeInteger(t.maxFileBytes)&&t.maxFileBytes>0,"SOURCE_CONFIGURATION_INVALID"),It(t.workspaceSource),at(t.tenantUid),at(t.tenantGid);const e=Math.max(1,Math.ceil(t.timeoutMs/1e3)),r=Math.max(1,Math.ceil(t.maxFileBytes/1048576)),o=t.workingDirectory??z;A(nt(o),"SOURCE_CONFIGURATION_INVALID");const n=["--mode","o",..."remote"===t.mode?["--disable_clone_newnet"]:[],"--chroot",Q,"--cwd",o,"--user",`65532:${t.tenantUid}:1`,"--group",`65532:${t.tenantGid}:1`,"--hostname","squid-trusted-git","--time_limit",String(e),"--rlimit_as","4096","--rlimit_cpu",String(e),"--rlimit_fsize",String(r),"--rlimit_nofile","256","--rlimit_nproc","128","--max_cpus","2","--bindmount",`${t.workspaceSource}:${z}`,"--mount","none:/home/source-agent:tmpfs:size=67108864,nr_inodes=16384,mode=0700,uid=65532,gid=65532","--mount","none:/tmp:tmpfs:size=67108864,nr_inodes=16384,mode=0700,uid=65532,gid=65532","--bindmount","/dev/null:/dev/null","--bindmount_ro","/dev/urandom:/dev/urandom","--disable_proc","--bindmount_ro",`${J}:${J}`];for(const e of Object.keys(t.environment).sort())A(Z.has(e),"SOURCE_CONFIGURATION_INVALID"),n.push("--env",e);return n.push("--",t.invocation.executable,...t.invocation.arguments),n}function ot(t,e){const r=T(e,t);return""===r?z:function(t){return".."!==t&&!t.startsWith(`..${p}`)&&!u(t)}(r)?l.join(z,...r.split(p)):t}function nt(t){return t===l.normalize(t)&&(t===z||t.startsWith(`${z}/`))}async function it(t){A(k(await s(t)),"SOURCE_SANDBOX_UNAVAILABLE")}function st(t){return A(void 0!==t,"SOURCE_SANDBOX_UNAVAILABLE"),at(t),t}function at(t){A(Number.isSafeInteger(t)&&t>=0,"SOURCE_SANDBOX_UNAVAILABLE")}function It(t){A(u(t)&&!t.includes("\0")&&!t.includes(":")&&!/[\r\n]/.test(t),"SOURCE_CONFIGURATION_INVALID")}function ct(t,e=process.env,r=process.platform){const o={GIT_CONFIG_GLOBAL:"win32"===r?"NUL":"/dev/null",GIT_CONFIG_NOSYSTEM:"1",GIT_LFS_SKIP_SMUDGE:"1",GIT_LITERAL_PATHSPECS:"1",GIT_NO_REPLACE_OBJECTS:"1",GIT_OPTIONAL_LOCKS:"0",GIT_TERMINAL_PROMPT:"0",LANG:"C",LC_ALL:"C",PATH:e.PATH};for(const r of q){const n=t?.[r]??e[r];void 0!==n&&(o[r]=n)}for(const e of X){const r=t?.[e];void 0!==r&&(o[e]=r)}return o}function _t(t,e,r,o=process.platform,n=Tt(o)){return"linux"!==o||void 0===r?{arguments:e,executable:t}:(A(Number.isSafeInteger(r)&&r>0&&void 0!==n&&u(n),"SOURCE_CONFIGURATION_INVALID"),{arguments:[`--fsize=${r}`,"--",t,...e],executable:n})}async function ut(t,e,r,o,n){A(Number.isSafeInteger(r)&&r>0,"SOURCE_CONFIGURATION_INVALID");const i=[],s=[];let a=0,I=!1,c=!1,_=!1;const u=(e,o)=>{if(a+=o.length,a>r)return c=!0,void Et(t);e.push(o)};t.stdout?.on("data",t=>u(i,t)),t.stderr?.on("data",t=>u(s,t));const E=()=>{I=!0,Et(t)};o?.addEventListener("abort",E,{once:!0}),o?.aborted&&E();const l=setTimeout(()=>{_=!0,Et(t)},n);l.unref?.(),e?t.stdin?.end(e):t.stdin?.end();try{const{code:e,error:r,signal:o}=await function(t){return new Promise(e=>{let r;t.once("error",t=>{r=t}),t.once("close",(t,o)=>{e({code:t,...r?{error:r}:{},signal:o})})})}(t);if(c)throw new tt("resource-limit");if(I)throw new tt("aborted");if(_)throw new tt("transient");if(r)throw new tt("transient");if(0===e)return{stdout:Buffer.concat(i)};if("SIGXFSZ"===o)throw new tt("resource-limit");if("SIGKILL"===o)throw new tt("transient");throw new tt(function(t){const e=t.toLowerCase();return e.includes("file too large")||e.includes("disk quota exceeded")||e.includes("no space left")||e.includes("resource limit")?"resource-limit":/\b429\b/.test(e)||/\b5\d\d\b/.test(e)||e.includes("could not resolve host")||e.includes("connection reset")||e.includes("rate limit")||e.includes("timed out")||e.includes("timeout")?"transient":/\b(?:401|403)\b/.test(e)||e.includes("authentication failed")||e.includes("access denied")?"authentication":/\b404\b/.test(e)||e.includes("repository not found")||e.includes("does not appear to be a git repository")?"repository-unavailable":"transient"}(Buffer.concat(s).toString("utf8")))}finally{clearTimeout(l),o?.removeEventListener("abort",E)}}function Et(t){if(null===t.exitCode&&null===t.signalCode){if("win32"!==process.platform&&t.pid)try{return void process.kill(-t.pid,"SIGKILL")}catch{}t.kill("SIGKILL")}}function lt(t){return t.length<=32768&&!t.includes("\0")&&!/[\r\n]/.test(t)}function Tt(t=process.platform){return"linux"===t?"/usr/bin/prlimit":void 0}class pt{async withEnvironment(t,r,o){const i=await I(E(S(),`squid-source-git-${e(6).toString("hex")}-`));await n(i,448);const s=E(i,"askpass.sh");try{return await _(s,'#!/bin/sh\ncase "$1" in\n *Username*) printf \'%s\\n\' "$SQUID_GIT_USERNAME" ;;\n *Password*) printf \'%s\\n\' "$SQUID_GIT_PASSWORD" ;;\n *) exit 1 ;;\nesac\n',{mode:448}),await o({GIT_ASKPASS:s,GIT_CONFIG_NOSYSTEM:"1",GIT_TERMINAL_PROMPT:"0",HOME:i,PATH:process.env.PATH,SQUID_GIT_PASSWORD:r,SQUID_GIT_USERNAME:t})}finally{await c(i,{force:!0,maxRetries:5,recursive:!0,retryDelay:100})}}}export{f as BitbucketCloudClient,P as GitHubAppClient,et as SourceGitCommand,tt as SourceGitCommandError,pt as SourceGitCredentialHelper,N as SourceProviderError,ct as buildSourceGitEnvironment,_t as buildSourceGitInvocation,rt as buildSourceGitNsjailArguments,k as isTrustedBoundaryExecutable};
@@ -33,6 +33,8 @@ export interface AiFunctionCallContext<FunctionContextType = unknown, AgentConte
33
33
  }
34
34
  /** AiFunctionContext for the connected integration function. Contains predefined integration data. */
35
35
  export interface AiFunctionCallContextWithIntegration<AiConnectedIntegrationOptionsType = unknown, FunctionContextType = unknown, AgentContextType = unknown> extends AiFunctionCallContext<FunctionContextType, AgentContextType>, AiConnectedIntegrationMetadata<AiConnectedIntegrationOptionsType> {
36
+ /** Access token the caller supplied for this integration on the current request, if any. */
37
+ accessToken?: string;
36
38
  }
37
39
  /**
38
40
  * Provides context to the security rules of an AI Agent for each new user prompt.
@@ -14,3 +14,5 @@ export type TenantModuleId = string;
14
14
  export declare const USER_CODE_MODULE_ID: TenantModuleId;
15
15
  /** Type for the Python user code module in hybrid (TS + Python) projects. */
16
16
  export declare const USER_CODE_PYTHON_MODULE_ID: TenantModuleId;
17
+ /** The modules an application's own code is stored under: TypeScript, plus Python in hybrid projects. */
18
+ export declare const USER_CODE_MODULE_IDS: TenantModuleId[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squidcloud/backend",
3
- "version": "1.0.492",
3
+ "version": "1.0.494",
4
4
  "description": "Squid Cloud's backend SDK",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -57,7 +57,7 @@
57
57
  "typedoc-plugin-markdown": "^4.8.1"
58
58
  },
59
59
  "peerDependencies": {
60
- "@squidcloud/client": "^1.0.492"
60
+ "@squidcloud/client": "^1.0.494"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=20.0.0"