@squidcloud/backend 1.0.503 → 1.0.504
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.
- package/dist/backend/src/source-code/source-git-command.d.ts +6 -2
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/internal/source-code.js +1 -1
- package/dist/cjs/langgraph.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/internal/source-code.js +1 -1
- package/dist/esm/langgraph.js +1 -1
- package/dist/internal-common/src/public-types-backend/ai-agent.public-context.d.ts +16 -0
- package/dist/internal-common/src/public-types-backend/langgraph.public-context.d.ts +11 -0
- package/dist/internal-common/src/public-types-backend/storage.public-types.d.ts +5 -1
- package/package.json +2 -2
|
@@ -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 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};
|
|
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 d}from"node:path";import{tmpdir as p}from"node:os";let S=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 S(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=x(s,"token"),I=x(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=$(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 $(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 x(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="/etc/resolv.conf",X=["COMSPEC","HOME","PATH","SSL_CERT_DIR","SSL_CERT_FILE","SystemRoot","WINDIR"],Z=["GIT_ASKPASS","GIT_SSL_CAINFO","SQUID_GIT_PASSWORD","SQUID_GIT_USERNAME"],tt=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 et extends Error{constructor(t,e){super(`SOURCE_GIT_${t.toUpperCase().replaceAll("-","_")}`),this.kind=t,this.exitCode=e,this.name="SourceGitCommandError"}}class rt{constructor(t,e=function(){return"win32"===process.platform?"git.exe":"/usr/bin/git"}(),r=dt()){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(Tt),"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 et("aborted");let r;try{const c="linux"===process.platform?e.sandboxRootDirectory:void 0;c?(ct(c),await Promise.all([st(j),st(`${Q}${this.executable}`),st(`${Q}${W}`),...this.fileLimitExecutable?[st(`${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 et("aborted");const p=_t(e.environment);if(c){const a=T(c,r);A(a.length>0&&!a.includes(d)&&".."!==a,"SOURCE_CONFIGURATION_INVALID");const I=l.join(z,a);p.GIT_ASKPASS=W,p.HOME="/home/source-agent",p.PATH="/usr/local/bin:/usr/bin:/bin",p.SSL_CERT_DIR="/etc/ssl/certs",p.SSL_CERT_FILE="/etc/ssl/certs/ca-certificates.crt",p.TMPDIR="/tmp",delete p.COMSPEC,delete p.SystemRoot,delete p.WINDIR,p.GIT_SSL_CAINFO&&(p.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)}(p.GIT_SSL_CAINFO,r,I));const _=t.map(t=>nt(t,c)),S=e.cwd?function(t,e){A(u(t),"SOURCE_CONFIGURATION_INVALID");const r=nt(t,e);return A(it(r),"SOURCE_CONFIGURATION_INVALID"),r}(e.cwd,c):z,N=ot({environment:p,invocation:ut(this.executable,[...this.fixedArguments(e.mode,l.join(I,"hooks")),..._],e.maxFileBytes,process.platform,this.fileLimitExecutable),maxFileBytes:e.maxFileBytes??10737418240,mode:e.mode,tenantGid:at(process.getgid?.()),tenantUid:at(process.getuid?.()),timeoutMs:e.timeoutMs,workingDirectory:S,workspaceSource:c}),O=o(j,N,{detached:!0,env:p,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await Et(O,e.input,e.maxOutputBytes??Y,e.signal,e.timeoutMs)}const S=ut(this.executable,[...this.fixedArguments(e.mode,_),...t],e.maxFileBytes,process.platform,this.fileLimitExecutable),N=o(S.executable,S.arguments,{cwd:e.cwd,detached:"win32"!==process.platform,env:p,stdio:["pipe","pipe","pipe"],windowsHide:!0});return await Et(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 ot(t){A(Number.isSafeInteger(t.timeoutMs)&&t.timeoutMs>0,"SOURCE_CONFIGURATION_INVALID"),A(Number.isSafeInteger(t.maxFileBytes)&&t.maxFileBytes>0,"SOURCE_CONFIGURATION_INVALID"),ct(t.workspaceSource),It(t.tenantUid),It(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(it(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}`,..."remote"===t.mode?["--bindmount_ro",`${q}:${q}`]:[]];for(const e of Object.keys(t.environment).sort())A(tt.has(e),"SOURCE_CONFIGURATION_INVALID"),n.push("--env",e);return n.push("--",t.invocation.executable,...t.invocation.arguments),n}function nt(t,e){const r=T(e,t);return""===r?z:function(t){return".."!==t&&!t.startsWith(`..${d}`)&&!u(t)}(r)?l.join(z,...r.split(d)):t}function it(t){return t===l.normalize(t)&&(t===z||t.startsWith(`${z}/`))}async function st(t){A(k(await s(t)),"SOURCE_SANDBOX_UNAVAILABLE")}function at(t){return A(void 0!==t,"SOURCE_SANDBOX_UNAVAILABLE"),It(t),t}function It(t){A(Number.isSafeInteger(t)&&t>=0,"SOURCE_SANDBOX_UNAVAILABLE")}function ct(t){A(u(t)&&!t.includes("\0")&&!t.includes(":")&&!/[\r\n]/.test(t),"SOURCE_CONFIGURATION_INVALID")}function _t(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 X){const n=t?.[r]??e[r];void 0!==n&&(o[r]=n)}for(const e of Z){const r=t?.[e];void 0!==r&&(o[e]=r)}return o}function ut(t,e,r,o=process.platform,n=dt(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 Et(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 lt(t);e.push(o)};t.stdout?.on("data",t=>u(i,t)),t.stderr?.on("data",t=>u(s,t));const E=()=>{I=!0,lt(t)};o?.addEventListener("abort",E,{once:!0}),o?.aborted&&E();const l=setTimeout(()=>{_=!0,lt(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 et("resource-limit");if(I)throw new et("aborted");if(_)throw new et("transient");if(r)throw new et("transient");if(0===e)return{stdout:Buffer.concat(i)};if("SIGXFSZ"===o)throw new et("resource-limit");if("SIGKILL"===o)throw new et("transient");throw new et(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")),e??void 0)}finally{clearTimeout(l),o?.removeEventListener("abort",E)}}function lt(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 Tt(t){return t.length<=32768&&!t.includes("\0")&&!/[\r\n]/.test(t)}function dt(t=process.platform){return"linux"===t?"/usr/bin/prlimit":void 0}class pt{async withEnvironment(t,r,o){const i=await I(E(p(),`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,rt as SourceGitCommand,et as SourceGitCommandError,pt as SourceGitCredentialHelper,N as SourceProviderError,_t as buildSourceGitEnvironment,ut as buildSourceGitInvocation,ot as buildSourceGitNsjailArguments,k as isTrustedBoundaryExecutable};
|
package/dist/esm/langgraph.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getPassiveSquid as t,metadata as e}from"@squidcloud/backend";import{
|
|
1
|
+
import{getPassiveSquid as t,metadata as e}from"@squidcloud/backend";import{isAIMessage as a,isBaseMessage as n,isBaseMessageChunk as o}from"@langchain/core/messages";import{Command as i}from"@langchain/langgraph";import{BaseCheckpointSaver as s,WRITES_IDX_MAP as r,copyCheckpoint as c,getCheckpointId as d}from"@langchain/langgraph-checkpoint";let p=t=>new Error(t);function h(t,e,...a){t||function(t,...e){const a=function(t){return void 0===t?"":"string"==typeof t?t:t()}(t);if("object"==typeof a)throw a;throw p(a||"Assertion error",...e)}(e,...a)}const u=36e5;class g extends s{constructor(t){super(),this.graphId=t}async getTuple(t){const{threadId:e,checkpointNs:a,checkpointId:n}=l(t),{graphId:o}=this,i=n?await this.postUnlessNotFound("getCheckpoint",{graphId:o,threadId:e,checkpointId:n}):await this.postUnlessNotFound("getLatestCheckpoint",{graphId:o,threadId:e,checkpointNs:a});return i&&await this.toTuple(i)}async*list(t,e={}){const{threadId:a,checkpointNs:n}=l(t),{limit:o,before:i,filter:s}=e,r={graphId:this.graphId,threadId:a,checkpointNs:n,limit:s?void 0:o,beforeCheckpointId:i&&d(i)||void 0},{checkpoints:c}=await this.post("listCheckpoints",r);let p=o??1/0;for(const t of c){if(p<=0)return;const e=await this.toTuple(t),a=e.metadata??{};s&&!Object.entries(s).every(([t,e])=>a[t]===e)||(p--,yield e)}}async put(t,e,a,n){const{threadId:o,checkpointNs:i,checkpointId:s}=l(t),r={graphId:this.graphId,threadId:o,checkpointNs:i,checkpointId:e.id,checkpoint:await this.dump(c(e)),parentCheckpointId:s??null,metadata:await this.dump(a)};return await this.post("saveCheckpoint",r),{configurable:{thread_id:o,checkpoint_ns:i,checkpoint_id:e.id}}}async putWrites(t,e,a){const{threadId:n,checkpointNs:o,checkpointId:i}=l(t);h(i,"Failed to put writes: the config has no checkpoint_id");const s=await Promise.all(e.map(async([t,e],n)=>({channel:t,taskId:a,idx:r[t]??n,value:await this.dump(e)}))),c={graphId:this.graphId,threadId:n,checkpointNs:o,checkpointId:i,writes:s};await this.post("savePendingWrites",c)}async deleteThread(t){await this.post("deleteCheckpoints",{graphId:this.graphId,threadId:t})}async toTuple(t){const{threadId:e,checkpointNs:a,checkpointId:n,parentCheckpointId:o}=t,i=t=>({configurable:{thread_id:e,checkpoint_ns:a,checkpoint_id:t}}),s=await Promise.all(t.pendingWrites.map(async({taskId:t,channel:e,value:a})=>[t,e,await this.load(a)]));return{config:i(n),checkpoint:await this.load(t.checkpoint),metadata:t.metadata?await this.load(t.metadata):void 0,parentConfig:o?i(o):void 0,pendingWrites:s}}async dump(t){const[e,a]=await this.serde.dumpsTyped(t),n=[e,{__bytes__:Buffer.from(a).toString("base64")}];return JSON.stringify(n)}async load(t){const[e,{__bytes__:a}]=JSON.parse(t);return await this.serde.loadsTyped(e,Buffer.from(a,"base64"))}async post(e,a){return await t().rpcManager.post(`langgraph/${e}`,a)}async postUnlessNotFound(t,e){try{return await this.post(t,e)}catch(t){if(404===t.statusCode)return;throw t}}}function l(t){const e=t.configurable?.thread_id;h("string"==typeof e&&e,"The LangGraph config has no thread_id");const a=t.configurable?.checkpoint_ns;return{threadId:e,checkpointNs:"string"==typeof a?a:"",checkpointId:d(t)||void 0}}async function f(t,e){const{graphId:a,operation:n,input:o,resumePayload:s,timeoutMs:r,graphContext:c}=e,d=new g(a),p=t.compile({checkpointer:d}),h=e.threadId??("function"==typeof globalThis.crypto?.randomUUID?globalThis.crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,t=>{const e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})),l={configurable:{thread_id:h}};if("getState"!==n){const t="resume"===n&&await d.getTuple(l)?new i({resume:s}):o??{},a=AbortSignal.timeout(r??u);try{const n={...l,signal:a,context:c};e.onStreamEvent?await async function(t,e,a,n){const o=await t.stream(e,{...a,streamMode:["custom","messages"],subgraphs:!0});for await(const t of o){const e=m(t);e&&n(e)}}(p,t,n,e.onStreamEvent):await p.invoke(t,n)}catch(t){if(a.aborted)return{ok:!1,errorCode:"TIMEOUT",error:`The graph run did not finish within ${r??u} ms`};throw t}}const f=await p.getState(l),k=[...f.next],y=(I=f.values)&&"object"==typeof I?JSON.parse(JSON.stringify(I)):{};var I;return"getState"===n?{ok:!0,threadId:h,state:y,next:k}:{ok:!0,threadId:h,status:k.length?"interrupted":"completed",state:y,next:k}}function m([,t,e]){if("custom"===t)return{type:"custom",data:e};if("messages"!==t)return;const[i,s]=e;if(!n(i)||!a(i)||!i.text)return;const{id:r,text:c}=i,d=String(s?.langgraph_node??"");if(!o(i))return{type:"message",messageId:r,node:d,text:c};const p=s?.tags;return{type:"token",messageId:r,node:d,tags:Array.isArray(p)?p.filter(t=>"string"==typeof t):[],text:c}}function k(t,a={}){return function(n,o,i){e.langGraph(t,`${n.constructor.name}:${o}`,a.description);const s=function(t,e,...a){return h(t,e,...a),t}(i.value,()=>`@langGraph can only decorate a method: ${o}`);return i.value=function(...t){const[e]=t;return(a=e)&&"object"==typeof a&&!0===a.__squidLangGraphRun__?async function(t,e,a){const n=await t;return h(function(t){return"function"==typeof t?.compile}(n),()=>`@langGraph function '${e}' must return an uncompiled StateGraph, got ${function(t){return null===t?"null":"object"==typeof t?t.constructor?.name??"object":typeof t}(n)}`),await f(n,a)}(s.apply(this,[]),o,e):s.apply(this,[]);var a},i}}export{g as SquidCheckpointer,k as langGraph,f as runLangGraphRequest};
|
|
@@ -3,6 +3,7 @@ import { AiFunctionId } from '@squidcloud/client';
|
|
|
3
3
|
import { AiAgentId, IntegrationId } from '@squidcloud/client';
|
|
4
4
|
import { AuthWithBearer } from '@squidcloud/client';
|
|
5
5
|
import { IntegrationType } from '@squidcloud/client';
|
|
6
|
+
import { Session, UpsertSessionRequest } from '@squidcloud/client';
|
|
6
7
|
/**
|
|
7
8
|
* Context object passed as a second parameter to every AI function call,
|
|
8
9
|
* providing both agent-specific and function-specific context.
|
|
@@ -24,6 +25,11 @@ export interface AiFunctionCallContext<FunctionContextType = unknown, AgentConte
|
|
|
24
25
|
functionContext: FunctionContextType;
|
|
25
26
|
/** The session associated with this AI function call. */
|
|
26
27
|
sessionContext: AiSessionContext;
|
|
28
|
+
/**
|
|
29
|
+
* The session (`squid.sessions()`) the agent call attached, read right before this function call. Absent when the
|
|
30
|
+
* call attached none, or when the session was deleted or expired.
|
|
31
|
+
*/
|
|
32
|
+
session?: Session;
|
|
27
33
|
/**
|
|
28
34
|
* Instructions of the calling AI agent, when the function runs an AI sub-agent that should
|
|
29
35
|
* honor them (e.g. formatting rules or domain conventions). For integration-connected functions
|
|
@@ -49,6 +55,16 @@ export declare class SecureAiAgentContext {
|
|
|
49
55
|
* `'models' in model` before reading it as a concrete model selection.
|
|
50
56
|
*/
|
|
51
57
|
readonly options?: AiChatRequestOptions;
|
|
58
|
+
/**
|
|
59
|
+
* The session the call asks to attach. Without the app API key, a new id creates it with the caller's `data`, and a
|
|
60
|
+
* live one is attached as it is: a rule that rejects it refuses the call.
|
|
61
|
+
*/
|
|
62
|
+
readonly session?: UpsertSessionRequest;
|
|
63
|
+
/**
|
|
64
|
+
* The live session stored under `session.id`, which a caller without the app API key attaches as it is: check that
|
|
65
|
+
* the caller may use it. Absent with the API key, which skips the rules.
|
|
66
|
+
*/
|
|
67
|
+
readonly storedSession?: Session;
|
|
52
68
|
}
|
|
53
69
|
/**
|
|
54
70
|
* Provides context to a knowledge base's `accessControl.scopeResolver` function, which turns the caller
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Session, UpsertSessionRequest } from '@squidcloud/client';
|
|
1
2
|
/**
|
|
2
3
|
* Which client-facing LangGraph action triggered this security check.
|
|
3
4
|
* @category AI
|
|
@@ -17,4 +18,14 @@ export declare class SecureLangGraphContext {
|
|
|
17
18
|
readonly resumePayload?: unknown;
|
|
18
19
|
/** The context the caller passed for the run (`invoke` and `resume`), which nodes read as `runtime.context`. */
|
|
19
20
|
readonly graphContext?: Record<string, unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* The session the call asks to attach (`invoke` and `resume`). Without the app API key, a new id creates it with the
|
|
23
|
+
* caller's `data`, and a live one is attached as it is: a rule that rejects it refuses the call.
|
|
24
|
+
*/
|
|
25
|
+
readonly session?: UpsertSessionRequest;
|
|
26
|
+
/**
|
|
27
|
+
* The live session stored under `session.id`, which a caller without the app API key attaches as it is: check that
|
|
28
|
+
* the caller may use it. Absent with the API key, which skips the rules.
|
|
29
|
+
*/
|
|
30
|
+
readonly storedSession?: Session;
|
|
20
31
|
}
|
|
@@ -6,7 +6,11 @@ export type StorageFunctionality = 'fileUpload' | 'getFileMetadata' | 'getDownlo
|
|
|
6
6
|
export interface StorageContext {
|
|
7
7
|
/** Identifier of the integration performing the storage action. */
|
|
8
8
|
integrationId: IntegrationId;
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* The keys the action runs on, as the bucket is accessed: each client path trimmed and stripped of one
|
|
11
|
+
* leading and one trailing '/', an uploaded file joined to its directory, a listed directory ending in
|
|
12
|
+
* '/' ('' for the bucket root). A path with a `.` or `..` segment is refused before any rule runs.
|
|
13
|
+
*/
|
|
10
14
|
pathsInBucket: Array<string>;
|
|
11
15
|
/** Type of action being performed on the storage. */
|
|
12
16
|
action: StorageActionType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squidcloud/backend",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.504",
|
|
4
4
|
"description": "Squid Cloud's backend SDK",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@langchain/core": "^1.1.48",
|
|
69
69
|
"@langchain/langgraph": "^1.4.0",
|
|
70
70
|
"@langchain/langgraph-checkpoint": "^1.1.0",
|
|
71
|
-
"@squidcloud/client": "^1.0.
|
|
71
|
+
"@squidcloud/client": "^1.0.504"
|
|
72
72
|
},
|
|
73
73
|
"peerDependenciesMeta": {
|
|
74
74
|
"@langchain/core": {
|